C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Error when returning a structure pointer

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
pai
Guest





PostPosted: Wed Jun 28, 2006 3:42 pm    Post subject: Error when returning a structure pointer Reply with quote



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(2Cool : 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(2Cool : error C2143: syntax error :
missing ';' before '*'
C:\Pai\c++\linked-list\test.cpp(2Cool : error C2065: 'T' : undeclared
identifier
C:\Pai\c++\linked-list\test.cpp(2Cool : 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(2Cool : error C2501: 'ret_1' : missing
storage-class or type specifiers
C:\Pai\c++\linked-list\test.cpp(2Cool : 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(2Cool : 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! ]
Back to top
dan2online
Guest





PostPosted: Thu Jun 29, 2006 4:29 pm    Post subject: Re: Error when returning a structure pointer Reply with quote



pai wrote:
Quote:
Hi ,

The following program gives error can any one solve this.

#include<iostream.h

struct ab{
int a;
};

template <class T

#include<iostream.h>
==> #include <iostream>

template <class T>
==>template <typename T>


Quote:
template<class T
A<T>::A(){

cout << "A const" ;

==> std::cout << "A const" ;

Quote:

}


template <class T
void A<T>::disp(){
cout << "disp" << endl;

std::cout << "disp" << std::endl;


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Sat Jul 01, 2006 2:23 am    Post subject: Re: Error when returning a structure pointer Reply with quote



In article <1151471069.719412.29090 (AT) d56g2000cwd (DOT) googlegroups.com>, pai
<grpai1 (AT) yahoo (DOT) com> writes
Quote:
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(){

The above line confuses the compiler. You have used a C'ism by using the
keyword 'struct'. I am not sure whether C++ actually prohibits its use
in this context but I am not surprised that a compiler would be confused
by its, at best redundant, use.
Quote:

struct ab* b;
b=new (struct ab);

return b;

}
C++ struct names are type names and do not need to be proceeded by

'struct'. Sometimes it is still OK in C++ as a mechanism for unhiding a
name that would otherwise be hidden by a later declaration but even that
should be avoided. If you need to deal with a C compatibility issue,
then use a typedef.

General comment: See how learning C first can cause problems in writing
good idiomatic C++?

--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Mon Jul 03, 2006 4:14 pm    Post subject: Re: Error when returning a structure pointer Reply with quote

Thomas Tutone wrote:
Quote:
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;

Which converts what was presumably legal (albeit non-standard)
code to undefined behavior. If you're going to make
recommendations, at least get them right: he needs an
#include <ostream>
as well, and a
using std::endl ;
(although many of us prefer to simply prefix each use with
std:Smile.

Quote:
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();

That would certainly be more idiomatic for C++. But my reading
of the standard says that the original code is legal, too (and
has exactly the same meaning).

Quote:
public:
A();
void disp();
void ret();

Please note that in the above line, you declare ret() to return void.

[...]
Quote:
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.

Technically, I think this is legal too (although I'm not 100%
sure). On the other hand, I can very easily imagine a compiler
getting confused about it.

[...]
Quote:
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.

I you want to use the standard headers (which is definitly
preferrable today), you need at least two of them. The standard
headers are NOT a one to one replacement for the classical
header.

Quote:
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).

I doubt that there is a compiler around today where you need to
fix the headers, either. While it's good advice to prefer the
standard headers (even if the classical iostream is better
designed), especially when learning, it's important too to learn
to use them correctly. This means including all of the correct
headers, and either specifying all of the qualifiers (e.g.
std:Smile, or correctly specifying the using. I'm rather
surprised, in fact, that your modified code compiled without an
error on endl. And it has undefined behavior unless you also
include <ostream>. (In practice, I suspect that this undefined
behavior will work just about anywhere. In this case, the
simplest solution might just be to bring the standard in line
with existing practice.)

Quote:
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).

Agreed. They may be legal, but they certainly aren't idiomatic
C++. About the only place I expect to see them is in header
files which must be included in both C++ and C.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.