This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
                                
                                    (32个答案)
                                
                        
                                4年前关闭。
            
                    
我只是在学习构造函数和析构函数,我正在遵循这个人正在做的教程bucky tutorial。我认为视频已经过时了吗?因为我遵循了他说的每一个步骤,但我仍然遇到错误。

main.cpp

#include <iostream>
#include "TESTING.h"

using namespace std;

int main(){

    TESTING so;
    cout << "TEST" << endl;

}


测试

#ifndef TESTING_H
#define TESTING_H


class TESTING
{
    public:
        TESTING();
    protected:
        private:
};

#endif // TESTING_H


TESTING.cpp

#include "TESTING.h"
#include <iostream>
using namespace std;


TESTING::TESTING()
{
    cout << "TESTTTTT!!" << endl;
}


错误讯息

\ main.o:main.cpp未定义对'TESTING :: TESTING()'的引用

构建日志

mingw32-g ++。exe -c D:\ C ++ \ TESTING!\ main.cpp -o D:\ C ++ \ TESTING!\ main.o
mingw32-g ++。exe -o D:\ C ++ \ TESTING!\ main.exe D:\ C ++ \ TESTING!\ main.o
D:\ C ++ \ TESTING!\ main.o:main.cpp :(。text + 0x52):未定义对`TESTING :: TESTING()'的引用
collect2.exe:错误:ld返回1退出状态
进程以状态1(0分钟,1秒)终止
1个错误,0个警告(0分钟,1秒)

最佳答案

您只能构建和链接main源文件,而不是TESTING源文件。您还需要编译TESTING.cpp,然后与TESTING.o链接:

mingw32-g ++。exe -c D:\ C ++ \ TESTING!\ TESTING.cpp -o D:\ C ++ \ TESTING!\ TESTING.o
mingw32-g ++。exe -o D:\ C ++ \ TESTING!\ main.exe D:\ C ++ \ TESTING!\ main.o D:\ C ++ \ TESTING!\ TESTING.o

关于c++ - 未定义对class::()C++的引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31890215/

10-13 08:30