我收到以下错误:
..\src\test.cpp:17:20: error: expected unqualified-id before 'try'
friend int try(Complex);
请帮助并告诉我这段代码为什么会产生错误:
#include <iostream>
using namespace std;
class Complex
{
private:
int a, b;
public:
showData()
{
cout<<"\na= "<<a<<" b= "<<b;
}
Complex(int x, int y)
{ //constructer
a = x;
b = y;
}
friend int try(Complex);
//friend function
};
int try(Complex c)
{
cout<<"You areworking in try now";
cout<<"\noutput from friend fun : "<<c.a<<" "<<c.b;
}
int main()
{
Complex c1(3, 4);
try(c1);
return 0;
}
答案 0 :(得分:1)
首先,我们将问题简化为一个重复问题的Minimal Complete and Verifiable example:
int try;
因为try
is a reserved word,所以不需要太多代码。如果没有编译器expecting a try/
catch` block,则不能在程序中使用try
。
解决方案:请勿使用try
作为标识符。而是使用try_func
或描述正在尝试的内容的东西。
附加说明:showData()
需要返回类型。很有可能应该是void showData()