問題描述
為什么代碼塊會(huì)給出這個(gè)錯(cuò)誤未定義對 class::classfunction() 的引用"在一個(gè)單獨(dú)的文件中創(chuàng)建一個(gè)類時(shí)會(huì)發(fā)生這種情況.所有這些文件都在同一個(gè)文件夾中
Why does codeblocks give this error "Undefined reference to class::classfunction()" It happens when a class is created in a separated file.All of these files are in the same folder
這是主要的 .cpp 文件
This is the main .cpp file
#include<iostream>
#include "Class2.h"
using namespace std;
main()
{
Class2 classObject;
cout<<"I'm class2"<<endl;
}
類頭文件
#ifndef CLASS2_H
#define CLASS2_H
class Class2
{
public:
Class2();
~Class2();
protected:
private:
};
#endif // CLASS2_H
類cpp文件
#include "Class2.h"
#include<iostream>
using namespace std;
Class2::Class2()
{
cout<<"Hello, I'm Constructor"<<endl;
}
Class2::~Class2()
{
cout<<"Yo!! I'm Destructor"<<endl;
}
錯(cuò)誤是對 Class2::Class2() 的未定義引用"
error is "undefined reference to Class2::Class2()"
推薦答案
您需要將 main.o
和 class.o
鏈接到您的可執(zhí)行文件中.確切的命令取決于您的編譯器和操作系統(tǒng).對于 g++ 命令看起來像
You need to link both main.o
and class.o
into your executable. The exact command depends on your compiler and OS. For g++ the command would look something like
g++ -o main main.cpp class.cpp
這篇關(guān)于C++ 錯(cuò)誤,未定義的引用類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!