函数中的参数
Arguments in function
我是一名新的c ++学生,我正在尝试为孩子们制作一个学习基本数学的基本程序。 这是一项正在进行的工作,因此某些代码尚未完成。 我正在尝试编译到目前为止我所做的工作,但收到一个错误,上面写着:47 59 [错误] 新声明"void add(int, int, std::string, char)" 和 18 5[错误] 歧义旧声明 'int add(int, int, std::string, char)'
#include <iostream>
#include <iomanip>
#include <time.h>
#include <string>
#include <cstdlib>
#define over2 "tt"
#define over3 "ttt"
#define over4 "tttt"
#define down5 "nnnnn"
#define down8 "nnnnnnnn"
#define down10 "nnnnnnnnnn"
#define down12 "nnnnnnnnnnnn"
using namespace std;
int add(int,int,string,char);
int sub();
int multi();
int div();
int main(int argc, char** argv)
{
int num1, num2;
unsigned seed = time(0);
srand(seed);
num1 = 1 +rand() % 4;
num2 = 1 +rand() % 4;
// correctAnswer = (num1+num2);
cout << "This program is a tool for young kids to learn basic math.n";
cout << "Just press any key and the learning will commence!n";
system("CLS");
add(num1, num2, "Addition", '+');
// addResults(num1+num2);
return 0;
}
void add(int num1, int num2, string operation, char symbol)
{
cout << down8;
cout << over3 << operation << endl;
cout << over3 << "________n";
cout << over3 << setw(3) << num1 << endl;
cout << over3 << symbol << endl;
cout << over3 << setw(3) << num2 << endl;
cout << over3 << "________n";
}
add
方法的返回类型在声明 ( int
) 和定义 ( void
) 上有所不同。我认为定义的类型应该更改为int
并使其返回结果。
我不
完全确定,但我认为问题出在开始。定义 add 函数时,将其编写为 int add(int,int,string,char);
但是,您没有像实际编写函数时那样正确定义变量。此外,一开始你把它定义为int,但你把它写成void。编译器不知道您要做什么,因为它认为有 2 个 add 函数,一个是 int 类型,另一个是 void 函数,这可能是 18 5[错误] 歧义旧声明"int add(int, int, std::string, char)"
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类