免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2842 | 回复: 1
打印 上一主题 下一主题

[C++] C++头文件报错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-03 17:35 |只看该作者 |倒序浏览
学习C++的时候,想看一下操作符重载,找了网上的一段代码:
  1. //complex.h
  2. #include <iostream>

  3. using namespace std;
  4. class Complex//声明一个复数类
  5. {
  6.    public:
  7.    Complex(Complex &a);//拷贝构造函数
  8.    Complex(double r=0,double i=0);
  9.    void display();//输出复数的值
  10.    void set(Complex &a);
  11.    Complex plus(Complex a);//复数的加法
  12.    Complex minus(Complex a); //复数的减法
  13.    Complex plus(double r); //复数与实数相加
  14.    Complex minus(double r); //复数与实数相减
  15.    private:
  16.    double real;//复数实部
  17.    double img;//复数虚部
  18. };
  19. Complex::Complex(Complex &a)
  20. {
  21.    real=a.real;
  22.    img=a.img;
  23. }
  24. Complex::Complex(double r,double i)
  25. {
  26.    real=r;
  27.    img=i;
  28. }
  29. void Complex::display()
  30. {
  31.    cout <<real <<(img>=0?"+":"") <<img <<"i";//适合显示1-3i等虚部为负值的复数
  32. }
  33. void Complex::set(Complex &a)
  34. {
  35.    real=a.real;
  36.    img=a.img;
  37. }
  38. Complex Complex::plus(Complex a)
  39. {
  40.    Complex temp(a.real+real,a.img+img);
  41.    return temp;
  42. }
  43. Complex Complex::minus(Complex a)
  44. {
  45.    Complex temp(real-a.real,img-a.img);
  46.    return temp;
  47. }
  48. Complex Complex::plus(double r)
  49. {
  50.    Complex temp(real+r,img);
  51.    return temp;
  52. }
  53. Complex Complex::minus(double r)
  54. {
  55.    Complex temp(real-r,img);
  56.    return temp;
  57. }
复制代码
然后是cpp文件:
  1. #include "complex.h"
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.    Complex a(3,2),b(5,4),temp;
  7.    temp.set(a.plus(b));//temp=a+b
  8.    temp.display();
  9.    cout <<endl;
  10.    temp.set(a.minus(b));//temp=a-b
  11.    temp.display();
  12.    cout <<endl;
  13.    return 0;
  14. }
复制代码
怎么都在ubuntu下的GCC编译不过去,主要是如下错误:
g++ -Wall -c "complex.cpp" (in directory: /home/oliver/course/c++ for c programmer/code)
complex.cpp: In function ‘int main()’:
complex.cpp:7:22: error: no matching function for call to ‘Complex::set(Complex)’
complex.cpp:7:22: note: candidate is:
In file included from complex.cpp:1:0:
complex.h:33:6: note: void Complex::set(Complex&)
complex.h:33:6: note:   no known conversion for argument 1 from ‘Complex’ to ‘Complex&’
complex.cpp:10:23: error: no matching function for call to ‘Complex::set(Complex)’
complex.cpp:10:23: note: candidate is:
In file included from complex.cpp:1:0:
complex.h:33:6: note: void Complex::set(Complex&)
complex.h:33:6: note:   no known conversion for argument 1 from ‘Complex’ to ‘Complex&’
Compilation failed.

尝试编译.h文件,得到的报错如下:
gcc -Wall -c "complex.h" (in directory: /home/oliver/course/c++ for c programmer/code)
complex.h:1:20: fatal error: iostream: No such file or directory
compilation terminated.
Compilation failed.


我把.h文件改成.cpp文件,能够编译通过。实在搞不懂什么情况了。求教

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
2 [报告]
发表于 2013-11-03 19:19 |只看该作者
回复 1# kingfighters

把函数原型void Complex::set(Complex &a)修改为void Complex::set(const Complex &a),
你调用的代码:temp.set(a.plus(b));//temp=a+b,你的注释也说明了参数是个临时对象,而临时对象是无法转换为引用的,只能常(const)引用。
至于你编译头文件有总是是因为你编译用的是gcc而不是g++,这两个命令默认的编译环境不一样。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP