Error invalid use of non-static data member

I'm having a bad time trying to call a correctly-working expression (ultra[currentSensor].ping_timer(echoCheck)) from an own class method (myclass::run) instead of from void loop().

I'm getting the following compiler error: "invalid use of non-static member function".

If I call ultra[currentSensor].ping_timer(echoCheck) directly from void loop() it gives me no error, so I may be missing something.

Here's the (shortened) piece of code which is generating the error:

#include <NewPing.h> #define ULTRA_NUM 3 #define MAX_DISTANCE 200 class myclass { public: void run(); void echoCheck(); unsigned int distance[ULTRA_NUM]; uint8_t currentSensor = 0; }; NewPing ultra[ULTRA_NUM] = { NewPing(4, 5, MAX_DISTANCE), NewPing(6, 7, MAX_DISTANCE), NewPing(8, 9, MAX_DISTANCE) }; void myclass::run() { // .... ultra[currentSensor].ping_timer(echoCheck); // Compiler error } void myclass::echoCheck() { if (ultra[currentSensor].check_timer()) distance[currentSensor] = ultra[currentSensor].ping_result }

ping_timer() is defined as it follows:

void NewPing::ping_timer(void (*userFunc)(void), unsigned int max_cm_distance) { if (max_cm_distance > 0) set_max_distance(max_cm_distance); if (!ping_trigger()) return; timer_us(ECHO_TIMER_FREQ, userFunc); }

I hope any of you can help me!

Thanks in advance,

Eduardo

Edit: What I am trying to do is to adapt this example (from NewPing Arduino Library) so that the sonar ping (sonar[currentSensor].ping_timer(echoCheck)) is done from a class method, and not from void loop().

@Hamish

The reason i had it as Quiz::ui is because its not in the same cpp file its in a seperate file called util.cpp, this is for organisation reasons, if i do not do this it throws the error "Use of undeclared 'ui'" so i assumed i needed to mention the class name similar to java

I often say "C++ is not Java", but mostly is in different context - memory management. Funnily, here it's even more applicable. In Java you are supposed to put every (public) class in it's own file (if I remember correctly), C++ doesn't care, it also doesn't care how you name your files . You can put the declaration in a header and then put the definitions in any cpp you want (as long as you don't duplicate the class/method names). It also doesn't care how many classes (exported or otherwise) you declare in a header file.

Well, if you make global Widgets variables, this can happen. As globals are created first. Before main is run.

Can i fix this?

You shouldn't create anything that's QObject derived before the QCoreApplication constructor has run. The first QObject you create should be the application object. My advice, forget global variables, especially since you appear to not have much experience with C++. In Java they are mostly okay, but with C++ they are a real pain and lead to all sorts of complications. Do as @mrjj suggested and put your objects as members of classes.

PS.

void appendInput(QString string) { Quiz::ui->input->setText(string); }

These functions are global functions, and not class members, you should convert them to methods instead and in these methods you can operate your ui object.

Kind regards.

Following the install process for the Pico on a Raspberry Pi 4 using the pico_setup.sh script, when it got to the building of the pico-examples, a couple of failures were seen:

OS Version: PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"

##Build errors**

make[5]: *** [CMakeFiles/elf2uf2.dir/build.make:63: CMakeFiles/elf2uf2.dir/main.cpp.o] Error 1 make[4]: *** [CMakeFiles/Makefile2:73: CMakeFiles/elf2uf2.dir/all] Error 2 make[3]: *** [Makefile:84: all] Error 2 make[2]: *** [blink/CMakeFiles/ELF2UF2Build.dir/build.make:111: blink/elf2uf2/src/ELF2UF2Build-stamp/ELF2UF2Build-build] Error 2 make[1]: *** [CMakeFiles/Makefile2:1578: blink/CMakeFiles/ELF2UF2Build.dir/all] Error 2 make: *** [Makefile:84: all] Error 2

Build Output

Adding PICO_EXAMPLES_PATH to ~/.bashrc Cloning //github.com/raspberrypi/pico-extras.git Cloning into 'pico-extras'... remote: Enumerating objects: 151, done. remote: Counting objects: 100% (151/151), done. remote: Compressing objects: 100% (110/110), done. remote: Total 151 (delta 12), reused 151 (delta 12), pack-reused 0 Receiving objects: 100% (151/151), 124.12 KiB | 1.88 MiB/s, done. Resolving deltas: 100% (12/12), done. Submodule 'lwip' (//git.savannah.nongnu.org/git/lwip.git) registered for path 'lib/lwip' Cloning into '/home/pi/development/pico/pico/pico-extras/lib/lwip'... Submodule path 'lib/lwip': checked out 'c385f31076b27efb8ee37f00cb5568783a58f299' Adding PICO_EXTRAS_PATH to ~/.bashrc Cloning //github.com/raspberrypi/pico-playground.git Cloning into 'pico-playground'... remote: Enumerating objects: 140, done. remote: Counting objects: 100% (140/140), done. remote: Compressing objects: 100% (117/117), done. remote: Total 140 (delta 24), reused 128 (delta 17), pack-reused 0 Receiving objects: 100% (140/140), 1.86 MiB | 2.43 MiB/s, done. Resolving deltas: 100% (24/24), done. Adding PICO_PLAYGROUND_PATH to ~/.bashrc Using PICO_SDK_PATH from environment ('/home/pi/development/pico/pico/pico-sdk') Pico SDK is located at /home/pi/development/pico/pico/pico-sdk Defaulting PICO_PLATFORM to rp2040 since not specified. Defaulting PICO platform compiler to pico_arm_gcc since not specified. PICO compiler is pico_arm_gcc PICO_GCC_TRIPLE defaulted to arm-none-eabi -- The C compiler identification is GNU 7.3.1 -- The CXX compiler identification is GNU 7.3.1 -- The ASM compiler identification is GNU -- Found assembler: /usr/bin/arm-none-eabi-gcc Using regular optimized debug build (set PICO_DEOPTIMIZED_DEBUG=1 to de-optimize) Defaulting PICO target board to pico since not specified. Using board configuration from /home/pi/development/pico/pico/pico-sdk/src/boards/include/boards/pico.h -- Found Python3: /usr/bin/python3.7 (found version "3.7.3") found components: Interpreter TinyUSB available at /home/pi/development/pico/pico/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; adding USB support. Compiling TinyUSB with CFG_TUSB_DEBUG=1 -- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) ELF2UF2 will need to be built PIOASM will need to be built -- Configuring done -- Generating done -- Build files have been written to: /home/pi/development/pico/pico/pico-examples/build Building blink Scanning dependencies of target ELF2UF2Build Scanning dependencies of target bs2_default [ 0%] Creating directories for 'ELF2UF2Build' [ 0%] Building ASM object pico_sdk/src/rp2_common/boot_stage2/CMakeFiles/bs2_default.dir/boot2_w25q080.S.obj [ 0%] Linking ASM executable bs2_default.elf [ 0%] Built target bs2_default Scanning dependencies of target bs2_default_bin [ 0%] No download step for 'ELF2UF2Build' [ 0%] Generating bs2_default.bin [ 0%] Built target bs2_default_bin [ 0%] No patch step for 'ELF2UF2Build' [ 0%] No update step for 'ELF2UF2Build' Scanning dependencies of target bs2_default_padded_checksummed_asm [ 0%] Generating bs2_default_padded_checksummed.S [ 0%] Performing configure step for 'ELF2UF2Build' [ 0%] Built target bs2_default_padded_checksummed_asm -- The C compiler identification is GNU 5.5.0 -- The CXX compiler identification is GNU 5.5.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/pi/development/pico/pico/pico-examples/build/elf2uf2 [ 0%] Performing build step for 'ELF2UF2Build' Scanning dependencies of target elf2uf2 [ 50%] Building CXX object CMakeFiles/elf2uf2.dir/main.cpp.o /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:68:58: error: invalid use of non-static data member ‘address_range::type’ address_range(FLASH_START, FLASH_END, address_range::type::CONTENTS), ^ /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:55:10: note: declared here type type; ^ /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:69:64: error: invalid use of non-static data member ‘address_range::type’ address_range(MAIN_RAM_START, MAIN_RAM_END, address_range::type::NO_CONTENT ^ /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:55:10: note: declared here type type; ^ /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:70:1: error: no matching function for call to ‘std::vector<address_range>::vector(<brace-enclosed initializer list>)’ }; ^ In file included from /usr/include/c++/5/vector:64:0, from /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:9: /usr/include/c++/5/bits/stl_vector.h:401:9: note: candidate: template<class _InputIterator, class> std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) vector(_InputIterator __first, _InputIterator __last, ^ /usr/include/c++/5/bits/stl_vector.h:401:9: note: template argument deduction/substitution failed: /usr/include/c++/5/bits/stl_vector.h:373:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(initializer_list<value_type> __l, ^ /usr/include/c++/5/bits/stl_vector.h:373:7: note: conversion of argument 1 would be ill-formed: /usr/include/c++/5/bits/stl_vector.h:348:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(vector&& __rv, const allocator_type& __m) ^ /usr/include/c++/5/bits/stl_vector.h:348:7: note: conversion of argument 1 would be ill-formed: /usr/include/c++/5/bits/stl_vector.h:339:7: note: candidate: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&, const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(const vector& __x, const allocator_type& __a) ^ /usr/include/c++/5/bits/stl_vector.h:339:7: note: conversion of argument 1 would be ill-formed: /usr/include/c++/5/bits/stl_vector.h:335:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = address_range; _Alloc = std::allocator<address_range>] vector(vector&& __x) noexcept ^ /usr/include/c++/5/bits/stl_vector.h:335:7: note: candidate expects 1 argument, 2 provided /usr/include/c++/5/bits/stl_vector.h:318:7: note: candidate: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = address_range; _Alloc = std::allocator<address_range>] vector(const vector& __x) ^ /usr/include/c++/5/bits/stl_vector.h:318:7: note: candidate expects 1 argument, 2 provided /usr/include/c++/5/bits/stl_vector.h:289:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::size_type = unsigned int; std::vector<_Tp, _Alloc>::value_type = address_range; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(size_type __n, const value_type& __value, ^ /usr/include/c++/5/bits/stl_vector.h:289:7: note: conversion of argument 1 would be ill-formed: /usr/include/c++/5/bits/stl_vector.h:277:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::size_type = unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(size_type __n, const allocator_type& __a = allocator_type()) ^ /usr/include/c++/5/bits/stl_vector.h:277:7: note: conversion of argument 1 would be ill-formed: /usr/include/c++/5/bits/stl_vector.h:264:7: note: candidate: std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT ^ /usr/include/c++/5/bits/stl_vector.h:264:7: note: candidate expects 1 argument, 2 provided /usr/include/c++/5/bits/stl_vector.h:253:7: note: candidate: std::vector<_Tp, _Alloc>::vector() [with _Tp = address_range; _Alloc = std::allocator<address_range>] vector() ^ /usr/include/c++/5/bits/stl_vector.h:253:7: note: candidate expects 0 arguments, 2 provided /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:73:64: error: invalid use of non-static data member ‘address_range::type’ address_range(MAIN_RAM_START, MAIN_RAM_END, address_range::type::CONTENTS), ^ /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:55:10: note: declared here type type; ^ /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:74:60: error: invalid use of non-static data member ‘address_range::type’ address_range(0x00000000u, 0x00002000u, address_range::type::IGNORE) // for ^ /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:55:10: note: declared here type type; ^ /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:75:1: error: no matching function for call to ‘std::vector<address_range>::vector(<brace-enclosed initializer list>)’ }; ^ In file included from /usr/include/c++/5/vector:64:0, from /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:9: /usr/include/c++/5/bits/stl_vector.h:401:9: note: candidate: template<class _InputIterator, class> std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) vector(_InputIterator __first, _InputIterator __last, ^ /usr/include/c++/5/bits/stl_vector.h:401:9: note: template argument deduction/substitution failed: /usr/include/c++/5/bits/stl_vector.h:373:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(initializer_list<value_type> __l, ^ /usr/include/c++/5/bits/stl_vector.h:373:7: note: conversion of argument 1 would be ill-formed: /usr/include/c++/5/bits/stl_vector.h:348:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&, const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(vector&& __rv, const allocator_type& __m) ^ /usr/include/c++/5/bits/stl_vector.h:348:7: note: conversion of argument 1 would be ill-formed: /usr/include/c++/5/bits/stl_vector.h:339:7: note: candidate: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&, const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(const vector& __x, const allocator_type& __a) ^ /usr/include/c++/5/bits/stl_vector.h:339:7: note: conversion of argument 1 would be ill-formed: /usr/include/c++/5/bits/stl_vector.h:335:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>&&) [with _Tp = address_range; _Alloc = std::allocator<address_range>] vector(vector&& __x) noexcept ^ /usr/include/c++/5/bits/stl_vector.h:335:7: note: candidate expects 1 argument, 2 provided /usr/include/c++/5/bits/stl_vector.h:318:7: note: candidate: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = address_range; _Alloc = std::allocator<address_range>] vector(const vector& __x) ^ /usr/include/c++/5/bits/stl_vector.h:318:7: note: candidate expects 1 argument, 2 provided /usr/include/c++/5/bits/stl_vector.h:289:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::size_type = unsigned int; std::vector<_Tp, _Alloc>::value_type = address_range; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(size_type __n, const value_type& __value, ^ /usr/include/c++/5/bits/stl_vector.h:289:7: note: conversion of argument 1 would be ill-formed: /usr/include/c++/5/bits/stl_vector.h:277:7: note: candidate: std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::size_type = unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(size_type __n, const allocator_type& __a = allocator_type()) ^ /usr/include/c++/5/bits/stl_vector.h:277:7: note: conversion of argument 1 would be ill-formed: /usr/include/c++/5/bits/stl_vector.h:264:7: note: candidate: std::vector<_Tp, _Alloc>::vector(const allocator_type&) [with _Tp = address_range; _Alloc = std::allocator<address_range>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<address_range>] vector(const allocator_type& __a) _GLIBCXX_NOEXCEPT ^ /usr/include/c++/5/bits/stl_vector.h:264:7: note: candidate expects 1 argument, 2 provided /usr/include/c++/5/bits/stl_vector.h:253:7: note: candidate: std::vector<_Tp, _Alloc>::vector() [with _Tp = address_range; _Alloc = std::allocator<address_range>] vector() ^ /usr/include/c++/5/bits/stl_vector.h:253:7: note: candidate expects 0 arguments, 2 provided /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp: In function ‘int check_address_range(const address_ranges&, uint32_t, uint32_t, uint32_t, bool, address_range&)’: /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:120:46: error: ‘address_range::type’ is not a class, namespace, or enumeration if (range.type == address_range::type::NO_CONTENTS && !uninitialize ^ /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp: In function ‘int read_and_check_elf32_ph_entries(FILE*, const elf32_header&, const address_ranges&, std::map<unsigned int, std::vector<page_fragment> >&)’: /home/pi/development/pico/pico/pico-sdk/tools/elf2uf2/main.cpp:153:51: error: ‘address_range::type’ is not a class, namespace, or enumeration if (ar.type != address_range::type::CONTENTS) { ^ make[5]: *** [CMakeFiles/elf2uf2.dir/build.make:63: CMakeFiles/elf2uf2.dir/main.cpp.o] Error 1 make[4]: *** [CMakeFiles/Makefile2:73: CMakeFiles/elf2uf2.dir/all] Error 2 make[3]: *** [Makefile:84: all] Error 2 make[2]: *** [blink/CMakeFiles/ELF2UF2Build.dir/build.make:111: blink/elf2uf2/src/ELF2UF2Build-stamp/ELF2UF2Build-build] Error 2 make[1]: *** [CMakeFiles/Makefile2:1578: blink/CMakeFiles/ELF2UF2Build.dir/all] Error 2 make: *** [Makefile:84: all] Error 2

What is non

Non-static data members are the variables that are declared in a member specification of a class.

How do you declare a static data member in C++?

Static Data Members in C++ The static data member is always initialized to zero when the first class object is created. static data_type data_member_name; In the above syntax, static keyword is used. The data_type is the C++ data type such as int, float etc.

Where do you initialize a non

Member initialization Non-static data members may be initialized in one of two ways: 1) In the member initializer list of the constructor.

Can static member functions access non

Explanation: A static function is a special type of function which is used to access only static data, any other normal data cannot be accessed through static function.

Toplist

Latest post

TAGs