超载函数调用功能具有相同名称,但参数不同
Overloaded function calling function with same name but different parameters
我正在创建一个链接列表。我想调用函数
addToTail(head*, data)
来自功能
addToTail(head*){int n = length(head*); addtoTail(head*, n)}
包含一个可以计算列表长度的函数。
不幸的是,这给了我一个错误
linkedList.cpp:97:16: error: too few arguments to function 'void addAtTail(node*, int)'
这对我来说似乎很有意义,我做错了吗?
这是代码。任何以改进它的评论将不胜感激。
#include<iostream>
using namespace std;
struct node
{
int data;
struct node* next;
};
void changeToNull(struct node** head)
{
*head = NULL;
}
int listLength(struct node* head)
{
struct node* curr = head;
int i = 0;
while(curr!=NULL)
{
i++;
curr = curr->next;
}
return i;
}
void addAtTail(struct node* head)
{
int length = listLength(head);
addAtTail(head, length);
}
void addAtTail(struct node* head, int n)
{
struct node* newNode = new node;
struct node* curr = new node;
newNode->next = NULL;
newNode->data = n;
curr= head;
while(curr->next!=NULL)
{
curr = curr->next;
}
curr->next = newNode;
}
int main()
{
//changeToNull(&head);
addAtTail(head);
printList(head);
return 0;
}
addToTail(head*){int n = length(head*); addtoTail(head*, n)}
此处 head*
是一个类型不是来自参数参数。您需要做:
addToTail(node* head){int n = length(head); addtoTail(head, n)}
笔记:您没有length(node*)
的定义,您定义了:
listLength(struct node*);
因此您应该使用int n = listLength(head)
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 操作员超载;必须是二进制操作员(具有3个参数)
- C bool表达式作为函数参数调用错误的超载
- 是否可以确定函数的参数是否已签名或无符号,以实现可能性超载函数
- 如何将基类方法超载作为参数传递到C 中的模板类
- 编译器如何在超载中区分浮动和双重参数
- 如何使用模板函数参数编写包装函数,该功能可以采用超载的成员函数
- 将超载函数指针作为参数传递到过载模板功能
- C 超载相等运算符.我应该写自己的功能以接受通过参考或值传递的参数
- rvalue参数无法解析功能超载
- C - 编译器如何以参数为参数的过载函数在超载函数之间决定
- 如何以相同的方法在子类中超载的相同方法来实现工作变量参数方法
- 功能超载以NULLPTR作为参数分辨率
- 确定是否有针对特定类型的参数定义的超载函数
- 编译器是否也将所有参数依赖类型实例化,即使超载函数的参数计数不匹配
- 超载函数调用功能具有相同名称,但参数不同