c++:初始化没有匹配的构造函数/候选构造函数不可行:需要单个参数,但没有提供参数
C++: no matching constructor for initialization / candidate constructor not viable: requires single argument, but no arguments were provided
我正试图在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
^
这是我的NodeType类在我的linkedList.h文件:
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 };
非常简单。查看NodeType
的构造函数(linkedList.h,第10行)。它需要一个字符串。然而,当您创建NodeType
的新实例(linkedList.cpp,第18行,第54行)时,您调用的构造函数没有任何参数。
正确的方法是…
NodeType* newnode = new NodeType("Some string.");
如果你想让构造函数中的字符串参数为可选,你可以通过将构造函数的头原型修改为…
NodeType(std::string str="Some default value");
还可以重载构造函数,一个不带参数,一个接受字符串,等等。
你知道,这是一个非常基本的原则。如果你是一名学生(正式或非正式),你会想复习c++面向对象编程,特别是"构造函数"。
相关文章:
- C++:使用运算符 = 调用多参数构造函数
- 通过零参数构造函数创建的 glm::mat4 应该包含哪些值?
- 好奇的混合与可变参数构造函数
- 具有默认值的单个参数构造函数是否与默认构造函数相同?
- 为什么我们需要创建一个单参数构造函数来使用临时的无名称对象
- 在可变参数构造函数中初始化常量数组
- C++ 显式多参数构造函数歧义
- 零一参数构造函数
- 可变参数构造函数中的 SFINAE
- 当没有显式关键字与单参数构造函数一起使用时,编译器可以发出警告
- 可变参数类模板和可变参数构造函数
- 确保模板参数类型与其可变参数构造函数的类型匹配
- C++默认参数构造函数与内联初始化优先级
- 如何在 c++ 中将包含复制构造函数的类的参数构造函数称为私有?
- 自动存储中没有无参数构造函数的类对象和异常
- 警告:用两个参数构造函数返回对象时,表达结果未使用
- 如何在C++中调用无参数构造函数
- 在 c++ 中具有多个参数构造函数的模板类存在问题
- 可变参数构造函数优先于用户提供的移动构造函数,除非默认
- 如何从可变参数构造函数参数构造任何对象?