Results 1 to 3 of 3

Thread: Function not Declared in This Scope error (However I believe I have declared it.....)

  1. #1

    Default Function not Declared in This Scope error (However I believe I have declared it.....)

    Hi folks,
    I have got what I believe is a very basic question. I get an error saying that the function 'pabort' was not declared in this scope when another function tries to use it.
    I am running some code, there's quite a few different files that are involved in the compilation but the two relevant to my error are a header file titled 'adcreader.h' and an implementation file titled 'adcreader.cpp'.

    The adcreader.cpp file is:

    Qt Code:
    1. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    2. #define MAX_SAMPLES 65536
    3.  
    4. void ADCreader::pabort(const char *s)
    5. {
    6. perror(s);
    7. abort();
    8. }
    9.  
    10.  
    11.  
    12.  
    13.  
    14. ADCreader::ADCreader()
    15. {
    16. int ret = 0;
    17. int fd;
    18. /// int sysfs_fd;
    19.  
    20. // int no_tty = !isatty( fileno(stdout) );
    21.  
    22. // set up ringbuffer
    23. samples = new int[MAX_SAMPLES];
    24. // pointer for incoming data
    25. pIn = samples;
    26. // pointer for outgoing data
    27. pOut = samples;
    28. //SPI Constants
    29. static const char *device = "/dev/spidev0.0";
    30. static uint8_t mode = SPI_CPHA | SPI_CPOL;
    31. static uint8_t bits = 8;
    32. static int drdy_GPIO = 22;
    33.  
    34. //Open SPI Device
    35. fd = open(device, O_RDWR);
    36. if (fd < 0)
    37. pabort("can't open device");
    38.  
    39. /*
    40. * spi mode
    41. */
    42. ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
    43. if (ret == -1)
    44. pabort("can't set spi mode");
    45.  
    46. ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
    47. if (ret == -1)
    48. pabort("can't get spi mode");
    49.  
    50. /*
    51. * bits per word
    52. */
    53. ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    54. if (ret == -1)
    55. pabort("can't set bits per word");
    56.  
    57. ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
    58. if (ret == -1)
    59. pabort("can't get bits per word");
    60.  
    61. fprintf(stderr, "spi mode: %d\n", mode);
    62. fprintf(stderr, "bits per word: %d\n", bits);
    63.  
    64. // enable master clock for the AD
    65. // divisor results in roughly 4.9MHz
    66. // this also inits the general purpose IO
    67. gz_clock_ena(GZ_CLK_5MHz,5);
    68.  
    69. // enables sysfs entry for the GPIO pin
    70. gpio_export(drdy_GPIO);
    71. // set to input
    72. gpio_set_dir(drdy_GPIO,0);
    73. // set interrupt detection to falling edge
    74. gpio_set_edge(drdy_GPIO,"falling");
    75. // get a file descriptor for the GPIO pin
    76. sysfs_fd = gpio_fd_open(drdy_GPIO);
    77.  
    78. // resets the AD7705 so that it expects a write to the communication register
    79. printf("sending reset\n");
    80. writeReset(fd);
    81.  
    82. // tell the AD7705 that the next write will be to the clock register
    83. writeReg(fd,0x20);
    84. // write 00001100 : CLOCKDIV=1,CLK=1,expects 4.9152MHz input clock
    85. writeReg(fd,0x0C);
    86.  
    87. // tell the AD7705 that the next write will be the setup register
    88. writeReg(fd,0x10);
    89. // intiates a self calibration and then after that starts converting
    90. writeReg(fd,0x40);
    91. }
    92.  
    93.  
    94.  
    95.  
    96. void ADCreader::writeReset(int fd)
    97. {
    98. int ret;
    99. uint8_t tx1[5] = {0xff,0xff,0xff,0xff,0xff};
    100. uint8_t rx1[5] = {0};
    101. struct spi_ioc_transfer tr;
    102.  
    103. memset(&tr,0,sizeof(struct spi_ioc_transfer));
    104. tr.tx_buf = (unsigned long)tx1;
    105. tr.rx_buf = (unsigned long)rx1;
    106. tr.len = sizeof(tx1);
    107.  
    108. ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
    109. if (ret < 1) {
    110. printf("\nerr=%d when trying to reset. \n",ret);
    111. pabort("Can't send spi message");
    112. }
    113. }
    114.  
    115.  
    116. void writeReg(int fd, uint8_t v)
    117. {
    118. int ret;
    119. uint8_t tx1[1];
    120. tx1[0] = v;
    121. uint8_t rx1[1] = {0};
    122. struct spi_ioc_transfer tr;
    123.  
    124. memset(&tr,0,sizeof(struct spi_ioc_transfer));
    125. tr.tx_buf = (unsigned long)tx1;
    126. tr.rx_buf = (unsigned long)rx1;
    127. tr.len = sizeof(tx1);
    128.  
    129. ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
    130. if (ret < 1)
    131. pabort("can't send spi message");
    132. }
    133.  
    134. uint8_t ADCreader::readReg(int fd)
    135. {
    136. int ret;
    137. uint8_t tx1[1];
    138. tx1[0] = 0;
    139. uint8_t rx1[1] = {0};
    140. struct spi_ioc_transfer tr;
    141.  
    142. memset(&tr,0,sizeof(struct spi_ioc_transfer));
    143. tr.tx_buf = (unsigned long)tx1;
    144. tr.rx_buf = (unsigned long)rx1;
    145. tr.len = sizeof(tx1);
    146.  
    147. ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
    148. if (ret < 1)
    149. pabort("can't send spi message");
    150.  
    151. return rx1[0];
    152. }
    153.  
    154. int ADCreader::readData(int fd)
    155. {
    156. int ret;
    157. uint8_t tx1[2] = {0,0};
    158. uint8_t rx1[2] = {0,0};
    159. struct spi_ioc_transfer tr;
    160.  
    161. memset(&tr,0,sizeof(struct spi_ioc_transfer));
    162. tr.tx_buf = (unsigned long)tx1;
    163. tr.rx_buf = (unsigned long)rx1;
    164. tr.len = sizeof(tx1);
    165.  
    166. ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
    167. if (ret < 1)
    168. {
    169. printf("\n can't send spi message, ret = %d\n",ret);
    170. exit(1);
    171. }
    172.  
    173. return (rx1[0]<<8)|(rx1[1]);
    174. }
    175.  
    176.  
    177.  
    178.  
    179. void ADCreader::run() //triggered when thread is started
    180. // we read data in an endless loop and display it
    181. // this needs to run in a thread ideally
    182. {
    183. int ret;
    184. int sysfs_fd;
    185. int no_tty = !isatty( fileno(stdout) );
    186.  
    187.  
    188. while (1) {
    189.  
    190. fprintf("We are running!\n");
    191.  
    192. // let's wait for data for max one second
    193. ret = gpio_poll(sysfs_fd,1000);
    194. if (ret<1) {
    195. fprintf(stderr,"Poll error %d\n",ret);
    196. }
    197.  
    198. // tell the AD7705 to read the data register (16 bits)
    199. writeReg(fd,0x38);
    200. // read the data register by performing two 8 bit reads
    201. int value = readData(fd)-0x8000;
    202. fprintf(stderr,"data = %d \r",value);
    203. // qDebug << value;
    204.  
    205. //*pIn = value;
    206. // if (pIn == (&samples[MAX_SAMPLES-1]))
    207. // pIn = samples;
    208. //else
    209. //pIn++;
    210. // if stdout is redirected to a file or pipe, output the data
    211. if( no_tty )
    212. {
    213. printf("%d\n", value);
    214. fflush(stdout);
    215. }
    216. }
    217.  
    218. close(fd);
    219. gpio_fd_close(sysfs_fd);
    220.  
    221. //return ret;
    222. }
    223.  
    224. int ADCreader::getSample()
    225. {
    226. assert(pOut!=pIn);
    227. int value = *pOut;
    228. if (pOut == (&samples[MAX_SAMPLES-1]))
    229. pOut = samples;
    230. else
    231. pOut++;
    232. return value;
    233. }
    234.  
    235. int ADCreader::hasSample()
    236. {
    237. return (pOut!=pIn);
    238. }
    239.  
    240. void ADCreader::quit()
    241. {
    242. running = false;
    243. exit(0);
    244. }
    To copy to clipboard, switch view to plain text mode 

    and the adcreader.h file is:

    Qt Code:
    1. class ADCreader : public QThread //taking this class and inheriting QThread,
    2. //Qthread has a bpublic function called run
    3. {
    4. public:
    5. ADCreader();
    6.  
    7. // ring buffer functions
    8. int hasSample();
    9. int getSample();
    10.  
    11.  
    12. public:
    13. // thread functions
    14. void quit();
    15. void run(); //when we start thread, this is the function going to be called.
    16.  
    17. protected:
    18. void writeReset(int fd);
    19. void writeReg(int fd, uint8_t v);
    20. uint8_t readReg(int fd);
    21. int readData(int fd);
    22. //int gz_clock_ena(int, int);
    23. private:
    24. bool running;
    25. void pabort(const char *s);
    26.  
    27. // file descriptor on the SPI interface
    28. int fd;
    29.  
    30. // file descriptor on the interrupt pin
    31. int sysfs_fd;
    32.  
    33. // data collected
    34. int *samples;
    35.  
    36. // pointer to ringbuffer
    37. int *pIn;
    38. int *pOut;
    39.  
    40. // spi constants for the ioctrl calls
    41. uint8_t mode;
    42. uint8_t bits;
    43. uint32_t speed;
    44. uint16_t delay;
    45. int drdy_GPIO;
    46.  
    47. };
    48.  
    49. #endif
    To copy to clipboard, switch view to plain text mode 

    When compiling I get the following error:

    'adcreader.cpp: In function ‘void writeReg(int, uint8_t)’:
    adcreader.cpp:160:36: error: ‘pabort’ was not declared in this scope
    pabort("can't send spi message");'

    From my code, I can't understand why I would get the error as I had declared it in the header file and declared it in the '.cpp' file. Any help would be greatly appreciated.

    Thanks,
    Martin

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Function not Declared in This Scope error (However I believe I have declared it..

    "writeReg" is a standalone function. It is not a member of the ADCreader class, therefore it cannot call non-static member functions of the ADCreader class without using an instance of that class. The compiler is telling you that there is no standalone function called "pabort()", which is true. If you want to call the pabort() method of ADCreader, you have to do it through an instance of that class:

    Qt Code:
    1. ADCreader myReader;
    2. myReader.pabort( "Whatever" );
    3.  
    4. // or
    5.  
    6. ADCreader * myReader = new ADCreader;
    7. myReader->pabort( "Whatever" );
    8. delete myReader;
    To copy to clipboard, switch view to plain text mode 

    Probably you don't want to do either of these alternatives, because it is likely you have a global instance of ADCreader that you are using in your program, and that's the one you want to call pabort(). If it isn't visible in the same scope as writeReg(), then you can make it visible by passing it as an argument to writeReg() (preferred) or declaring the global instance as an "extern ADCreader * myGlobalReader;" and ensuring that your global instance is named the same thing and assigned properly.

    I think it would be useful to you to learn to walk in C++ before you run while holding a RaspberryPi. Get a C++ book, learn about classes, scoping, and the like, then have at it.

    --Edit--

    Actually, it looks like you just forgot to make writeReg() a member function of ADCreader, since you have a "readReg" member function already. Still, you need to learn more C++ so you can recognize what it means when your compiler points out these errors to you.
    Last edited by d_stranz; 11th April 2016 at 23:35.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Function not Declared in This Scope error (However I believe I have declared it..

    You probably wanted to have writeReg as a member function like the others.
    You even have the prototype declared like one, but your implementation is missing the class name, so it is, as d_stranz said, a stand alone function.

    So add the ADCreader:: prefix in front of writeReg and it should build.

    Cheers,
    _

Similar Threads

  1. Function not Declared in This Scope - error
    By marinskye in forum Qt Programming
    Replies: 3
    Last Post: 8th April 2016, 17:34
  2. Replies: 10
    Last Post: 22nd September 2010, 06:20
  3. Replies: 2
    Last Post: 16th July 2010, 07:17
  4. glutSolidSphere was not declared in this scope error
    By nuts_fever_007 in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2009, 04:56
  5. error: 'connect' was not declared in this scope ??
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2007, 15:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.