问题描述
所以我遇到了一个问题,当我将类与主 .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 (将#修改为@)