函数中的参数

Arguments in function

本文关键字:参数 函数      更新时间:2023-10-16

我是一名新的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)"