cant pass the pointer to the funtion

this is really stupid of me but the compiler shows error sayin that add function is not declared. When i remove the pointer from the argument of add...Test* add(Test p)....with the corresponding changes it works normally. From the code segments that are commented u ll know wat i want ultimately. but i m stuck at this point itself.


#include<iostream.h>
#include<conio.h>

class Test{
private:
int a;
public:
Test(int k){
a=k;
}
Test* operator +(Test *p){
int d=this->a+p->geta();
Test *temp=new Test(d);
return temp;
}
Test* add(Test *p){
int d=this->a+p->geta();
Test *temp=new Test(d);
return temp;
}
int geta(){
return a;
}
};

template<class T>
T sum(T a,T b){
return a+b;
}

int main(){
Test *t1,*t2;
t1=new Test(5);
t2=new Test(7);
int k=sum(2,4);
Test *h=t1.add(t2);
//Test *z=sum(t1,t2);
cout<<k<<endl<<h->geta();
getch();
}
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
#include<iostream.h>
#include<conio.h>

class Test
{ 
        private: 
                       int a; 
       public: 
                  Test( int k ) ;
                  Test* operator +(Test *p); 
                  Test* add(Test *p);
                  int geta();
};

Test::Test(int k)
{ 
      a=k; 
} 
Test& Test::operator +(Test *p)
{ 
    int d=a+p->geta(); 
    Test *temp=new Test(d); 
    return temp; 
} 
Test& Test::add(Test *p)
{ 
          int d=a+p->geta(); 
          Test *temp=new Test(d); 
           return temp; 
} 
int Test::geta()
{ 
      return a; 
} 
 


try this class
Last edited on
still not working....
Test&... and Test*.... cant be overloaded....
how can you call 'add' by '.', you must use '->' reference operator right?
Test* h = t1->add(t2)

Use your first code itself.
Last edited on
Oh...thats just careless of me...thanx fr pointing tht out...altho m still sceptical abt th operation of th program.
Um... do you mind writing in standard english? Or at least, do it like me and try to. Wrtn liek thiz doesn't only make it hard to read, it also makes you look stupid.
Topic archived. No new replies allowed.