這個問題在這里已經有了答案: 什么是未定義的參考/未解決的外部符號錯誤,我該如何解決? (37 個回答) 17 小時前關閉。
我無法讓我的代碼運行,互聯網似乎也不知道為什么。我不確定我需要讓你知道什么,但如果有幫助,我正在使用 CLion。
這是我的 plant.h 檔案:
#ifndef COURSEWORK_PLANT_H
#define COURSEWORK_PLANT_H
using namespace std;
class Plant {
public:
void addGrowth();
int getSize();
string getName();
Plant(string x, int y);
private:
string plantName;
int plantSize;
};
#endif //COURSEWORK_PLANT_H
這是我的 plant.cpp 檔案:
#include <iostream>
#include "plant.h"
using namespace std;
void Plant::addGrowth(int x) {
plantSize = x;
cout << "You have added " << x << " leaves to your plant. Well done!";
}
int Plant::getSize() {
return Plant::plantSize;
}
string Plant::getName() {
return Plant::plantName;
}
這是我的 main.cpp 檔案:
#include <iostream>
#include "plant.h"
using namespace std;
int main() {
Plant myPlant("Bugg", 2);
return 0;
}
這是我的 CMakeLists.txt 檔案:
cmake_minimum_required(VERSION 3.21)
project(Coursework)
set(CMAKE_CXX_STANDARD 14)
add_executable(Coursework main.cpp plant.h plant.cpp)
預先感謝您的任何幫助!
uj5u.com熱心網友回復:
未定義符號表示符號已宣告但未定義。
例如,在類定義中,您有以下不帶引數的成員函式
void addGrowth();
但是你定義了一個同名但現在有一個引數的函式
void Plant::addGrowth(int x) {
plantSize = x;
cout << "You have added " << x << " leaves to your plant. Well done!";
}
所以類定義 Plant 中宣告的函式仍然是未定義的。
也沒有建構式的定義
Plant(string x, int y);
在您提供的代碼中。
如果您在標頭中使用string標準庫中的名稱,plant.h那么您需要包含標頭<string>
#include <string>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455275.html
