| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1305 人关注过本帖
标题:复数运算,就显示>>没有与这些操作数匹配的“>>"的运算符
只看楼主 加入收藏
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
复数运算,就显示>>没有与这些操作数匹配的“>>"的运算符
头文件
#ifndef COMPLEX_H_
#define COMPLEX_H_
#include<iostream>
namespace COMPLEX
{
    class Complex
    {
    private:
        double real;
        double imaginary;
    public:
        Complex();
        Complex(double a_real, double b_imaginary);
        ~Complex();
        Complex operator+(const Complex& a)const;
        Complex operator-(const Complex& a)const;
        Complex operator*(const Complex& a)const;
        Complex operator*(double m)const;
        Complex operator-()const;
        friend Complex operator*(double m, const Complex& a);
        friend std::ostream& operator<<(std::ostream& os, const Complex& a);
        friend std::istream& operator>>(std::istream& os, const Complex& a);
    };
}
#endif
源代码1
#include"stack.h"
#include<iostream>
namespace COMPLEX
{
    Complex::Complex()
    {
        real = imaginary = 0;
    }
    Complex::Complex(double a_real, double b_imaginary)
    {
        real = a_real;
        imaginary = b_imaginary;
    }
    Complex::~Complex()
    {
        
    }
    Complex Complex::operator+(const Complex& a)const
    {
        Complex temp;
        temp.real = real + a.real;
        temp.imaginary = imaginary + a.imaginary;
        return temp;
    }
    Complex Complex::operator-(const Complex& a)const
    {
        Complex temp;
        temp.real = real - a.real;
        temp.imaginary = imaginary - a.imaginary;
        return temp;
    }
    Complex Complex::operator*(const Complex& a)const
    {
        Complex temp;
        temp.real = real * a.real- imaginary * a.imaginary;
        temp.imaginary =real*a.imaginary+imaginary*a.real;
        return temp;
    }
    Complex Complex:: operator-()const
    {
        return Complex(real, -imaginary);
    }
    Complex Complex::operator*(double m)const
    {
        Complex temp;
        temp.real = real * m;
        temp.imaginary = imaginary * m;
        return temp;
    }
    Complex operator*(double m, const Complex& a)
    {
        return a * m;
    }
    std::ostream& operator<<(std::ostream& os, const Complex& a)
    {
        os << a.real<<","<<a.imaginary<<"i"<<std::endl;
        return os;
    }
    std::istream& operator>>(std::istream& is, const Complex& a)
    {
        std::cout << "real:";
        if (!(is >> a.real))
            return is;
        std::cout << "imaginary:";
        is >> a.imaginary;
        return is;
    }
}
源代码2
#include<iostream>
#include"stack.h"
int main()
{
    COMPLEX::Complex a(3.0, 4.0);
    COMPLEX::Complex c;
    std::cout << "Enter a complex number(q to quit):\n";
    while (std::cin >> c)
    {
        std::cout << "c is " << c << '\n';
        std::cout << "complex conjugate is" << -c << '\n';
        std::cout << "a is " << a << '\n';
        std::cout << "a+c is " << a + c << '\n';
        std::cout << "a-c is " << a - c << '\n';
        std::cout << "a*c is " << a * c << '\n';
        std::cout << "2*c is " << 2 * c << '\n';
        std::cout << "Enter a complex number(q to quit):\n";
    }
    std::cout << "Done!\n";
    return 0;
}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: COMPLEX real temp std const 
2021-05-31 21:37
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:20 
friend std::istream& operator>>(std::istream& os, const Complex& a);
改为
friend std::istream& operator>>(std::istream& os, Complex& a);

std::istream& operator>>(std::istream& is, const Complex& a)
改为
std::istream& operator>>(std::istream& is, Complex& a)
2021-06-01 08:41
快速回复:复数运算,就显示>>没有与这些操作数匹配的“>>"的运算符 ...
数据加载中...
 
   



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

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