我知道之前已经提出这个问题,但我无法弄清楚我的计划有什么问题。
我有一个类和一个头文件:
State.cpp
#include "State.h"
#include "GameObject.h"
#include <iostream>
#include <iomanip>
#include <memory>
#include <iterator>
#include <vector>
#include "wordwrap.h"
using std::string;
using std::unique_ptr;
using namespace std;
using std::list;
/**
* Current state of the game.
*/
/**
* Display the description of the room the player is in. */
void State::announceLoc() const {
this->currentRoom->describe();
}
/**
* Constructor.
* @param startRoom Pointer to the room to start in.
*/
State::State(const Room *startRoom) : currentRoom(startRoom) {};
/**
* Move to a specified room and print its description.
* @param target Pointer to the room to move to.
*/
void State::goTo(const Room *target) {
this->currentRoom = target;
this->announceLoc();
}
GameObject State::addToInv(GameObject* gameobject) {
Inventory.push_back(gameobject);
}
void State::dispInv(){
for (auto test : Inventory){
wrapOut(test->getName());
wrapEndPara();
}
}
/**
* Return a pointer to the current room.
* @return Pointer to the current room.
*/
const Room* State::getCurrentRoom() const {
return this->currentRoom;
}
和State.h
#ifndef TEXTADV_STATE_H
#define TEXTADV_STATE_H
#include "Room.h"
#include "GameObject.h"
#include <list>
using std::string;
using namespace std;
using std::list;
class State {
const Room *currentRoom;
static list<GameObject*> Inventory;
public:
GameObject addToInv(GameObject* gameobject);
void dispInv();
explicit State(const Room *startRoom);
void goTo(const Room *target);
void announceLoc() const;
const Room* getCurrentRoom() const;
};
然而,当我尝试运行它时,我遇到了问题:
CMakeFiles \ ott.dir / objects.a(State.cpp.obj):在函数中
ZN5State8addToInvEP10GameObject': H:/.0 Year 3/C and C++/CW/ott/State.cpp:49: undefined reference to
国家::库存[ABI:cxx11]&#39; CMakeFiles \ ott.dir / objects.a(State.cpp.obj):在函数中ZN5State7dispInvEv': H:/.0 Year 3/C and C++/CW/ott/State.cpp:54: undefined reference to
国家::库存[ABI:cxx11]&#39; collect2.exe:错误:ld返回1退出 状态mingw32-make.exe [3]: * [ott.exe]错误1 mingw32-make.exe [2]: [CMakeFiles / ott.dir / all]错误2 CMakeFiles \ ott.dir \ build.make:225:目标&#39; ott.exe&#39;的配方 失败的CMakeFiles \ Makefile2:66:目标的配方 &#39; CMakeFiles / ott.dir /所有&#39;失败的CMakeFiles \ Makefile2:78:食谱 对于目标&#39; CMakeFiles / ott.dir / rule&#39; mingw32-make.exe失败[1]: [CMakeFiles / ott.dir / rule]错误2 Makefile:117:目标配方&#39; ott&#39; mingw32-make.exe失败:* [ott]错误2
我做错了什么,如何解决这个问题?