"sort3"未在此范围内声明
'sort3'was not declared in this scope
如何更正代码?我不明白sort3 was not declared in this scope
的意思。
#include <iostream>
using namespace std;
int main ( )
{
cout<<" Enter first value. "<<endl;
int a;
cin>> a;
cout<<"Enter second value. "<<endl;
int b;
cin>>b;
cout<<"Enter third value. "<<endl;
int c;
cin>>c;
sort3(a,b,c);
cout << a << " " << b << " " << c << endl;
return 0;
}
void sort3(int& a, int& b, int&c )
{
int temp;
if (a < b )
{
temp = a;
a = b;
b = temp;
}
else if (a < c )
{
temp = a;
a = c;
c = temp;
}
else if ( b < c )
{
temp = b;
b = c;
c = temp;
}
}
您可以在main之前"声明"sort3
函数:
void sort3(int& a, int& b, int&c );
int main ()
{
...
另一种选择是将sort3
的定义移到main之前。
在C++中,您至少需要一个函数的声明才能使用它。
您需要将声明放在第一次使用函数之前:
#include <iostream>
using namespace std;
void sort3(int& a, int& b, int&c ); //declaration
int main ( )
{
声明
void sort3(int&, int&, int& );
在main
起作用之前,它将起作用。
将声明放在函数之前:
void sort3(int& a, int& b, int&c )
int main()
或者试试这个:
#include <iostream>
using namespace std;
void sort3(int& a, int& b, int&c )
{
int temp;
if (a < b )
{
temp = a;
a = b;
b = temp;
}
else if (a < c )
{
temp = a;
a = c;
c = temp;
}
else if ( b < c )
{
temp = b;
b = c;
c = temp;
}
}
int main ( )
{
cout<<" Enter first value. "<<endl;
int a;
cin>> a;
cout<<"Enter second value. "<<endl;
int b;
cin>>b;
cout<<"Enter third value. "<<endl;
int c;
cin>>c;
sort3(a,b,c);
cout << a << " " << b << " " << c << endl;
return 0;
}
请注意,函数的声明必须在使用之前。
试试这个
#include <iostream>
using namespace std;
void sort3(int& a, int& b, int&c )
{
int temp;
if (a < b )
{
temp = a;
a = b;
b = temp;
}
else if (a < c )
{
temp = a;
a = c;
c = temp;
}
else if ( b < c )
{
temp = b;
b = c;
c = temp;
}
}
int main ( )
{
cout<<" Enter first value. "<<endl;
int a;
cin>> a;
cout<<"Enter second value. "<<endl;
int b;
cin>>b;
cout<<"Enter third value. "<<endl;
int c;
cin>>c;
sort3(a,b,c);
cout << a << " " << b << " " << c << endl;
return 0;
}
或者,您可以在顶部的"using namespace std;"行之后定义函数
void sort3(int &, int &, int &);
由于编译器从上到下解析文件,因此会出现此错误。因此编译器会遇到您对sort3
的调用,但它从未被声明。
为了解决这个问题,您可以使用forward声明,或者将整个sort3
-函数移到main()
前面。
具有前向声明的解决方案:
#include <iostream>
using namespace std;
void sort3(int& a, int& b, int&c );
int main ( )
{
... code ...
}
void sort3(int& a, int& b, int&c )
{
... code ...
}
没有正向声明,但sort3
移动到main()
:前面
#include <iostream>
using namespace std;
void sort3(int& a, int& b, int&c )
{
... code ...
}
int main ( )
{
... code ...
}
相关文章:
- 错误:未在此范围内声明'reverse'
- 错误:"imread"未在此范围内声明
- 未在此范围内声明错误 'xy'
- Socklen_t未在此范围内声明
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- "Main"已在当前范围内声明
- c++ 变量在宏的扩展中没有在这个范围内声明
- C++ 在编译过程中 strtok 函数 Eclipse 说没有在范围内声明?
- 'num1'未在此范围内声明
- 如何找到在本地范围内声明的变量的地址?
- NT状态未在此范围内声明
- 变量未在此范围内声明 数组线性搜索
- 包含文件中的 Typdef "未在此范围内声明"
- 'ClassName'和'ClassInstance'未在此范围内声明
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 代码作为 C 文件工作,但不作为C++文件,错误:'__builtin_types_compatible_p'未在此范围内声明
- 如何修复错误:"vreinterpretq_u32_f64"未在此范围内声明 - 在Android上使用Eigen构建
- 如何修复C++中的"arrStud未在此范围内声明"错误
- 错误:"SHGetKnownFolderPath"未在此范围内声明
- 错误:'Sprite'未在此范围内声明