【发布时间】:2020-10-08 03:22:28
【问题描述】:
我在使用 c++ 时收到此错误。我尝试了其他解决方案,例如使用共享指针,但无济于事。这是我的代码:
Main.cpp:
int main() {
game_init();
cout << items.at(0).name << endl;
while(true) game_tick();
return 0;
}
项目.h:
#ifndef ITEM_H
#define ITEM_H
#include "../Rooms/Room.h"
#include <string>
#include <vector>
#include <memory>
using namespace std;
class Item {
public :
Room location;
string name, desc;
//Item(string, string, Room);
virtual void use() = 0;
};
vector <Item> items;
/*
Item::Item(string in_name, string in_desc, Room in_location) {
name = in_name;
desc = in_desc;
location = in_location;
}*/
#endif
Items.h:
#ifndef ITEMS_H
#define ITEMS_H
#include "OldKey.h"
#include <memory>
#include <vector>
vector <shared_ptr<Item>> all_items;
OldKey old_key;
void items_init() {
items.push_back(old_key);
}
#endif
错误是抽象类类型“项目”的无效新表达式。希望我能解决这个问题,请记住我是 C++ 新手,所以请尝试简单地解释解决方案或解决方法,谢谢。
【问题讨论】:
-
我认为这将有助于您理解抽象类:Why can't we create an instance of an abstract class?
-
我找到了删除错误消息的方法。在项目类中,我定义了子类没有定义它的用途。但这意味着如果我要使用命令 items.at(anyindex).use(),它只会给出预定义的函数。我不知道该怎么做,希望得到帮助。
标签: c++