如何解决在 VS Code 中链接 C++ 期间出现未定义符号错误
所以我遇到了一个问题,当我将类与主 .cpp 一起编译时,出现链接错误。我不是最精通 C++ 的人,因为这是我第一年用这种语言编码。我也在为此使用 VS Code。
我的团队课程:
#ifndef TEAM_H
#define TEAM_H
#include "Player.h"
#include <vector>
using namespace std;
class Team{
public:
Team();
void setGold(int _Gold);
int getGold();
void changeGold(int _Gold);
void setIngredients(int _Ingredients);
int getIngredients();
void changeIngredients(int _Gold);
void setCookware(int _Cookware[3]);
void changeCookware(int _Cookware[3]);
void setArmor(int _Armor);
int getArmor();
void changeArmor(int _Armor);
void addWeapon(string _Name,string _Level);
string removeOneWeapon();
int getWeapons();
int getPlayers();
void setKey(bool _Key);
bool getKey();
void updateFull(int _Fullness);
static string weapons[5][2]; //First index is name,Second is level
//"NULL" and "NULL" = no weapon,static int cookware[3];//0,1,2 index's equate to number of that type of cookware
vector<string> getTreasures();
int getTreasureSize();
static Player players[5];
private:
int gold;
int ingredients;
int armor;
bool key;
vector<string> treasures;
};
#endif
void Game::startGame(){
string leaderName;
string playerOne,playerTwo,playerThree,playerFour;
monster1.readMonsterFile("monsters.txt");
cout << "Enter name of party leader:" << endl;
cin >> leaderName;
Team::players[0].setName(leaderName);
cout << "Enter the names of other party members:" << endl;
cin >> playerOne >> playerTwo >> playerThree >> playerFour;
Team::players[1].setName(playerOne);
Team::players[2].setName(playerTwo);
Team::players[3].setName(playerThree);
Team::players[4].setName(playerFour);
cout << "Welcome party leader " << leaderName << " and his/her companions " << playerOne << playerTwo << playerThree << playerFour << endl;
}
然后我得到的错误:
Williams-MacBook-Pro:Project3 William$ g++ -std=c++11 Player.cpp Team.cpp Monster.cpp Game.cpp main.cpp
Undefined symbols for architecture x86_64:
"Team::players",referenced from:
Team::getPlayers() in Team-b8a69d.o
Team::updateFull(int) in Team-b8a69d.o
Game::startGame() in Game-98f56e.o
Game::statusUpdate() in Game-98f56e.o
"Team::weapons",referenced from:
Team::Team() in Team-b8a69d.o
Team::getWeapons() in Team-b8a69d.o
Team::addWeapon(std::__1::basic_string<char,std::__1::char_traits<char>,std::__1::allocator<char> >,std::__1::basic_string<char,std::__1::allocator<char> >) in Team-b8a69d.o
Team::removeOneWeapon() in Team-b8a69d.o
Monster::attack(int,int,int) in Monster-7397ef.o
Game::statusUpdate() in Game-98f56e.o
"Team::cookware",referenced from:
Team::Team() in Team-b8a69d.o
Team::setCookware(int*) in Team-b8a69d.o
Team::changeCookware(int*) in Team-b8a69d.o
Game::statusUpdate() in Game-98f56e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command Failed with exit code 1 (use -v to see invocation)
非常感谢任何帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。