illegal else without matching if可我找不出错误

leveldd 2012-06-08 02:44:55
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;};
void set(){cin>>real>>imag;}
bool operator==(Complex&c2);
void display();
double real;
double imag;
};
bool Complex::operator==(Complex&c2)
{
Complex c;
if(c.real==c2.real&&c.imag==c2.imag) return true;
else return false;
}
void Complex::display()
{
if(real==0)
{
if(imag!=0) cout<<imag<<"i,";
else cout<<"0,";
};
else
{
if(imag>0) cout<<real<<"+"<<imag<<"i,";
else if(imag<0) cout<<real<<imag<<"i,";
else cout<<real<<",";
};
}
int main()
{
Complex c1,c2;
c1.set();
c2.set();
while (c1.real!=0||c1.imag!=0||c2.real!=0||c2.imag!=0)
{
cout<<"a=";
c1.display();
cout<<"b=";
c2.display();
if (c1==c2) cout<<"a等于b"<<endl;
else
{
cout<<"a不等于b"<<endl;
};
c1.set();
c2.set();
}
return 0;
}


一直显示error C2181: illegal else without matching if

可我检查了好多遍也没检查出错误。。。到底是哪里出问题了。。不是都配对的好好的嘛~~~欲哭无泪 求救哇~~
...全文
6167 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
leveldd 2012-06-08
  • 打赏
  • 举报
回复
多谢楼上各位~问题解决啦~
赵4老师 2012-06-08
  • 打赏
  • 举报
回复
偶遇到类似问题都是用
“每次用/*...*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。”
的方法解决的。
sunxc123 2012-06-08
  • 打赏
  • 举报
回复
貌似你的重载==不对吧,应该比较的是形参和调用该函数的对象,你在函数里面新建一个对象,失去原来比较的意义了
fish_in_wind 2012-06-08
  • 打赏
  • 举报
回复
楼主有大括号,分号就不带了
W170532934 2012-06-08
  • 打赏
  • 举报
回复
楼主最好养成习惯将if与else搞成{}配对的形式
xunilcyl 2012-06-08
  • 打赏
  • 举报
回复
void Complex::display()函数里面的第一个if语句块后面多了一个分号
更正后
void Complex::display()
{
if(real==0)
{
if(imag!=0) cout<<imag<<"i,";
else cout<<"0,";
}
else
{
if(imag>0) cout<<real<<"+"<<imag<<"i,";
else if(imag<0) cout<<real<<imag<<"i,";
else cout<<real<<",";
};
}
cryingbee 2012-06-08
  • 打赏
  • 举报
回复
学一下if的语法吧

if(real==0)
{
if(imag!=0) cout<<imag<<"i,";
else cout<<"0,";
}; 如果后面有if,有了大括号了,分号就要去掉,
You are visitor as of October 17, 1996.The Art of Assembly Language ProgrammingForward Why Would Anyone Learn This Stuff?1 What's Wrong With Assembly Language2 What's Right With Assembly Language?3 Organization of This Text and Pedagogical Concerns4 Obtaining Program Source Listings and Other Materials in This TextSection One: Machine OrganizationArt of Assembly Language: Chapter OneChapter One - Data Representation1.0 - Chapter Overview1.1 - Numbering Systems1.1.1 - A Review of the Decimal System1.1.2 - The Binary Numbering System1.1.3 - Binary Formats1.2 - Data Organization1.2.1 - Bits1.2.2 - Nibbles1.2.3 - Bytes1.2.4 - Words1.2.5 - Double Words1.3 - The Hexadecimal Numbering System1.4 - Arithmetic Operations on Binary and Hexadecimal Numbers1.5 - Logical Operations on Bits1.6 - Logical Operations on Binary Numbers and Bit Strings1.7 - Signed and Unsigned Numbers1.8 - Sign and Zero Extension1.9 - Shifts and Rotates1.10 - Bit Fields and Packed Data1.11 - The ASCII Character Set1.12 Summary1.13 Laboratory Exercises1.13.1 Installing the Software1.13.2 Data Conversion Exercises1.13.3 Logical Operations Exercises1.13.4 Sign and Zero Extension Exercises1.13.5 Packed Data Exercises1.14 Questions1.15 Programming ProjectsChapter Two - Boolean Algebra2.0 - Chapter Overview2.1 - Boolean Algebra2.2 - Boolean Functions and Truth Tables2.3 - Algebraic Manipulation of Boolean Expressions2.4 - Canonical Forms2.5 - Simplification of Boolean Functions2.6 - What Does This Have To Do With Computers, Anyway?2.6.1 - Correspondence Between Electronic Circuits and Boolean Functions2.6.2 - Combinatorial Circuits2.6.3 - Sequential and Clocked Logic2.7 - Okay, What Does It Have To Do With Programming, Then?2.8 - Generic Boolean Functions2.9 Laboratory Exercises<

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧