ttowill 2023-04-02 09:18 采纳率: 71.4%
浏览 11
已结题

C++的类做完后出现许多报错,求解决。

我用C++做了一个复数类Complex,结果编译时报一大堆错,代码如下:

#include <iostream>
#include <cmath>
#include <stdio.h>
#pragma once
using namespace std;
class Complex{
    private:
    double x;
    double i;
    string doubleToString(double num){
        char ch[20];
        sprintf(ch,"%2lf",num);
        string result(ch);
        return result;
    }
    public:
    // define constructor
    Complex():x(0),i(0){}
    // normal constructor
    Complex(double num):x(num){}
    Complex(double num,double y):x(num),i(y){}
    // copy constructor
    Complex(const Complex &obj):x(obj.x),i(obj.i){}
    // copy operator
    Complex operator=(const Complex &obj){
        x=obj.x;
        i=obj.i;
    }
    // make function
    static Complex make(double num){
        Complex obj(num);
        return obj;
    }
    static Complex make(double num,double y){
        Complex obj(num,y);
        return obj;
    }
    // get function
    double num(){
        return x;
    }
    double i(){
        return (double)i;
    }
    // set function
    Complex setAs_DA(double dis,double ang){
        x=cos(ang)*dis;
        i=sin(ang)*dis;
        return *this;
    }
    // math function
    double distance(){
        return sqrt((pow(x,2))+(pow(i,2)));
    }
    double angle(){
        return atan(i/x);
    }
    // operator
    Complex operator+=(double num){
        x+=num;
        return *this;
    }
    Complex operator+=(const Complex &obj){
        x+=obj.x;
        i+=obj.i;
        return *this;
    }
    Complex operator-=(double num){
        x-=num;
        return *this;
    }
    Complex operator-=(const Complex &obj){
        x-=obj.x;
        i-=obj.i;
        return *this;
    }
    Complex operator+(double num){
        Complex temp=*this;
        temp+=num;
        return temp;
    }
    Complex operator+(const Complex &obj){
        Complex temp=*this;
        temp+=obj;
        return temp;
    }
    Complex operator-(double num){
        Complex temp=*this;
        temp-=num;
        return temp;
    }
    Complex operator-(const Complex &obj){
        Complex temp=*this;
        temp-=obj;
        return temp;
    }
    Complex operator*=(Complex obj){
        double dis=distance();
        double ang=angle();
        dis*=obj.distance();
        ang+=obj.angle();
        setAs_DA(dis,ang);
        return *this;
    }
    Complex operator*=(double num){
        double dis=distance();
        double ang=angle();
        dis*=num;
        setAs_DA(dis,ang);
        return *this;
    }
    Complex operator*(Complex obj){
        Complex temp=*this;
        temp*=obj;
        return temp;
    }
    Complex operator*(double num){
        Complex temp=*this;
        temp*=num;
        return temp;
    }
    bool operator==(const Complex &obj){
        return (x==obj.x&&i==obj.i);
    }
    bool operator!=(const Complex &obj){
        return !(this->operator==(obj));
    }
    bool operator>(const Complex &obj){
        return x>obj.x;
    }
    bool operator<(const Complex &obj){
        return x<obj.x;
    }
    // other function
    string toNIString(){
        string num=doubleToString(x);
        string sign="+";
        string y=doubleToString(i);
        if(i<0) sign="";
        return (num+sign+"i");
    }
};

而且大多数都是Complex::i的问题,谁能帮我解决呀

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-04-02 12:33
    关注
    • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/255378
    • 除此之外, 这篇博客: C++通过类实现单链表(保姆级教程,附源码,可实现多种操作)中的 3.1按位查找结点(返回第i个结点的结点指针) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    • 上面实现了一些基操,那么下面再来实现一些有趣的方法,按位查找首当其冲嗷!这个操作很简单,通过上面的方法实现,相信你已经熟练掌握了如何遍历一个链表,那只要在遍历的时候一遍遍历一遍数数,数到i的时候就直接return p;

      在开写之前还是先考虑一手异常情况,当i<=0的时候是死都找不到的,直接回NULL(i=0的时候带头结点的可以找到头结点但这没意义所以一并当作异常处理掉!)

      于是我们得到了如下代码:

      //检查无误!
      node* locate(int i)
      {
      	
      	//i<=0回空指针
      	if (i <= 0)
      	{
      		return NULL;
      	}
      
      	node* current = head;//当前所在结点指针
      	int k = (with_head) ? 0 : 1;//计数
      	while (current != NULL && k < i)
      	{
      		current = current->next;
      		k++;
      	}
      	return current;
      
      }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月2日
  • 已采纳回答 8月25日
  • 创建了问题 4月2日

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数