This program has the user input name
/age
pairs and then outputs them, using a class.
Here is the code.
#include "std_lib_facilities.h"
class Name_pairs
{
public:
bool test();
void read_names();
void read_ages();
void print();
private:
vector<string>names;
vector<double>ages;
string name;
double age;
};
void Name_pairs::read_names()
{
cout << "Enter name: ";
cin >> name;
names.push_back(name);
cout << endl;
}
void Name_pairs::read_ages()
{
cout << "Enter corresponding age: ";
cin >> age;
ages.push_back(age);
cout << endl;
}
void Name_pairs::print()
{
for(int i = 0; i < names.size() && i < ages.size(); ++i)
cout << names[i] << " , " << ages[i] << endl;
}
bool Name_pairs::test()
{
int i = 0;
if(ages[i] == 0 || names[i] == "0") return false;
else{
++i;
return true;}
}
int main()
{
cout << "Enter names and ages. Use 0 to cancel.n";
while(Name_pairs::test())
{
Name_pairs::read_names();
Name_pairs::read_ages();
}
Name_pairs::print();
keep_window_open();
}
However, in int main()
when I’m trying to call the functions I get "cannot call 'whatever name is' function without object."
I’m guessing this is because it’s looking for something like variable.test
or variable.read_names
. How should I go about fixing this?
Ziezi
6,3273 gold badges37 silver badges47 bronze badges
asked Jul 14, 2009 at 20:15
3
You need to instantiate an object in order to call its member functions. The member functions need an object to operate on; they can’t just be used on their own. The main()
function could, for example, look like this:
int main()
{
Name_pairs np;
cout << "Enter names and ages. Use 0 to cancel.n";
while(np.test())
{
np.read_names();
np.read_ages();
}
np.print();
keep_window_open();
}
Jason Plank
2,3424 gold badges32 silver badges40 bronze badges
answered Jul 14, 2009 at 20:19
sthsth
218k53 gold badges277 silver badges364 bronze badges
0
If you want to call them like that, you should declare them static.
answered Jul 14, 2009 at 20:20
Rob KRob K
8,6292 gold badges32 silver badges35 bronze badges
2
just add static keyword at the starting of the function return type..
and then you can access the member function of the class without object:)
for ex:
static void Name_pairs::read_names()
{
cout << "Enter name: ";
cin >> name;
names.push_back(name);
cout << endl;
}
answered Jun 4, 2019 at 9:57
You are right — you declared a new use defined type (Name_pairs) and you need variable of that type to use it.
The code should go like this:
Name_pairs np;
np.read_names()
O’Neil
3,7904 gold badges15 silver badges30 bronze badges
answered Jul 14, 2009 at 20:21
dimbadimba
26.3k30 gold badges138 silver badges195 bronze badges
- Fix the
error: cannot call member function without object
in C++ - Use an Instance of the Class to Access the Member Functions
- Use Static Member Functions
This article describes the commonly occurring error cannot call member function without object
while doing object-oriented programming using C++. Furthermore, it also provides potential fixes to the error.
Fix the error: cannot call member function without object
in C++
A common error in C++ frequently occurs when working in object-oriented programming, stating cannot call member function without object
. The cause of this error is that you are calling some member method of the class without instantiating the class.
Every class has a set of data members and some member functions. We need to create the class object to access the member methods or functions and then call/access the methods using this object.
Consider the following code.
#include<iostream>
using namespace std;
class Rectangle{
private:
int length=5;
int width=8;
public:
double getArea()
{
return length*width;
}
};
int main()
{
cout<<"Area: "<<Rectangle::getArea()<<endl;
}
This code will generate the following output.
Line #16 in the above code snippet tries to call the getArea()
method using the class name. All the non-static members of a class must only be accessed through an object of the class; therefore, the line generates the error.
Use an Instance of the Class to Access the Member Functions
The error can be solved by calling the function/method with an object of the class like this:
int main()
{
Rectangle r;
cout<<"Area: "<<r.getArea()<<endl;
}
This will give the correct output.
Use Static Member Functions
Static member functions are the functions of a class that do not need an object to call them. They can be called directly with the class name using the scope resolution operator ::
.
Certain limitations must be kept in mind when using the static member functions. A static member function can access only the static data members of the class and can only call the other static member functions.
Let’s look at the example below discussing the solution to the cannot call member function without object
error.
#include <iostream>
using namespace std;
class Rectangle{
private:
int length =5;
int width=8;
public:
double getArea()
{
return length*width;
}
static void getShapeName()
{
cout<<"Hello, I am a Rectangle."<<endl;
}
};
int main()
{
Rectangle r ;
cout<<"Area: "<<r.getArea()<<endl;
Rectangle::getShapeName();
}
This will give the following output.
Area: 40
Hello, I am a Rectangle.
- Forum
- Beginners
- Cannot call member function without obje
Cannot call member function without object
Hi, I’m having problem calling a function from a class in the main.cpp. Could someone tell me how to fix this please?
|
|
|
|
|
|
If I set the method to be static, then I get the following error: «cannot declare member function ‘static void CD::setd(double, double)’ to have static linkage».
Thank you in advance for your help!
A class represents a «thing», but the class itself is not actually a thing. You need to create objects of that class. Each object is its own thing.
For example… string
is a class. This means that you can have multiple «objects» of type string. Each has its own string data.
|
|
Here, foo and bar are both strings. Now you can call member functions (such as the length function) by using the object name and the dot operator:
|
|
But without an object, a call to length makes no sense:
|
|
This is basically what you’re doing. You’re trying to call setd, but you’re not telling the compiler which CD object you want to set. So it spits that error at you.
You’ll need to create an object:
|
|
Last edited on
where do I create that object?
Objects are just like normal variables (like ints, doubles, etc). Declare it wherever you need to use it.
Topic archived. No new replies allowed.
Mesteriis 599 / 237 / 69 Регистрация: 08.08.2015 Сообщений: 1,637 |
||||||||||||||||
1 |
||||||||||||||||
20.01.2017, 22:07. Показов 10700. Ответов 3 Метки нет (Все метки)
Доброе время суток ребят такая фигня, решил значит наконец то классы освоить но прям беда! чой то не пойму h файл
реализация класса
ну и маин файл
и собственно ошибка
0 |
GbaLog- Любитель чаепитий 3734 / 1793 / 563 Регистрация: 24.08.2014 Сообщений: 5,998 Записей в блоге: 1 |
||||
20.01.2017, 22:28 |
2 |
|||
Сообщение было отмечено Mesteriis как решение Решение
1 |
599 / 237 / 69 Регистрация: 08.08.2015 Сообщений: 1,637 |
|
20.01.2017, 22:32 [ТС] |
3 |
GbaLog-, А можешь в двух словах объяснить?
0 |
Любитель чаепитий 3734 / 1793 / 563 Регистрация: 24.08.2014 Сообщений: 5,998 Записей в блоге: 1 |
|
20.01.2017, 22:39 |
4 |
А можешь в двух словах объяснить? Если метод класса не статический, то для обращения к ним нужен экземпляр этого класса.
0 |
Автор | Тема: ошибка: cannot call member function without object (Прочитано 6550 раз) |
|
||||||
|
||||||
|
||||||
|
||||||
|
||||||
|
||||||
Python version:
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
GCC version:
mariyan@mz-lin:~/dev/spconv$ gcc --version
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
This is the error I am getting, any help would be appreciated! (essentially there are several errors that all say cannot call member function without object.)
tf) mariyan@mz-lin:~/dev/spconv$ python setup.py bdist_wheel
running bdist_wheel
running build
running build_py
running build_ext
Release
|||||CMAKE ARGS||||| ['-DCMAKE_PREFIX_PATH=/home/mariyan/.conda/envs/tf/lib/python3.7/site-packages/torch', '-DPYBIND11_PYTHON_VERSION=3.7', '-DSPCONV_BuildTests=OFF', '-DCMAKE_CUDA_FLAGS="--expt-relaxed-constexpr"', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/home/mariyan/dev/spconv/build/lib.linux-x86_64-3.7/spconv', '-DCMAKE_BUILD_TYPE=Release']
-- Caffe2: CUDA detected: 10.1.105
-- Caffe2: CUDA nvcc is: /usr/local/cuda-10.1/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda-10.1
-- Caffe2: Header version is: 10.1
-- Found cuDNN: v7.5.0 (include: /usr/local/cuda-10.1/include, library: /usr/local/cuda-10.1/lib64/libcudnn.so)
-- Autodetected CUDA architecture(s): 6.1
-- Added CUDA NVCC flags for: -gencode;arch=compute_61,code=sm_61
-- pybind11 v2.3.dev0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mariyan/dev/spconv/build/temp.linux-x86_64-3.7
[ 7%] Building CUDA object src/utils/CMakeFiles/spconv_nms.dir/nms.cu.o
[ 14%] Building CUDA object src/spconv/CMakeFiles/spconv.dir/indice.cu.o
[ 28%] Building CUDA object src/spconv/CMakeFiles/spconv.dir/maxpool.cu.o
[ 28%] Building CUDA object src/spconv/CMakeFiles/spconv.dir/reordering.cu.o
/usr/include/c++/7/bits/basic_string.tcc: In instantiation of ‘static std::basic_string<_CharT, _Traits, _Alloc>::_Rep* std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_create(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’:
/usr/include/c++/7/bits/basic_string.tcc:578:28: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&, std::forward_iterator_tag) [with _FwdIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.h:5033:20: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std::__false_type) [with _InIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.h:5054:24: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.tcc:656:134: required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/basic_string.h:6679:95: required from here
/usr/include/c++/7/bits/basic_string.tcc:1067:16: error: cannot call member function ‘void std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_M_set_sharable() [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’ without object
__p->_M_set_sharable();
~~~~~~~~~^~
/usr/include/c++/7/bits/basic_string.tcc: In instantiation of ‘static std::basic_string<_CharT, _Traits, _Alloc>::_Rep* std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_create(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’:
/usr/include/c++/7/bits/basic_string.tcc:578:28: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&, std::forward_iterator_tag) [with _FwdIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.h:5033:20: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std::__false_type) [with _InIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.h:5054:24: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.tcc:656:134: required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/basic_string.h:6684:95: required from here
/usr/include/c++/7/bits/basic_string.tcc:1067:16: error: cannot call member function ‘void std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_M_set_sharable() [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’ without object
src/utils/CMakeFiles/spconv_nms.dir/build.make:62: recipe for target 'src/utils/CMakeFiles/spconv_nms.dir/nms.cu.o' failed
make[2]: *** [src/utils/CMakeFiles/spconv_nms.dir/nms.cu.o] Error 1
CMakeFiles/Makefile2:202: recipe for target 'src/utils/CMakeFiles/spconv_nms.dir/all' failed
make[1]: *** [src/utils/CMakeFiles/spconv_nms.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
/usr/include/c++/7/bits/basic_string.tcc: In instantiation of ‘static std::basic_string<_CharT, _Traits, _Alloc>::_Rep* std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_create(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’:
/usr/include/c++/7/bits/basic_string.tcc:578:28: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&, std::forward_iterator_tag) [with _FwdIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.h:5033:20: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std::__false_type) [with _InIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.h:5054:24: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.tcc:656:134: required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/basic_string.h:6679:95: required from here
/usr/include/c++/7/bits/basic_string.tcc:1067:16: error: cannot call member function ‘void std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_M_set_sharable() [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’ without object
__p->_M_set_sharable();
~~~~~~~~~^~
/usr/include/c++/7/bits/basic_string.tcc: In instantiation of ‘static std::basic_string<_CharT, _Traits, _Alloc>::_Rep* std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_create(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’:
/usr/include/c++/7/bits/basic_string.tcc:578:28: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&, std::forward_iterator_tag) [with _FwdIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.h:5033:20: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std::__false_type) [with _InIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.h:5054:24: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.tcc:656:134: required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/basic_string.h:6684:95: required from here
/usr/include/c++/7/bits/basic_string.tcc:1067:16: error: cannot call member function ‘void std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_M_set_sharable() [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’ without object
src/spconv/CMakeFiles/spconv.dir/build.make:88: recipe for target 'src/spconv/CMakeFiles/spconv.dir/indice.cu.o' failed
make[2]: *** [src/spconv/CMakeFiles/spconv.dir/indice.cu.o] Error 1
make[2]: *** Waiting for unfinished jobs....
/usr/include/c++/7/bits/basic_string.tcc: In instantiation of ‘static std::basic_string<_CharT, _Traits, _Alloc>::_Rep* std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_create(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’:
/usr/include/c++/7/bits/basic_string.tcc:578:28: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&, std::forward_iterator_tag) [with _FwdIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.h:5033:20: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std::__false_type) [with _InIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.h:5054:24: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.tcc:656:134: required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/basic_string.h:6679:95: required from here
/usr/include/c++/7/bits/basic_string.tcc:1067:16: error: cannot call member function ‘void std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_M_set_sharable() [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’ without object
__p->_M_set_sharable();
~~~~~~~~~^~
/usr/include/c++/7/bits/basic_string.tcc: In instantiation of ‘static std::basic_string<_CharT, _Traits, _Alloc>::_Rep* std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_create(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’:
/usr/include/c++/7/bits/basic_string.tcc:578:28: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&, std::forward_iterator_tag) [with _FwdIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.h:5033:20: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std::__false_type) [with _InIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.h:5054:24: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.tcc:656:134: required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/basic_string.h:6684:95: required from here
/usr/include/c++/7/bits/basic_string.tcc:1067:16: error: cannot call member function ‘void std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_M_set_sharable() [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’ without object
src/spconv/CMakeFiles/spconv.dir/build.make:114: recipe for target 'src/spconv/CMakeFiles/spconv.dir/reordering.cu.o' failed
make[2]: *** [src/spconv/CMakeFiles/spconv.dir/reordering.cu.o] Error 1
/usr/include/c++/7/bits/basic_string.tcc: In instantiation of ‘static std::basic_string<_CharT, _Traits, _Alloc>::_Rep* std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_create(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’:
/usr/include/c++/7/bits/basic_string.tcc:578:28: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&, std::forward_iterator_tag) [with _FwdIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.h:5033:20: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std::__false_type) [with _InIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.h:5054:24: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = const char16_t*; _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’
/usr/include/c++/7/bits/basic_string.tcc:656:134: required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/basic_string.h:6679:95: required from here
/usr/include/c++/7/bits/basic_string.tcc:1067:16: error: cannot call member function ‘void std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_M_set_sharable() [with _CharT = char16_t; _Traits = std::char_traits<char16_t>; _Alloc = std::allocator<char16_t>]’ without object
__p->_M_set_sharable();
~~~~~~~~~^~
/usr/include/c++/7/bits/basic_string.tcc: In instantiation of ‘static std::basic_string<_CharT, _Traits, _Alloc>::_Rep* std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_create(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’:
/usr/include/c++/7/bits/basic_string.tcc:578:28: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&, std::forward_iterator_tag) [with _FwdIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.h:5033:20: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std::__false_type) [with _InIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.h:5054:24: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = const char32_t*; _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’
/usr/include/c++/7/bits/basic_string.tcc:656:134: required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/basic_string.h:6684:95: required from here
/usr/include/c++/7/bits/basic_string.tcc:1067:16: error: cannot call member function ‘void std::basic_string<_CharT, _Traits, _Alloc>::_Rep::_M_set_sharable() [with _CharT = char32_t; _Traits = std::char_traits<char32_t>; _Alloc = std::allocator<char32_t>]’ without object
src/spconv/CMakeFiles/spconv.dir/build.make:140: recipe for target 'src/spconv/CMakeFiles/spconv.dir/maxpool.cu.o' failed
make[2]: *** [src/spconv/CMakeFiles/spconv.dir/maxpool.cu.o] Error 1
CMakeFiles/Makefile2:108: recipe for target 'src/spconv/CMakeFiles/spconv.dir/all' failed
make[1]: *** [src/spconv/CMakeFiles/spconv.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2
Traceback (most recent call last):
File "setup.py", line 89, in <module>
zip_safe=False,
File "/home/mariyan/.conda/envs/tf/lib/python3.7/site-packages/setuptools/__init__.py", line 145, in setup
return distutils.core.setup(**attrs)
File "/home/mariyan/.conda/envs/tf/lib/python3.7/distutils/core.py", line 148, in setup
dist.run_commands()
File "/home/mariyan/.conda/envs/tf/lib/python3.7/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/home/mariyan/.conda/envs/tf/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/home/mariyan/.conda/envs/tf/lib/python3.7/site-packages/wheel/bdist_wheel.py", line 192, in run
self.run_command('build')
File "/home/mariyan/.conda/envs/tf/lib/python3.7/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/home/mariyan/.conda/envs/tf/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/home/mariyan/.conda/envs/tf/lib/python3.7/distutils/command/build.py", line 135, in run
self.run_command(cmd_name)
File "/home/mariyan/.conda/envs/tf/lib/python3.7/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/home/mariyan/.conda/envs/tf/lib/python3.7/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "setup.py", line 40, in run
self.build_extension(ext)
File "setup.py", line 73, in build_extension
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
File "/home/mariyan/.conda/envs/tf/lib/python3.7/subprocess.py", line 347, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '--build', '.', '--config', 'Release', '--', '-j4']' returned non-zero exit status 2.
-
23rd October 2010, 04:07
#1
cannot call member function without object
I get the following error when trying to connect two classes together:
qapp.cpp: In static member function 'static bool QApp::eventFilter(void*)':
qapp.cpp:32: error: cannot call member function 'void QApp::mouseCoord(int, int, int, int, int)' without object
To copy to clipboard, switch view to plain text mode
main.cpp:
int main(int argc, char *argv[])
{
QApp a(argc, argv);
Widget w;
w.show();
QObject::connect(&a,SIGNAL(mouseCoords(int, int, int, int, int)),
&w,SLOT(mouseCoords(int, int, int, int, int)));
return a.exec();
}
To copy to clipboard, switch view to plain text mode
qapp.cpp:
emit mouseCoord(get_raw_mouse_x_delta(i), get_raw_mouse_y_delta(i), // this is line 32...
is_raw_mouse_button_pressed(i, 0), is_raw_mouse_button_pressed(i, 1), is_raw_mouse_button_pressed(i, 2));
To copy to clipboard, switch view to plain text mode
I have searched the forum and found several issues quite like mine, but not enough to help me out.
-
23rd October 2010, 05:54
#2
Re: cannot call member function without object
As the compiler error message says, you cannot call a member function, which requires a «this» pointer, from a static method that does not have a «this» pointer to give. This might do it:
QApp *app = qobject_cast<QApp>(qApp);
app->mouseCoords(...);
To copy to clipboard, switch view to plain text mode
BTW: Is that mouseCoord() or mouseCoords() ? You aren’t consistent.
-
23rd October 2010, 13:01
#3
Re: cannot call member function without object
If I do this:
QApp *app = qobject_cast<QApp>(qApp);
app->mouseCoords(...); // what is this exactly? Am I not supposed to use connect()?
To copy to clipboard, switch view to plain text mode
Then what happens to this?
QApp a(argc, argv);
To copy to clipboard, switch view to plain text mode
Thanks for the help.
-
23rd October 2010, 13:23
#4
Re: cannot call member function without object
I would ask first why did you make your QApp::eventFilter() method static?
Your biological and technological distinctiveness will be added to our own. Resistance is futile.
Please ask Qt related questions on the forum and not using private messages or visitor messages.
-
23rd October 2010, 15:18
#5
Re: cannot call member function without object
I made it static because that’s the only way it would work with QAbstractEventDispatcher(). I was experiencing some difficulties with setting QAbstractEventDispatcher() filter and a thread here in QtCentre Forum said that I should make eventFilter() static, I did and it worked. Only for that reason. But if there is a better way I would really like to know.
EDIT:
if I remove «static» from eventFilter I get this error message:qapp.cpp: In constructor 'QApp::QApp(int&, char**)':
qapp.cpp:14: error: no matching function for call to 'QAbstractEventDispatcher::setEventFilter(<unresolved overloaded function type>)'
c:Qt2010.05qtincludeQtCore/../../src/corelib/kernel/qabstracteventdispatcher.h:91: note: candidates are: bool (* QAbstractEventDispatcher::setEventFilter(bool (*)(void*)))(void*)
To copy to clipboard, switch view to plain text mode
Last edited by been_1990; 23rd October 2010 at 15:30.
-
23rd October 2010, 15:30
#6
Re: cannot call member function without object
But this method shouldn’t be static. And if you make it static then you can’t call non-static methods from it. What do you need QAbstractEventDispatcher for and what is that you can’t make work without making eventFilter() static?
Your biological and technological distinctiveness will be added to our own. Resistance is futile.
Please ask Qt related questions on the forum and not using private messages or visitor messages.
-
23rd October 2010, 15:33
#7
Re: cannot call member function without object
I need QAbstractEventDispatcher to be able to get WM_INPUT messages because winEvenFilter won’t do it.
EDIT:
if I remove «static» from eventFilter I get this error message:qapp.cpp: In constructor 'QApp::QApp(int&, char**)':
qapp.cpp:14: error: no matching function for call to 'QAbstractEventDispatcher::setEventFilter(<unresolved overloaded function type>)'
c:Qt2010.05qtincludeQtCore/../../src/corelib/kernel/qabstracteventdispatcher.h:91: note: candidates are: bool (* QAbstractEventDispatcher::setEventFilter(bool (*)(void*)))(void*)
To copy to clipboard, switch view to plain text mode
-
23rd October 2010, 15:52
#8
Re: cannot call member function without object
Can we see the exact code?
Your biological and technological distinctiveness will be added to our own. Resistance is futile.
Please ask Qt related questions on the forum and not using private messages or visitor messages.
-
23rd October 2010, 16:09
#9
Re: cannot call member function without object
Yes. Here it is:
main.cpp:#include <QtGui/QApplication>
#include <QObject>
#include "widget.h"
#include "qapp.h"
int main(int argc, char *argv[])
{
//QApp *app = qobject_cast<QApp>(qApp);
QApp a(argc, argv);
Widget w;
w.show();
QObject::connect(&a,SIGNAL(mouseCoord(int, int, int, int, int)),
&w,SLOT(mouseCoord(int, int, int, int, int)));
return a.exec();
}
To copy to clipboard, switch view to plain text mode
qapp.cpp:
extern "C"{
#include "raw_mouse.h"
}
#include "qapp.h"
#include <QDebug>
#include <QAbstractEventDispatcher>
#define WM_INPUT 0x00FF
QApp::QApp(int & argc, char ** argv) :
{
qDebug() << init_raw_mouse(1, 0, 1);
qDebug() << raw_mouse_count();
}
bool QApp::eventFilter(void *msg)
{
MSG *mess;
mess = (MSG*)msg;
//qDebug() << mess->message;
switch(mess->message){
case WM_INPUT:
{
add_to_raw_mouse_x_and_y((HANDLE)mess->lParam);
for (int i = 0; i < raw_mouse_count(); i++){
emit mouseCoord(get_raw_mouse_x_delta(i), get_raw_mouse_y_delta(i),
is_raw_mouse_button_pressed(i, 0), is_raw_mouse_button_pressed(i, 1), is_raw_mouse_button_pressed(i, 2));
}
}
return true;
}
return false;
}
To copy to clipboard, switch view to plain text mode
app.h:
#ifndef QAPP_H
#define QAPP_H
#include <QApplication>
{
Q_OBJECT
public:
explicit QApp(int & argc, char ** argv );
//bool winEventFilter(MSG *message, long *result);
static bool eventFilter(void *msg);
private:
signals:
void mouseCoord(int x, int y, int btn1, int btn2, int btn3);
public slots:
};
#endif // QAPP_H
To copy to clipboard, switch view to plain text mode
-
23rd October 2010, 17:01
#10
Re: cannot call member function without object
Why not do it this way?
bool myInputEventFilter(void*);
Q_OBJECT
friend bool myInputEventFilter(void *);
public:
QApp(int argc, char **argv) : QApplication(argc, argv){
init_raw_mouse(1, 0, 1);
}
signals:
void mouseCoord(int x, int y, int btn1, int btn2, int btn3);
};
// the following function may probably be made static so that it doesn't pollute the namespace
bool myInputEventFilter(void *msg) {
MSG *mess = (MSG*)msg;
QApp *app = (QApp*)qApp;
switch(mess->message){
case WM_INPUT:
{
add_to_raw_mouse_x_and_y((HANDLE)mess->lParam);
for (int i = 0; i < raw_mouse_count(); i++){
emit app->mouseCoord(get_raw_mouse_x_delta(i), get_raw_mouse_y_delta(i),
is_raw_mouse_button_pressed(i, 0), is_raw_mouse_button_pressed(i, 1), is_raw_mouse_button_pressed(i, 2));
}
}
return true;
}
return false;
}
To copy to clipboard, switch view to plain text mode
Your biological and technological distinctiveness will be added to our own. Resistance is futile.
Please ask Qt related questions on the forum and not using private messages or visitor messages.
-
23rd October 2010, 18:07
#11
Re: cannot call member function without object
Great! Working perfectly now. Thanks Wysota.
What do you mean with «pollute the namespace»?
-
23rd October 2010, 18:12
#12
Re: cannot call member function without object
Originally Posted by been_1990
What do you mean with «pollute the namespace»?
Try defining (in a different cpp file) a function called myInputEventFilter and taking a void* as input and you’ll see for yourself.
Your biological and technological distinctiveness will be added to our own. Resistance is futile.
Please ask Qt related questions on the forum and not using private messages or visitor messages.