c++ - 对类::() C++ 的 undefined reference

标签 c++ constructor destructor

<分区>

我只是在学习构造函数和析构函数我正在学习这个人正在做的教程bucky tutorial .我觉得视频过时了?因为我遵循了他说的每一步,但我仍然遇到错误。

main.cpp

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

using namespace std;

int main(){

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

}

测试.h

#ifndef TESTING_H
#define TESTING_H


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

#endif // TESTING_H

测试.cpp

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


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

错误信息

\main.o:main.cpp 对“TESTING::TESTING()”的 undefined reference

构建日志

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): undefined reference to `TESTING::TESTING()'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
1 error(s), 0 warning(s) (0 minute(s), 1 second(s))

最佳答案

您只能构建和链接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++ - 对类::() C++ 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31890215/

相关文章:

c++ - (Ab)使用构造函数和析构函数进行副作用不好的做法?备择方案?

c++ - 可以使用类的析构函数内部函数来重置值吗?

C++ 做 OS X 或 Linux header 不同

javascript - this.element 在对象构造函数内部无法识别

scala - 从特征引用构造函数参数

c++ - 具有多个非可选参数的转换构造函数看起来如何,为什么有意义?

c++ - 独特元素的组合,无需重复

C++:获取类实例的大小

c++ - "type"可以在 C++ 中传递吗?

c++ - vector 析构函数导致程序在 C++ 中崩溃