开发者

problem with `==` operator

There is some class wComplex with == operator.

#ifndef WCOMPLEX_H
#define WCOMPLEX_H

#include <stdio.h>

// sample class of complex-like numbers with weird `==` operator
class wComplex{
private:
    double realPart;
    double imagePart;

public:
    wComplex();
    wComplex(double R);
    wComplex(double R, double I);
    bool operator==(wComplex &);
    void print();
};

wComplex::wComplex(){
    realPart  = 0;
    imagePart = 0;
}

wComplex::wComplex(double R){
    realPart  = R;
    imagePart = 0;
}

wComplex::wComplex(double R, double I)
{
    realPart  = R;
    imagePart = I;
}

bool wComplex::operator==(wComplex &El){
    double diff = realPart*realPart + imagePart*imagePart - 
    El.realPart*El.realPart - El.imagePart*El.imagePart;
    return (diff == 0);
}

void wComplex::print(){
    printf("(%g) + (%g)i\n", realPart, imagePart);
}   

#endif

It successfully worked with stuff like that:

wComplex A(1, 2);
wComplex B(2, 4);
wComplex C(2, 1);

(A==C) is true.

There is another class - queue-like. But it should control the new pushed element for equality (in == meaning) of other elements.

#ifndef MYQueue_H
#define MYQueue_H

#include <stdio.h>
#include <queue>


template<typename T>

class myQueue : public std::queue<T>{

public:

    myQueue(){
        printf("new myQueue successfully created\n");
    }

    void push (const T& x){
        myQueue* tmp = new myQueue;
        myQueue* old = new myQueue;
        old = this;
        bool MATCH = false;

        while(!old->empty()){
            T el = old->front();
            if(el == x){
                MATCH = true;
                tmp->push(x);
            }
            else
                tmp->push(el);
            old->pop();
        }
        if(!MATCH)
            tmp->push(x);
        this = *tmp;
        delete tmp;
        delete old;
    }

};

#endif

So, now t开发者_开发技巧here is one problem

myqueue.h: In member function ‘void myQueue<T>::push(const T&) [with T = wComplex]’:
shit.cpp:23:   instantiated from here
myqueue.h:26: error: no match for ‘operator==’ in ‘el == x’
wcomplex.h:36: note: candidates are: bool wComplex::operator==(wComplex&)
myqueue.h:36: error: lvalue required as left operand of assignment
make: *** [compile] Error 1

Actually, I can't understand why no match for ‘operator==’ in ‘el == x’ And what should I do? Any ideas

UPD: and how can I replace this element by tmp? It's something wrong with this = *tmp;


You have a const reference to T in push() but your operator== only accepts non-const references.

bool wComplex::operator==(wComplex &El)

should be

bool wComplex::operator==(wComplex const &El) const

Or, optimally, your operator== should be a free function:

bool operator==(wComplex const & Left, wComplex const & Right) {
}

If you don't want outside access to the member variables of wComplex, you'll need to make the operator a friend function:

class wComplex {
...
    friend bool operator==(wComplex const & Left, wComplex const & Right);
...
};

EDIT: On the updated question:

You cannot assign to this. this is of type T * const - since it wouldn't make sense to modify it. What you're trying to do is to change an external variable which points to the current class, you cannot do that from inside a class member function unless this external variable is passed in as an argument.

I think you need to make a "queue" class which manages "node" class instances - trying to combine a container and the contained elements isn't really a good idea

Also, inheriting standard library containers is rarely a good idea. If you want to use a std::queue then make a member variable.


Change:

bool wComplex::operator==(wComplex &El){

into:

bool wComplex::operator==(const wComplex &El) const {


One tips for the future:

The const keyword, either you put it nowhere, or everywhere you can.

Obviously, everywhere you can is better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜