0

Sorry for such a not good title. Now please see my detailed question.

Actually, I faced with such an exercise problem: Definite a class CComplex for complex number. Then, definite two objects c1 and c2 in CComplex . Next, use a constructor to initialize c1 and c2. After that, give c1's value to c2.

My code is as followed:

#include<iostream>
using namespace std;

class CComplex
{
public:
    CComplex(int real1,int image1)
    {
        real=real1;
        image=image1;
    }
    CComplex(CComplex &c)
    {
        real=c.real;
        image=c.image;
    }
public:
    void Display(void)
    {
        cout<<real<<"+"<<image<<"i"<<endl;
    }
private:
    int real,image;
};

int main()
{
    CComplex c1(10,20);
    CComplex c2(0,0);
    c1.Display();
    c2.Display();
    CComplex c2(c1);
    c2.Display();
    return 0;
}

It has an error that 'c2' : redefinition.

Then, I changed CComplex c2(c1); into c2(c1);.

At this time, it has an error that error C2064: term does not evaluate to a function

Now, I don't know how to correct it.

PS: I know that using c2=c1 can achieve the goal straightly. But, I really want to know how to correct almost based on my code above. Also, I want to know if there is any better ways to convey a complex number.

asheeshr
  • 4,088
  • 6
  • 31
  • 50
Yingli Yan
  • 69
  • 1
  • 8
  • 1
    you have `CComplex c2(0,0);` and then `CComplex c2(c1);` - this declares a new object with same name and compiler complains. `c2 = c1` is the correct way, otherwise you must remove first declaration. – Zdeslav Vojkovic Mar 13 '13 at 11:47
  • This might help a bit. http://stackoverflow.com/questions/800368/declaring-a-variable-before-initializing-it-in-c And, your exercise problem itself is misdirecting you. You cannot declare an object and construct the object later on. – asheeshr Mar 13 '13 at 11:48
  • I refuse to tell you do define an `operator()` because you should just use `operator=`. – Alex Chamberlain Mar 13 '13 at 11:48
  • 1
    A better way to convey a complex number would be to use `std::complex`. – juanchopanza Mar 13 '13 at 11:51
  • @AlexChamberlain What's the meaning of `operator()`? A function? – Yingli Yan Mar 13 '13 at 11:58
  • It means overloading the `()` operator, so that you can call the object itself as if it were a function: `c1()` - calls whatever code you have defined in `CComplex operator()()` – JBentley Mar 13 '13 at 11:59

3 Answers3

2

I know that using c2=c1 can achieve the goal straightly

It will work, and will do its job marvellously. I therefore don't see what you're trying to achieve with more convoluted (and incorrect) syntax.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

yes, you cannot create c2 object and than use copy constructor on it, because copy constructor creates NEW object, you can use it directly

CComplex c1(10,20);
c1.Display();
CComplex c2(c1);
c2.Display();

to create c2 as a copy of c1 or if you want to assign value to object use something like this:

CComplex c1(10,20);
CComplex c2(0,0);
c1.Display();
c2.Display();
c2=c1;
c2.Display();

also you should provide your own assignnment operator for such purposes

    CComplex& operator=(const CComplex& other){
    if (this != &other) // protect against invalid self-assignment
    {
        // possible operations if needed:
        // 1: allocate new memory and copy the elements
        // 2: deallocate old memory
        // 3: assign the new memory to the object

    }
    // to support chained assignment operators (a=b=c), always return *this
    return *this;
    }
4pie0
  • 29,204
  • 9
  • 82
  • 118
  • This answer is wrong, because he needs to call `Display()` on `c2` *before* he reassigns with the value of `c1`. – JBentley Mar 13 '13 at 11:57
  • Yes, you do now that you've edited the answer! my comment was accurate at the time I posted it. – JBentley Mar 13 '13 at 19:43
0

I'm not sure exactly what your goal is, since you already know the correct answer. However, perhaps this "looks" more like your incorrect version, and is better for you?

c2 = CComplex(c1);
JBentley
  • 6,099
  • 5
  • 37
  • 72
  • Yes! You've got my point. Now, I still can use the function `CComplex(CComplex &c)`. But, I know `c2=c1` is the best one. – Yingli Yan Mar 13 '13 at 12:18