PDA

View Full Version : Function not Declared in This Scope - error



marinskye
8th April 2016, 00:49
Hi folks, I'm relatively new to Qt programming and have come across an error stating that a function I had created was 'not declared in this scope'.
However, I don't see how this is the case as I included the header file in which it was declared into the offending file.

The error I get is:

' adcreader.cpp: In constructor ‘ADCreader::ADCreader()’:
adcreader.cpp:79:29: error: ‘gz_clock_ena’ was not declared in this scope
gz_clock_ena(GZ_CLK_5MHz,5);'

The file in which the error comes from is called 'adcreader.cpp' and I have included the relevant sections to the error below, any help would be greatly appreciated. I do apologise for the length of the post.

```cpp


1 #include "adcreader.h"
2 #include <QDebug>
3
4 #include "gpio-sysfs.h"
5 #include <fcntl.h>
6 #include <sys/ioctl.h>
7 #include <linux/types.h>
8 #include <linux/spi/spidev.h>
9 #include <assert.h>
10 #include "gz_clk.h"
11
12 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
13
14 #define MAX_SAMPLES 65536
15
16 ADCreader::ADCreader()
17 {
18 int ret = 0;
...........................

77 // enable master clock for the AD
78 // divisor results in roughly 4.9MHz
79 // this also inits the general purpose IO
80 gz_clock_ena(GZ_CLK_5MHz,5);

.....

}

```

The file in which I declared the function is called gz_clk.h and I have included its code below:

```cpp


22 #ifndef GZ_CLK_H
23 #define GZ_CLK_H
24 #include <QtCore>
25 #include<bcm2835.h>
26
27 #define GZ_CLK_5MHz 0
28 #define GZ_CLK_125MHz 1
29
30 //int gz_clock_ena(int speed, int divider);
31 //int gz_clock_dis();
32
33 //#endif // GZ_CLK_H
34
35 class gz_clk : public QObject
36 {
37
38 Q_OBJECT
39
40 public:
41 gz_clk();
42 int gz_clock_ena(int speed, int divider);
43 int gz_clock_dis();
44
45
46 };
47
48 #endif // GZ_CLK_H

```

The file in which I implemented the function was in a file called gz_clk.cpp and its code is below:

```cpp


22 #include "gz_clk.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <QtCore>
27
28 #define GZ_CLK_BUSY (1 << 7)
29
30 gz_clk::gz_clk()
31
32 {
33 }
34
35 int gz_clk::gz_clock_ena(int speed, int divisor) {
36 int speed_id = 6;
37 if (speed < GZ_CLK_5MHz || speed > GZ_CLK_125MHz) {
38 printf("gz_clock_ena: Unsupported clock speed selected.\n");
39 printf("Supported speeds: GZ_CLK_5MHz (0) and GZ_CLK_125MHz (1).");
40 exit(-1);
41 }
42 if (speed == 0) {
43 speed_id = 1;
44 }
45 if (divisor < 2) {
46 printf("gz_clock_ena: Minimum divisor value is 2.");
47 exit(-1);
48 }
49 if (divisor > 0xfff) {
50 printf("gz_clock_ena: Maximum divisor value is %d.", 0xfff);
51 exit(-1);
52 }
53 if (bcm2835_init() !=1) {
54 printf("gz_clock_ena: Failed to initialize I/O\n");
55 exit(-1);
56 }
57 usleep(5);
58 bcm2835_gpio_fsel(RPI_GPIO_P1_07, BCM2835_GPIO_FSEL_ALT0);
59 *(bcm2835_clk + 0x1C) = 0x5A000000 | speed_id; // GPCLK0 off
60 while (*(bcm2835_clk + 0x1C) & GZ_CLK_BUSY) {} // Wait for BUSY low
61 *(bcm2835_clk + 0x1D) = 0x5A002000 | (divisor << 12); // set DIVI
62 *(bcm2835_clk + 0x1C) = 0x5A000010 | speed_id; // GPCLK0 on
63 return 0;
64
65 }
66
67 int gz_clk::gz_clock_dis() {
68 if (bcm2835_init() !=1) {
69 printf("gz_clock_dis: Failed to initialize I/O\n");
70 exit(-1);
71 }
72 bcm2835_gpio_fsel(RPI_GPIO_P1_07, BCM2835_GPIO_FSEL_INPT);
73 return 0;
74 }

```

Prady_80
8th April 2016, 02:47
Hi,

You have not invoked the function with the gz_clock_ena(...) with an object of class gz_clk. You can only use the function directly if you have inherited ADCreader from gz_clk. This has nothing to do with Qt.
Just a suggestion, please read the posting norms and use code formatting while posting.

marinskye
8th April 2016, 09:58
Hi,

You have not invoked the function with the gz_clock_ena(...) with an object of class gz_clk. You can only use the function directly if you have inherited ADCreader from gz_clk. This has nothing to do with Qt.
Just a suggestion, please read the posting norms and use code formatting while posting.

Hi, thanks a lot for your suggestion - a bad mistake. However, before checking here I managed to find another solution to my problem. The problem seemed to be a linking problem rather than a compiling one. Despite being installed, the library 'bcm2835' was not being properly detected. In the project's '.pro' file, I added the line 'LIBS += -lbcm2835 -lrt' - which allowed the program to compile and link without error.

marinskye
8th April 2016, 18:34
Good solution!