【发布时间】:2022-11-02 21:58:31
【问题描述】:
我对 C++ 很陌生,我正在尝试在一个名为 Room 的类中初始化一个名为 GameObject 的对象,该类包含一个 gameObjects array,其中包含两个 GameObject GameObject 类的构造函数采用 @987654328 @ 作为参数来初始化字段。但是我不断收到错误消息,说“没有匹配的构造函数用于初始化GameObject。有人可以告诉我这里的错误是什么吗?
抱歉,如果这个问题的格式不正确,我不习惯用多个头文件和源文件来问 C++ 问题。但也请就此纠正我。
GameObject C++ 源文件:
#include "GameObject.h"
#include "wordwrap.h"
using std::string;
GameObject::GameObject(string* _name, string* _description, char* _keyWord):
name(_name), description(_description), keyWord(_keyWord){
}
Room C++ 源文件:
#include "Room.h"
#include "wordwrap.h"
Room::Room(const string* _name, const string *_desc) :
name(_name), description(_desc), north(nullptr) {
//error!, "No matching constructor for initialization..."
gameObjects[0] = new GameObject("knife", "a knife", 'k');
gameObjects[1] = new GameObject("sword", "a sword", 's');
};
【问题讨论】:
-
为什么
std::string*s 而不是std::strings? -
(a) 请阅读How to Ask 和minimal reproducible example,因为类声明有助于回答问题。 (b)
string*参数不寻常,我希望string const &或只是string(对于接收器参数)。
标签: c++ pointers constructor