First Topic. Operator overloading

+2 baaa booo · August 22, 2016
Hey i am new guys so at first dont be to harm pls. I saw buckys ep.50-51 where he explains how to overload the operator "+" soo i tried it too and it ended with a bunches of errors might u want to help me ;)? i hope so.
//Code follows soon:
#include <iostream>

using namespace std;

class Water{
    public:
        int liter;
        Water()
        {}
        Water(int a){
            liter   =   a;
        }
        Water::operator+(Water b){
            Water neu;
            neu.liter =   liter   +   b.liter;
            return(neu);
        }
};

int main()
{
   Water Redsea(22);
   Water Ocean(201);
   Water Sum;
   
   Sum=Redsea+Ocean;
   
   cout << "Redsea:"        << Redsea.liter         << endl;
   cout << "Ocean:"         << Ocean.liter          << endl;
   cout << "Sum_of_Both:"   << Sum                  << endl;
   
}

Damn where is the issue(s)? do i have a handicap or something that i cant find any issue? however i think i did exact the same way as bucky did just without splitting the code over 3 sheets.

Post a Reply

Replies

Oldest  Newest  Rating
0 baaa booo · August 29, 2016
Hey guys i am still waiting for an answer in case that noone saw this thread i hope it helps to post this to push the thread up 
0 baaa booo · August 29, 2016


Water::operator

Drop the Water::. You are already inside the water class. Also what is the return type of this operator function?
cout << "Sum_of_Both:"   << Sum                  << endl;

You mean Sum.liter?


at first i didnt used Water:: the compiler told me to do this ; /

yea i mean Sum.liter


edited how u said 
#include <iostream>

using namespace std;

class Water{
    public:
        int liter;
        Water()
        {}
        Water(int a){
            liter   =   a;
        }
        operator+(Water b){
            Water neu;
            neu.liter =   liter   +   b.liter;
            return(neu.liter); // Why I cant write just return(neu) like Bucky did? I am using C++11 and Bucky used it to because
        } // he uploaded at 2011 I don't get it.
}; // And why cant I use return(liter + b.liter)

int main()
{
   Water Redsea(22);
   Water Ocean(201);
   Water Sum;
   
   Sum=Redsea+Ocean;
   
   cout << "Redsea:"        << Redsea.liter         << endl;
   cout << "Ocean:"         << Ocean.liter          << endl;
   cout << "Sum_of_Both:"   << Sum.liter                  << endl;
   
}


/*
Error:
sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                   
main.cpp:13:26: error: ISO C++ forbids declaration of 'operator+' with no type [-fpermissive]                                                                                                                                                          
        operator+(Water b){ 
*/
// Why the hell I get this error? I did exact the same like Bucky did... I also tried it with operator+(int b) and Water b.liter

I keep going watching Buckys C++ Tutorials and new Questions are going to pop up : /  Should I create to every question a new thread or add it in this thread!? I mean the source code is so long isnt there a "spoiler-function"?
0 umer afzal · August 29, 2016
What are the errors? can you post them here!!
0 baaa booo · September 2, 2016
I already posted the error into the code.
  • 1

C++

130,881 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator