Error when returning a structure pointer
Hi ,
The following program gives error can any one solve this.
#include<iostream.h>
struct ab{
int a;
};
template <class T>
class A{
struct ab * ret_1();
public:
A();
void disp();
void ret();
};
template <class T>
void A<T>::ret(){
struct ab* b;
b=ret_1();
return b;
}
template <class T>
struct ab * A<T>::ret_1(){
struct ab* b;
b=new (struct ab);
return b;
}
template<class T>
A<T>::A(){
cout << "A const" ;
}
template <class T>
void A<T>::disp(){
cout << "disp" << endl;
}
int main(){
A<int> a;
a.disp();
return 0;
}
*****************************
Compiling...
test.cpp
C:\Pai\c++\linked-list\test.cpp(28) : error C2989: 'ab' : template
class has already been defined as a non-template class
C:\Pai\c++\linked-list\test.cpp(3) : see declaration of 'ab'
C:\Pai\c++\linked-list\test.cpp(28) : error C2143: syntax error :
missing ';' before '*'
C:\Pai\c++\linked-list\test.cpp(28) : error C2065: 'T' : undeclared
identifier
C:\Pai\c++\linked-list\test.cpp(28) : error C2955: 'A' : use of class
template requires template argument list
C:\Pai\c++\linked-list\test.cpp(16) : see declaration of 'A'
C:\Pai\c++\linked-list\test.cpp(28) : error C2501: 'ret_1' : missing
storage-class or type specifiers
C:\Pai\c++\linked-list\test.cpp(28) : error C2556: 'int *__thiscall
A::ret_1(void)' : overloaded function differs only by return type from
'struct ab *__thiscall A<T>::ret_1(void)'
C:\Pai\c++\linked-list\test.cpp(10) : see declaration of
'ret_1'
C:\Pai\c++\linked-list\test.cpp(28) : error C2371: 'ret_1' :
redefinition; different basic types
C:\Pai\c++\linked-list\test.cpp(10) : see declaration of
'ret_1'
C:\Pai\c++\linked-list\test.cpp(54) : error C2079: 'a' uses undefined
class 'A<int>'
C:\Pai\c++\linked-list\test.cpp(56) : error C2228: left of '.disp' must
have class/struct/union type
Error executing cl.exe.
test.obj - 9 error(s), 0 warning(s)
****************************************
This is just a sample program . I am trying to return a structure
pointer in a priviate method . but as the calss is template class
defining it is giveing me error . How could i rewite to remove the
error.
Thanks
Pai..
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]