求助啊...错误在哪

jiafei166 2012-08-26 06:04:35
//complex0.h
#ifndef complex0_h_
#define complex0_h_
class complex{
double x;
double y;
public:
complex() ;
complex(double, double);
~complex() ;
friend complex operator+(complex &, const complex &);//Addtion
complex operator-(const complex &);//Substraction
complex operator*(const complex &);//Multiplication
friend complex operator*(double, const complex &);//Multiplication by double
complex operator~( );//conjugate complex
friend std::istream & operator>>(std::istream &, complex &);
friend std::ostream & operator<<(std::ostream &, const complex &);
}
#endif

//complex0.cpp

#include <iostream>
#include "complex0.h"

using namespace std;

complex::complex( ){x=y=0;}

complex::complex(double a, double b)
{
x=a;
y=b;
}

complex operator+(complex &cp1, const complex& cp2)//Addtion
{
cp1.x+=cp2.x;
cp1.y+=cp2.y;

return cp1;
}

complex complex::operator-(const complex &cp2)//Substraction
{
x-=cp2.x;
y-=cp2.y;

return *this;
}

complex complex::operator*(const complex &cp2)//Multiplication
{
x*=cp2.x;
y*=cp2.y;

return *this;
}

complex operator*(double m, const complex & cp1)//Multiplication by double
{
complex temp;

temp.x=m*cp1.x;
temp.y=m*cp1.y;

return temp;
}

complex complex::operator~( )//conjugate complex
{
x=-x;
y=-y;

return *this;
}

istream & operator>>(istream & is, complex& cp1)
{
cout<<"real: ";
is>>cp1.x;
cout<<"img: ";
is>>cp1.y;

return is;
}

ostream & operator<<(ostream & os, const complex& cp1)
{
os<<"( "<<cp1.x<<" , "<<cp1.y<<"i )";

return os;
}

//complex1.cpp


#include <iostream>
#include <cstring>
#include <string>
#include<limits>
#include <cctype>
#include <fstream>
#include "complex0.h"

using namespace std;

int main( )
{
complex a(3.0, 4.0); // initialize to (3,4i)
complex c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << '\n';
cout << "complex conjugate is " << ~c << '\n';
cout << "a is " << a << '\n';
cout << "a + c is " << a + c << '\n';
cout << "a - c is " << a - c << '\n';
cout << "a * c is " << a * c << '\n';
cout << "2 * c is " << 2 * c << '\n';
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done!\n";

return 0;
}
...全文
84 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zssure 2012-08-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

syntax error : missing ';' before 'using',
应该是#include "complex0.h"这一行出的问题,lz检查一下complex0.h头文件中complex类定义最后是不是少了分号;

class complex
{ ... };
[/Quote]

出现这个错误通常是因为在类的定义后,没有添加分号所引起的。
jiafei166 2012-08-27
  • 打赏
  • 举报
回复
感谢4楼...犯了弱智错误。。。
jiafei166 2012-08-27
  • 打赏
  • 举报
回复
烦恼在于按照语法没发现与提示能对应的错误啊,我可是粘贴的原程序,觉得没有提示的错误啊...

谢谢1楼,只是我的问题在于我的程序哪里有了问题,按照编译的提示怎么修改......
xingfeng2510 2012-08-26
  • 打赏
  • 举报
回复
syntax error : missing ';' before 'using',
应该是#include "complex0.h"这一行出的问题,lz检查一下complex0.h头文件中complex类定义最后是不是少了分号;

class complex
{ ... };
dazhong159 2012-08-26
  • 打赏
  • 举报
回复
你再仔细看下,我给你的代码确实可以正常运行。
jiafei166 2012-08-26
  • 打赏
  • 举报
回复
这里面的析构函数没什么意义,我去掉了
问题一样,编译错误
syntax error : missing ';' before 'using'

没找到错误在哪里
dazhong159 2012-08-26
  • 打赏
  • 举报
回复
析构函数需要定义,你可以在函数体内这样写:~complex(){}
或者在函数体外complex::~complex(){}


下面的代码是我把所有的文件放在一个cpp中,正常运行。
#include <iostream>

using namespace std;
class complex;
complex operator+(complex &, const complex &);
std::istream & operator>>(std::istream &, complex &);
std::ostream & operator<<(std::ostream &, const complex &);

class complex{
double x;
double y;
public:
complex() ;
complex(double, double);
~complex(){}
friend complex operator+(complex &, const complex &);//Addtion
complex operator-(const complex &);//Substraction
complex operator*(const complex &);//Multiplication
friend complex operator*(double, const complex &);//Multiplication by double
complex operator~();//conjugate complex
friend std::istream & operator>>(std::istream &, complex &);
friend std::ostream & operator<<(std::ostream &, const complex &);
};

complex::complex(){x=y=0;}

complex::complex(double a, double b)
{
x=a;
y=b;
}

complex operator+(complex &cp1, const complex& cp2)//Addtion
{
cp1.x+=cp2.x;
cp1.y+=cp2.y;

return cp1;
}

complex complex::operator-(const complex &cp2)//Substraction
{
x-=cp2.x;
y-=cp2.y;

return *this;
}

complex complex::operator*(const complex &cp2)//Multiplication
{
x*=cp2.x;
y*=cp2.y;

return *this;
}

complex operator*(double m, const complex & cp1)//Multiplication by double
{
complex temp;

temp.x=m*cp1.x;
temp.y=m*cp1.y;

return temp;
}

complex complex::operator~()//conjugate complex
{
x=-x;
y=-y;

return *this;
}

istream & operator>>(istream & is, complex& cp1)
{
cout<<"real: ";
is>>cp1.x;
cout<<"img: ";
is>>cp1.y;

return is;
}

ostream & operator<<(ostream & os, const complex& cp1)
{
os<<"( "<<cp1.x<<" , "<<cp1.y<<"i )";

return os;
}


int main( )
{
complex a(3.0, 4.0); // initialize to (3,4i)
complex c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << '\n';
cout << "complex conjugate is " << ~c << '\n';
cout << "a is " << a << '\n';
cout << "a + c is " << a + c << '\n';
cout << "a - c is " << a - c << '\n';
cout << "a * c is " << a * c << '\n';
cout << "2 * c is " << 2 * c << '\n';
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done!\n";

return 0;
}








64,639

社区成员

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

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