【问题标题】:too few arguments to function C++ [closed]函数 C++ 的参数太少 [关闭]
【发布时间】:2015-02-09 16:00:49
【问题描述】:
#include <iostream>
#include <iomanip>
#include <cstdlib>

// function prototypes
void intOutput();
void floatingPointOutput();
void intMathOperations(int rows, int b, int width);
void writeHeaderLine(int width);
void writeMathLine(int a, int b, int width);

using namespace std;

int main()
{
cout << "\nProject 1: Math and Functions";
cout << "\n";
cout << "\n";
cout << "\nProject 1 Start.";
cout << "\nZack Cunningham";
cout << "\n";
cout << "\nInteger Output Demo:";
cout << "\n";

intOutput();
floatingPointOutput();
intMathOperations();
writeHeaderLine();
writeMathLine();

cout << "\n";
cout << "\nProject 1 End.";
cout << "\n";

const int FIELD_WIDTH = 10;
intMathOperations(12, 5, FIELD_WIDTH);

return EXIT_SUCCESS;
}

void intMathOperations(int rows, int b, int width){
cout << "\n";
cout << "\nInteger Math Operations Demo:";
cout << "\n";
writeHeaderLine(width);
cout << "\n";
for (int a = 0; a < rows; ++a){writeMathLine(a, b, width);
}
}


void writeHeaderLine(int width){
cout << "\n";
cout << setw(width) << "a";
cout << setw(width) << "b";
cout << setw(width) << "a * b";
cout << setw(width) << "a / b";
cout << setw(width)<< "a % b";
}

void writeMathLine(int width){
int a;
cout << setw(width) << a;
int b;
int rows;
for (int a = 0; a < rows; ++a){writeMathLine(a, b, width);
}
}

void floatingPointOutput(){
double a = 2000;
double b = 3;
double c = a / b;
cout << "\n" << a << " / " << b << " = ";
cout << "\n" << c;

cout << setprecision(10);
cout << "\n" << setw(20) << c;
cout << scientific; // scientific notation
cout << "\n" << setw(20) << c;
cout << fixed; // standard decimal notation
cout << "\n" << setw(20)<< c;
cout << left; // left justify
cout << "\n" << setw(20) << c;
cout << right;

// right justify (default)
cout << "\n" << setw(20) << c;
cout << setprecision(6); // return to default
cout << "\n" << setw(20) << c;
cout << "\n";
}

// function calls
void intOutput(){
cout << "\nInteger Output Demo:";
cout << "\n";
int a = 12;
int b = 12345678;
cout << "\n....5...10...15...20"; // spacing info
cout << "\n";
cout << "\n" << setw(20) << a;
cout << "\n" << setw(20) << b;
cout << "\n";
cout << "\n" << setw(4) << a;
cout << "\n" << setw(4) << b;
cout << left; // left justified
cout << "\n";
cout << "\n" << setw(20) << a;
cout << "\n" << setw(20) << b;
cout << right; // right (default) justified
cout << "\n";
}

这当然是我的代码,它给了我 3 个错误,说我的最后 3 个函数的参数太少。任何帮助将不胜感激!在我看来,所有论点似乎都是有效的,但无论如何我只是一个初学者。

【问题讨论】:

  • 当您在此处发布代码时,选项卡无法正常工作。因此,请确保为每个缩进使用一致数量的空格。它确实有助于提高可读性。 另外writeMathLine 声明和实现签名不同。很简单。
  • 另外,Zack 的老师是否知道其他人正在为他做(阅读:试图做)他的作业?
  • 参数太少我认为错误信息不言自明
  • @Novocaine:实际上,不,他是对的……最好转换为 SO 的空格缩进。
  • @Novocaine:SO 不会自动这样做。

标签: c++


【解决方案1】:

在这里你声明一个需要三个参数的函数:

void intMathOperations(int rows, int b, int width);

在这里你完全没有参数地调用它:

intMathOperations();

编译器告诉你这是不正确的。 writeHeaderLinewriteMathLine 相同。

【讨论】:

  • 注意:他还定义它与声明中不同数量的参数。
  • 嗯,不是那个,而是void writeMathLine(int a, int b, int width); / void writeMathLine(int width) { }
【解决方案2】:

你在调用这些函数时不带参数

void intMathOperations(int rows, int b, int width);
void writeHeaderLine(int width);
void writeMathLine(int a, int b, int width);

【讨论】:

  • 我在想这可能是原因,我在寻求帮助,而不是做我的项目。但是谢谢,感谢您的帮助和关注;)
猜你喜欢
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-08-18
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 2017-04-22
相关资源
最近更新 更多