Error in Programme

Dear Sir,

I dont understand the below error in following Program. I'm trying to return objects from member function.


35 C:\...\Untitled1.cpp no match for 'operator=' in 'r = (&my)->Travel::add(your)'
note C:\...\Untitled1.cpp:6 candidates are: Travel& Travel::operator=(const Travel&)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Returning Object from Member function.
#include <iostream>
#include <string>
using namespace std;

class Travel{
     private:
             int km, hr;
     public:
            Travel(){
                     km=hr=0;
                     }
            int get(){
                  cout <<"Enter Kilometer: ";
                  cin>>km;
                  cout<<"Enter Hour: ";
                  cin>>hr;
                }
            void show(){
                   cout <<"Your Total Travel Kilometer is: " <<km<<" in Hour "<<hr<<endl;  
                 }
            void add(Travel p){
                   Travel t;
                   t.km=km+p.km;
                   t.hr=hr+p.hr;
                   return t;
                 }
};
int main(){
    Travel my,your,r;
    my.get();
    my.show();
    your.get();
    your.show();
    r=my.add(your);
    cout <<"!!! Total traveling Record !!!"<<endl;
    r.show();
    system("pause");
}
There are a couple of problems with function return types:
 
    int get(){
should be
 
    void get(){
because the function does not return a value.

The opposite problem is in function add(), it is declared as void, but it should return a value of type Travel
Change
 
    void add(Travel p){
to
 
    Travel add(Travel p){


Thank you..

I wonder when ill become expert in programming
Topic archived. No new replies allowed.