Message Board


Message Board > C/C++ > Clearly a wtf is in order

November 20, 2005, 21:08
Frimkron
Frustrated Megalomaniac
703 posts

Ok I'm having trouble compiling a program. I think its something to do with the "[ ]" operator overloading I'm doing, but I'm not sure. I managed to recreate the problem in a test program I wrote and it seemed to start going wrong when I added that operator overload. Anyway, here's the code:

Code:
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;


class Kitty {
    private:
        string name;
        int values[2];
    public:
        Kitty(const string& nameIn, const int valueInA, const int valueInB);
        ~Kitty();
        Kitty(Kitty& copyMe);
        Kitty& operator= (Kitty& rhs);  
        const Kitty operator+ (Kitty& rhs);
        Kitty& operator+= (Kitty& rhs);
        const int& operator[] (const unsigned int index);
};
Kitty::Kitty(const string& nameIn, const int valueInA, const int valueInB){
    name = nameIn;
    values[0] = valueInA;
    values[1] = valueInB;
    cout << name << " is born with values " 
        << values[0] << " and " << values[1] << "n";
}
Kitty::~Kitty(){
    cout << name << " has died with values " 
        << values[0] << " and " << values[1] << "n";
}
Kitty::Kitty(Kitty& copyMe) {
    values[0] = copyMe[0];
    values[1] = copyMe[1];
    name = "Copy-of-" + copyMe.name;
    cout << "A copy of " << copyMe.name << " is maden";
}
Kitty& Kitty::operator= (Kitty& rhs){
    values[0] = rhs[0];
    values[1] = rhs[1];   
    cout << name << " is given the same values as " 
        << rhs.name << ", " << values[0] << " and " << values[1] << "n";
    return *this;
}
const Kitty Kitty::operator+ (Kitty& rhs){
    cout << "Temp kitty is returned with values " 
        << values[0] << " + " << rhs[0] << " and " 
        << values[1] << " + " << rhs[1] << "n";
    Kitty temp("Temporary-addition-kitty", values[0]+rhs[0], values[1]+rhs[1]);
    return temp;
}
Kitty& Kitty::operator+= (Kitty& rhs) {
    *this = *this + rhs;
    cout << name << " has " << rhs[0] << " and " << rhs[1] << " added to itn";
    return *this;   
}
const int& Kitty::operator[] (const unsigned int index){
    if(index > 1) {
        cout << "The index passed to " << name << ", (" << index 
            << "), was too large and first value is returnedn";
        return values[0];
    }
    cout << name << " is indexed into, returning " << values[index] << "n";
    return values[index];
}
              
void MyFunction() {
    cout << "My function startsn";
    
    Kitty tigger("Tigger", 4, 5);
    
    tigger += Kitty("Whiskers", 6, 8);
    
    cout << "My function endsn";    
}


int main(int argc, char *argv[])
{
    MyFunction();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


And the compiler has this to say:

Quoting Dev-cpp:
In member function `Kitty& Kitty::operator+=(Kitty&)':

no match for 'operator=' in '*(Kitty*)this = Kitty::operator+(Kitty&)(((Kitty&)(+rhs)))'

candidates are: Kitty& Kitty::operator=(Kitty&)

That one's complaining about the "*this = *this + rhs" line in the += operator function. Also:

Quoting dev-cpp:
In function `void MyFunction()':

no match for 'operator+=' in 'tigger += Kitty(((const std::string&)(&string(((const char*)"Whiskers"), ((const std::allocator<char>&)((const std::allocator<char>*)(&allocator<char>())))))), 6, 8)'

candidates are: Kitty& Kitty::operator+=(Kitty&)


Interestingly the second error doesn't appear if I change the code in MyFunction to be:

Code:
void MyFunction() {
    cout << "My function startsn";
    
    Kitty tigger("Tigger", 4, 5);
    Kitty whiskers("Whiskers", 6, 8);
    
    tigger += whiskers;
    
    cout << "My function endsn";    
}


I really have no idea what its on about :S

[Edited on November 20, 2005 by Frimkron]
____________
#
November 20, 2005, 22:53
PEader
お前はもう死んでいる
1486 posts

It's fucked. Start again. Maybe using a non-memeber function to add two classes. I can't see where the error is since it's a jungle to follow but it is somewhere in your return types and paramters you're changing the return type so it doesn't match up on assignment. I'm a bit rusty though.

[Edited on November 20, 2005 by PEader]
____________
I see 57,005 people.
#
November 20, 2005, 23:01
Fiona
games are terrible
-9616558 posts

Leave Tiggy out of this.
____________
laffo
#
November 20, 2005, 23:40
Frimkron
Frustrated Megalomaniac
703 posts

The thing is, how can it distinguish between a class instance and a reference to a class instance anyway? Those are the only return types that are going on here really.
____________
#
November 21, 2005, 01:42
PEader
お前はもう死んでいる
1486 posts

Quoting Frimkron:
The thing is, how can it distinguish between a class instance and a reference to a class instance anyway? Those are the only return types that are going on here really.

I think it is more that it is a reference to a reference to a reference.

There is something wrong with the assignment operator overload. Maybe it is interfering when you are returning the class in functions. But like I say I'm way too rusty and was never clued up on operator overloading.
____________
I see 57,005 people.
#
November 22, 2005, 21:30
Frimkron
Frustrated Megalomaniac
703 posts

I think I've finally solved this thing.

Apparently in standard C++ (i.e. not Visual C++ 6), you're not allowed to assign a temporary object to a non-const reference. A temporary being one that's created without being declared with a name - like my Whiskers kitty in the program above.

Making all the arguements to the operator functions const seemed to do the trick.
____________
#
November 24, 2005, 12:47
Mike Green
Gone forever
23 posts
Why on earth are you using C++?? Talk about bastardisation. Whats wrong with using plain C? If you code C within .cpp files you can still declare variables anywhere you like - and *usually* C code is faster than C++ code.
____________
So long and thanks for all the fish.
#
November 25, 2005, 21:41
Deadmaster
Where is Johnny?
458 posts
Maybe he's making a program which will require the extra functions of C++ over C, but he hasn't got to using them yet. Like he wants to use classes.

[Edited on December 16, 2005 by Deadmaster]
____________
My site still needs updating and redesigning. My last.fm page.

GO TO TEXTUAL ANARCHY II YOU FISHDOG YOU
#

Message Board > C/C++ > Clearly a wtf is in order

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!