本文介紹了C++ 錯誤,未定義的引用類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
為什么代碼塊會給出這個錯誤未定義對 class::classfunction() 的引用"在一個單獨的文件中創建一個類時會發生這種情況.所有這些文件都在同一個文件夾中
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;
}
錯誤是對 Class2::Class2() 的未定義引用"
error is "undefined reference to Class2::Class2()"
推薦答案
您需要將 main.o
和 class.o
鏈接到您的可執行文件中.確切的命令取決于您的編譯器和操作系統.對于 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
這篇關于C++ 錯誤,未定義的引用類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!