Re: Error when returning a structure pointer
pai wrote:
The following program gives error can any one solve this.
#include<iostream.h>
Please substitute the following for the above line:
#include <iostream>
using std::cout;
struct ab{
int a;
};
template <class T>
class A{
struct ab * ret_1();
Why do you use "struct" in the above line? It confuses matters. Please
omit it. In other words, substitute the following instead:
ab* ret_1();
public:
A();
void disp();
void ret();
Please note that in the above line, you declare ret() to return void.
};
template <class T>
void A<T>::ret(){
Again, please note that you have declared this member function to
return void, not ab*.
struct ab* b;
Again, why do you use "struct" in the above line? It confuses matters.
Please omit it.
b=ret_1();
return b;
The above line is an error. This function returns void, not ab*, so
please delete the above line entirely.
}
template <class T>
struct ab * A<T>::ret_1(){
Again, why do you use "struct" in the above line? It confuses matters.
Please omit it.
struct ab* b;
Again, why do you use "struct" in the above line? It confuses matters.
Please omit it.
b=new (struct ab);
Again, why do you use "struct" in the above line? It confuses matters.
Please omit it.
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;
}
[compilation errors snipped.]
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.
First, please don't use <iostream.h>, which is not a standard header.
Use <iostream> instead.
Second, if you have a void function, please don't try to return a value
- that's an error. When I fixed that (and the wrong header), your
program compiled fine (even if I didn't delete your unnecessary
"struct"s).
Third, although they don't actually affect the meaning, please omit all
the unnecessary "struct"s - they confuse matters (and appear to confuse
your compiler as well).
Best regards,
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]