【发布时间】:2018-08-18 16:29:20
【问题描述】:
我想要一个带有可选参数的函数。但是,VC++ 抛出了一个我无法理解的错误。代码很简单如下:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void myFunction(string something, int w);
int main()
{
myFunction("a string");
int a = 1;
}
void myFunction(string something, int w = 5)
{
cout << something << " - " << w << endl;
}
它抱怨:
'myFunction': function does not take 1 arguments
但是,如果我将 myFunction() 移到 main() 之前,它不会出错。到底是怎么回事?请帮忙。谢谢。
【问题讨论】:
-
把默认参数
int w=5放在声明而不是定义中。 -
如果我解决了您的问题,请不要忘记将我的答案标记为已接受。 :)