C++ 我在函数中声明了n,但在我编译代码并尝试运行之后,它给了我n是未识别的错误
以下是简单的语法错误:C++ 我在函数中声明了n,但在我编译代码并尝试运行之后,它给了我n是未识别的错误,c++,visual-c++,C++,Visual C++,以下是简单的语法错误: Error 1 error C2039: 'print_out' : is not a member of '`global namespace'' Error 2 error C2065: 'n' : undeclared identifier Error 3 error C2448: 'print_out' : function-style initializer appears to be a function definitio
Error 1 error C2039: 'print_out' : is not a member of '`global namespace''
Error 2 error C2065: 'n' : undeclared identifier
Error 3 error C2448: 'print_out' : function-style initializer appears to be a function definition
4 IntelliSense: identifier "n" is undefined
5 IntelliSense: identifier "n" is undefined
修复这些问题并编译:您应该像这样声明您的函数:
// void::print_out(int n); //// :: is the scope operator.
vpid print_out(int n);
// int print_out(n) { //// the type must still be there.
void print_out(int n) {
并且应使用与标题相同的返回类型定义:
void print_out(int); //Takes an int argument, and 'void' specifies it has no return value
它应该是int print_outit n,main中的参数和变量是两个独立的实体。这给了我一个错误:不能重载仅由返回类型区分的函数。当我加入for循环时,它必须是一个空白,它要求我这样做。它说打印输出函数应该有类型void;它不返回值。
void print_out(int); //Takes an int argument, and 'void' specifies it has no return value
void print_out(int n) { //is a 'void' type function as previously declared
for (int i = 1; i <= n; i++) //We can declare 'i' in the for loop like this
cout << i << " ";
//Note we're not returning a value: It's a 'void' function.
}