Forum

> > Off Topic > C++ no match for operators
Forums overviewOff Topic overviewLog in to reply

English C++ no match for operators

6 replies
To the start Previous 1 Next To the start

old C++ no match for operators

Bowlinghead
User Off Offline

Quote
Hello community ,

I hope you can help me. For school I have to create a QT program to input a matrix (mArray) with complex numbers (mReal and mImag) and then get the result after the Gaussian Elimination (see CMatrix).
Actually its my first C++ program and first time I am using QT (Creator 4.0.0; QT 5.6.0). Its probably a silly mistake by me. But for now operators are my worst enemy and Im sitting on this problem for hours!

I created operator overloads in my CKomplex class to easily calculate with complex numbers. But it seems that the CMatrix class cant find them. Probably because of the wrong signature

One of my build errors is:
1
2
3
C:\Users\User\Documents\QTProjects\MatrixKomplex\cmatrix.cpp:97: Fehler: no match for 'operator==' (operand types are 'CKomplex' and 'double')
         if (mArray[newRow][row] == 1.) {
                                 ^
Arrow at the first =

All other errors are the same just on different lines and with different operators
Spoiler >


Even though I declared them:
1
2
3
4
5
6
7
8
9
10
11
12
// CKomplex.h
bool operator==(number &rhs); // number is a typedef of double

// ..CKomplex.cpp
bool CKomplex::operator==(number &rhs) {
    if (mReal == rhs) {
        if (!mImag) {
            return true;
        }
    }
    return false;
}

It seems that operator /= and / and == (both CKomplex) do work. Atleast when the compiler didnt just randomly stop which I dont think so.

Ways I tried:
1
2
3
4
5
6
7
8
9
// inline with 2 parameters
bool operator==(CKomplex &lhs, number &rhs) {
        return true;
}

// results in
C:\Users\User\Documents\QTProjects\MatrixKomplex\ckomplex.h:67: Fehler: 'bool CKomplex::operator==(CKomplex&, number&)' must take exactly one argument
     bool operator==(CKomplex &lhs, number &rhs) {
                                               ^
[i]Arrow points on ")"

1
2
3
4
// inline friend
friend bool operator==(CKomplex &lhs, number &rhs) {
	return true;
}
Results in the same error as above.

I cant find help in the internet too.
Note that CMatrix works with the double datatype flawless if you delete the Complex template in the last line of CMatrix.cpp

All classes >
edited 1×, last 23.12.16 07:53:26 pm

old Re: C++ no match for operators

Flacko
User Off Offline

Quote
The value "1." is a literal constant, so it cannot be casted into an lvalue reference as this would mean that you can modify it at run-time (which you can obviously not)
Fix:
CKomplex.h:73
1
bool operator==(const number &rhs);
CKomplex.cpp:259
1
bool CKomplex::operator==(const number &rhs) {
I would also suggest that you add the const qualifier to all arguments in relational method overloads. For more const-ness I further suggest:
1
bool operator==(const number &rhs) const;
This means that you won't modify neither the rhs argument nor the CKomplex instance during equality comparison
edited 1×, last 25.12.16 01:36:33 pm

old Re: C++ no match for operators

Bowlinghead
User Off Offline

Quote
Thanks for your replys. My program does work now!

Yates, I asked this question on stack overflow and they also gave me the tip that I missed many consts. Interestingly, I got a more detailed answer on US than on stackoverflow - even though I had to wait longer.

I also read this to get the basics of const.
I consted my Getter methods and the operators.
If someone is interested:
Spoiler >

old Re: C++ no match for operators

gotya2
GAME BANNED Off Offline

Quote
There's already a complex class in the STL.
http://www.cplusplus.com/reference/complex/

If you persist on using your own class for complex numbers, you can make it a generic class with template <typename T> for bonus points.
And then use:
T mReal, mImag;


If you are using c++11, also have a look at move semantics. Then you can use rvalues in your copy assignment operator. You can leave out the copy assignment operator, by default the compiler will implement this function for you, and copy all the data members.
http://stackoverflow.com/questions/3106110/what-are-move-semantics

Furthermore, I wouldn't include those functions coutEuler and coutKarte. First of all, in some environments you don't even have a std::cout defined. If later on you decide you want to write them to a file instead of std::cout, you have a problem and need to change your class.
Also your QStrKarte doesn't return anything.
Also... by using those Qt headers you force users to include the Qt library. Better keep the class general for reuseability.
You could return std::string.

1
2
3
bool CKomplex::operator<=(number const &rhs) const {
    return (GetRadius() <= rhs ? true : false);
}

is equivalent to
1
2
3
bool CKomplex::operator<=(number const &rhs) const{
   return GetRadius() <= rhs;
}
and saves some typing

old Re: C++ no match for operators

Bowlinghead
User Off Offline

Quote
I know there is a complex class but this program is a task for school - so I have to create my own class (as well as matrix).
But I understand your point of keeping everything as compatible as possible!

Making CKomplex as a class template doesnt make sense in my eyes, because you basicly just need 1 datatype at runtime which are point decimal data types like float, double.
I firstly designed CKomplex as template but the messy code made me think otherwise! CMatrix does have templates though

Since its my first C++ program I played around (not the most professional way), including the Cout functions.
My teacher told me afterwards that its smarter to have QString functions to be compatible with environments without Cout (or even GUI applications).
Why QString and no char array/string? Because my teacher loves~ QT.

What does C++11 mean? Is it the the language version?

Im happy that the operator do work now, I wont change them I already wasted enough time with them...

And actually my focus is more on UML than on coding cuz its much harder to learn (at least on my level)

old Re: C++ no match for operators

GeoB99
Moderator Off Offline

Quote
user Bowlinghead has written
What does C++11 mean? Is it the the language version?

Yes, the "11" number refers to the standard version language being published in 2011. Each new standard version comes with new features and implementations on the language.
To the start Previous 1 Next To the start
Log in to replyOff Topic overviewForums overview