| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 939 人关注过本帖
标题:求错误讲解,最好给个答案
只看楼主 加入收藏
APTX
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2015-3-29
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:5 
求错误讲解,最好给个答案
#include<iostream>
using namespace std;
class complex
{ public:
  int real,image;
  complex(int a=0,int b=0)
 { real=a;
   image=b;
   cout<<real<<"+"<<image<<"i";
   
 }
  ~complex();
  friend complex & operator*(complex &x,complex &y);
  complex & operator/(complex &n);
  friend ostream & operator<<(ostream &outpyt,complex &v);
};

 complex & complex::operator/(complex &n)
{ complex t;
  t.real=(real*n.real+image*n.image)/(n.real*n.real+n.image*n.image);
  t.image=(image*n.real-real*n.image)/(n.real*n.real+n.image*n.image);
  return t;
}
  complex & operator*(complex &x,complex &y)
  { complex z;
    z.real=(x.real*y.real)-(x.image*y.image);
    z.image=(x.real*y.image)+(x.image*y.real);
    return z;
  }
  ostream & operator<<(ostream &output,complex &v)
  { output<<v<<' ';
    return output;
  }
  void main()
  { complex x(1,2),y(1,3),z(),c();
    z()=operator*(x,y);
    c()=x.operator/(y);
    cout<<x<<' '<<y<<' '<<z<<' '<<c<<endl;
  }

1>c:\users\tel\documents\visual studio 2012\projects\consoleapplication15\consoleapplication15\源.cpp(22): warning C4172: 返回局部变量或临时变量的地址
1>c:\users\tel\documents\visual studio 2012\projects\consoleapplication15\consoleapplication15\源.cpp(28): warning C4172: 返回局部变量或临时变量的地址
1>c:\users\tel\documents\visual studio 2012\projects\consoleapplication15\consoleapplication15\源.cpp(33): warning C4717: “operator<<”: 如递归所有控件路径,函数将导致运行时堆栈溢出
1>源.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall complex::~complex(void)" (??1complex@@QAE@XZ),该符号在函数 "class complex & __cdecl operator*(class complex &,class complex &)" (??D@YAAAVcomplex@@AAV0@0@Z) 中被引用
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppCommon.targets(611,5): error MSB6006: “link.exe”已退出,代码为 1120。
1>源.obj : error LNK2019: 无法解析的外部符号 "class complex __cdecl z(void)" (?z@@YA?AVcomplex@@XZ),该符号在函数 _main 中被引用
1>源.obj : error LNK2019: 无法解析的外部符号 "class complex __cdecl c(void)" (?c@@YA?AVcomplex@@XZ),该符号在函数 _main 中被引用
1>c:\users\tel\documents\visual studio 2012\Projects\ConsoleApplication15\Debug\ConsoleApplication15.exe : fatal error LNK1120: 3 个无法解析的外部命令

[ 本帖最后由 APTX 于 2015-3-30 13:23 编辑 ]
搜索更多相关主题的帖子: complex include public friend 最好 
2015-03-29 22:50
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
哪里错了,你告诉别人。
比如编译失败,贴出编译器给出的错误信息
比如运行失败,贴出你的输入、实际输出、期待输出
……
2015-03-30 08:24
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:15 
程序代码:
#include <iostream>
#include <stdexcept>

class complex
{
    friend std::ostream& operator<<( std::ostream &out, const complex& v );
    friend complex operator*( const complex& a, const complex& b );

public:
    complex( int real=0,int image=0 ) : real_(real), image_(image)
    {
    }

    complex operator/( const complex& b ); // 为什么你要将 除 设计得与 乘 不一样?

private:
    int real_, image_;
};

complex complex::operator/( const complex& b )
{
    if( b.real_==0 && b.image_==0 )
        throw std::overflow_error( "devided by zero" );

    int real = (real_*b.real_+image_*b.image_)/(b.real_*b.real_+b.image_*b.image_);
    int image = (image_*b.real_-real_*b.image_)/(b.real_*b.real_+b.image_*b.image_);
    return complex(real,image);
}

complex operator*( const complex& a, const complex& b )
{
    int real = (a.real_*b.real_)-(a.image_*b.image_);
    int image = (a.real_*b.image_)+(a.image_*b.real_);
    return complex(real,image);
}
std::ostream& operator<<( std::ostream &out, const complex& v )
{
    return out << v.real_ << '+' << v.image_ << 'i';
}

using namespace std;

int main( void )
{
    complex a(1,2), b(1,3);
    complex c = a * b;
    complex d = a / b;
    cout << c << '\n'
         << d << endl;

    return 0;
}
2015-04-01 09:31
王翔
Rank: 2
等 级:论坛游民
帖 子:13
专家分:12
注 册:2015-2-1
收藏
得分:5 
回复 楼主 APTX
1   析构函数未定义;
2   <<运算符重载定义出错;不能直接操作类(output<<v是错的),重载的目的就是为了能直接操作;
3   创建对象是,不初始化则不用( ),使用是也不用( );
PS: 数据应为私有成员吧
2015-04-01 13:04
APTX
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2015-3-29
收藏
得分:0 
回复 3楼 rjsp
谢谢~
#include <stdexcept>这个头文件是什么作用?
设计得不一样是我想用成员函数和友元函数
return complex(real,image)可以直接return complex?

[ 本帖最后由 APTX 于 2015-4-3 19:23 编辑 ]
2015-04-03 19:16
APTX
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2015-3-29
收藏
得分:0 
回复 4楼 王翔
谢谢
2015-04-03 19:16
快速回复:求错误讲解,最好给个答案
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012705 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved