簡體   English   中英

頭文件中沒有對構造函數的匹配函數調用

[英]No matching function call call to constructor in header file

我已經看到類似的問題,並嘗試過解決方案,但對他們的答案似乎沒有用。 我有以下代碼:

。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;
}

我收到錯誤消息: error: no matching function for call to '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 &);

這是因為在構造函數中,您要指定參數為常量類型的字符串,並且在創建對象時要傳遞字符串。 類型不匹配是問題所在,要么將構造函數參數固定為字符串,要么在創建對象時進行更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM