gpt4 book ai didi

c++ - 头文件中没有对构造函数的匹配函数调用调用

转载 作者:行者123 更新时间:2023-11-30 03:28:08 25 4
gpt4 key购买 nike

我看过类似的问题并尝试了他们的解决方案,但对他们的回答似乎不起作用。我有以下代码:

.h

#include <iostream>
#include <vector>
#include <string>
using std::string; using std::vector;

struct DialogueNode;

struct DialogueOption {
string text;
DialogueNode *next_node;
int return_code;

DialogueOption(string t, int rc, DialogueNode * nn) : text{t},
return_code{rc}, next_node{nn} {}
};

struct DialogueNode {
string text;
vector <DialogueOption> dialogue_options;
DialogueNode();
DialogueNode(const string &);
};

struct DialogueTree {
DialogueTree() {}
void init();
void destroyTree();

int performDialogue();
private:
vector <DialogueNode*> dialogue_nodes;
};

.cpp

#include "dialogue_tree.h"

DialogueNode::DialogueNode(const string &t) : text{t} {}

void DialogueTree::init() {
string s = "Hello";
for(int i = 0; i < 5; i++) {
DialogueNode *node = new DialogueNode(s);
dialogue_nodes.push_back(node);
delete node;
}
}

void DialogueTree::destroyTree() {

}

int DialogueTree::performDialogue() {
return 0;
}

int main() {
return 0;
}

我得到错误:错误:没有匹配函数调用'DialogueNode::DialogueNode(std::__cxx11::string&)' DialogueNode *node = new DialogueNode(s);

编辑关于错误的附加说明

dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode()
dialogue_tree.h:17:8: note: candidate expects 0 arguments, 1 provided
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode(const DialogueNode&)
dialogue_tree.h:17:8: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const DialogueNode&’
dialogue_tree.h:17:8: note: candidate: DialogueNode::DialogueNode(DialogueNode&&)
dialogue_tree.h:17:8: note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘DialogueNode&&’

这对我来说毫无意义,因为我将构造函数定义为将 string 作为参数。

最佳答案

您已将构造函数声明为:

DialogueNode(const string);

但是定义为:

DialogueNode(const string &t);

这两个不一样;前者采用 const string 而后者采用 const string 引用。您必须添加 & 以指定引用参数:

DialogueNode(const string &);

关于c++ - 头文件中没有对构造函数的匹配函数调用调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com