【问题标题】:C++: no matching constructor for initialization / candidate constructor not viable: requires single argument, but no arguments were providedC ++:没有用于初始化的匹配构造函数/候选构造函数不可行:需要单个参数,但没有提供参数
【发布时间】:2016-01-10 09:35:48
【问题描述】:

我正在尝试在 C++ 中实现一个链表,但不知道如何修复下面给出的错误。我将非常感谢调试方面的任何帮助,更重要的是,我会解释我对此错误不理解的任何基本概念。谢谢。

这是我的错误:

linkedList.cpp:19:23: error: no matching constructor for initialization of 'NodeType'
        newnode = new NodeType;
                      ^
./linkedList.h:10:9: note: candidate constructor not viable: requires single argument 'str', but no arguments
      were provided
        NodeType(const std::string str);
        ^
./linkedList.h:4:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1
      argument, but 0 were provided
class NodeType
      ^
linkedList.cpp:54:29: error: no matching constructor for initialization of 'NodeType'
    NodeType* newnode = new NodeType;
                            ^
./linkedList.h:10:9: note: candidate constructor not viable: requires single argument 'str', but no arguments
      were provided
        NodeType(const std::string str);
        ^
./linkedList.h:4:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1
      argument, but 0 were provided
class NodeType
      ^

这是我的 linkedList.h 文件中的 NodeType 类:

  3 // define the node class
  4 class NodeType
  5 {
  6     private:
  7         std::string value;
  8         NodeType* next;
  9     public:
 10         NodeType(std::string str);
 11     friend class LinkedList;
 12 };

这里是linkedList.cpp文件中第一个错误的方法:

 10 LinkedList::LinkedList(const LinkedList& src)
 11 {
 12     head = NULL;
 13     NodeType* srccur; //node that is currently in src
 14     srccur = src.head;
 15 
 16     NodeType* pre = NULL; //predecessor of the new node
 17     while (srccur != NULL) // have not finished yet
 18     {   NodeType* newnode;
 19         newnode = new NodeType;
 20         if (newnode == NULL) //dynamic allocation failed
 21             { cout << "Memory allocation error" << endl;
 22                 exit(1);
 23             }
 24 
 25     newnode->value = srccur->value;
 26 
 27     if (pre == NULL)    //the new node becomes the 1st node
 28         head = newnode;
 29     else    // the new node is attached to the end
 30         pre->next = newnode;
 31 
 32     newnode->next = NULL;
 33 
 34     pre = newnode;
 35     srccur = srccur->next;
 36     }
 37 };

这里是linkedList.cpp文件中第二个错误的方法:

 53 bool LinkedList::insertFirst(string v){
 54     NodeType* newnode = new NodeType;
 55 
 56     if (newnode == NULL) // dynamic allocation failed
 57     {
 58         return false;
 59     }
 60 
 61     newnode->value = v;
 62     newnode->next = head;
 63 
 64     head = newnode;
 65 
 66     return true;
 67 };

【问题讨论】:

    标签: c++ constructor linked-list initialization nodes


    【解决方案1】:

    非常简单。查看 NodeType 的构造函数(linkedList.h,第 10 行)。它需要一个字符串。然而,当您创建 NodeType 的新实例(linkedList.cpp,第 18 行,第 54 行)时,您调用的构造函数没有任何参数。

    正确的做法是……

    NodeType* newnode = new NodeType("Some string.");
    

    如果您希望构造函数中的字符串参数为可选,您可以通过将构造函数的标头原型更改为...

    NodeType(std::string str="Some default value");
    

    另外,你可以重载你的构造函数,一个不带参数,一个接受字符串,等等。


    您知道,这是一个非常基本的原则。如果您是学生(正式或非正式),您需要复习 C++ 面向对象编程,特别是“构造函数”。

    【讨论】:

    • 是的...我首先学习了 Python,并且将是第一个承认我对 C++ 的理解在这一点上非常不完整的人。感谢您提供信息丰富的答案。我也会听取你的建议。
    • @SergeiWallace,我也进行了这种转换,并开始学习 C++ 中的 OOP。这很有帮助:stackoverflow.com/questions/388242/…
    猜你喜欢
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2020-11-17
    • 2017-06-27
    • 1970-01-01
    相关资源
    最近更新 更多