开发者

Undefined reference linking error when using &MyClass::MyFunction

this just has me stumped, so I thought I'd query here:

I have a class as follows:

class MyClass {
    public:
        void myThreadFunc();
};

That's in the header. In the constructor

MyClass::MyClass() {
    ...
    boost::thread t(boost::bind(&MyClass::myThreadFunc, this));
    ...
}

As I've seen done. There are NO compile time errors. 开发者_如何学PythonHowever, when I link as follows:

g++ -o test.exe main.o MyClass.o /*specify boost and other libraries */

I get:

MyClass.o:MyClass.cpp:(.text+0xa4): undefined reference to `MyClass::myThreadFunc()'
collect2: ld returned 1 exit status

Which doesn't make any sense. What strikes me especially odd is that's its a linker error. I included both of my object files.

Can anyone tell me what's going on? If it might be relevant, I'm on MinGW on Windows.

EDIT:

Epic fail. I forgot the MyClass:: prefix when defining the function in my cpp file. I just didn't decide to check that. Almost as bad as forgetting a semicolin after a class definition.


You need to write a function body for MyClass::myThreadFunc() somewhere. Writing a constructor for MyClass is different from implementing the MyClass::myThreadFunc() member function.

If you call a function in C/C++, it must have a function body somewhere. That's why it's a linker error; it's trying to find the function body in all of the available object files, but you didn't write one so it can't.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜