[英]undefined reference to constructor c++
我已經搜索並找到了解決此問題的方法,但我覺得這有點奇怪。 無論如何,我的問題是這樣的:
個人.h
class Personal
{
public:
Personal();
int money;
~Personal();
}
個人.cpp
#include "Personal.h"
Personal::Personal()
{
money = 1800;
}
Personal::~Personal(){};
現在我想在 main 中編譯
主程序
#include "Personal.h"
#include <iostream>
#include <vector>
int main()
{
std::vector<Personal> test(100);
}
當我寫: g++ -Wall main.cpp -o main 它給了我:
undefine reference to Personal::Personal()
undefine reference to Personal::~Personal()
解決方案:
g++ -Wall Personal.cpp main.cpp -o main
為什么我也需要編譯 Personal.cpp?
或者另一個主要版本是包含而不是“Personal.h”,“Personal.cpp”
主程序
#include "Personal.cpp"
#include <iostream>
#include <vector>
然后正常的 g++ -Wall main.cpp -o main 工作
有人能幫我嗎?
為什么我也需要編譯 Personal.cpp?
因為您使用在該文件中定義的函數。 特別是,您使用Personal::Personal
和Personal::~Personal
函數。
有人能幫我嗎?
確保所有函數(odr 使用的)都定義在您編譯和鏈接在一起的源文件中的一個(或所有文件中,如果是內聯函數)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.