Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template & typedefs declaration problem

Reply
Thread Tools

template & typedefs declaration problem

 
 
krema2ren@gmail.com
Guest
Posts: n/a
 
      12-08-2005
Hi

Does some of you know how to declare getCollection() in the cpp file?
My code snippet below produces following compile error:

foo.cpp(4 : error C2143: syntax error : missing ';' before '&'
foo.cpp(4 : error C2501: foo<T>::Collection' : missing storage-class
or type specifiers
foo.cpp(4 : error C2065: 'T' : undeclared identifier
foo.cpp(4 : error C2955: 'foo' : use of class template requires
template argument list

Hope you guys can help me...

foo.h
--------------------------------------
template <class T>
class foo
{
public:
typedef std::vector< foo<T> > Collection;

foo();
virtual ~foo(void);

Collection& getCollection();

private:
Collection col;
};


foo.cpp
--------------------------------------
template <class T>
foo<T>::foo(void)
{
}

template <class T>
foo<T>::~foo(void)
{
}

template <class T>
foo<T>::Collection& foo<T>::getCollection()
{
return col;
};

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-08-2005
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> Hi
>
> Does some of you know how to declare getCollection() in the cpp file?


2 don'ts:

* Technically, only one compiler supports placing template definitions
in a separately compiled cpp file.

* Stylistically, if you use the name 'getCollection' instead of just
'collection', your code will be less readable, and in other cases
than the one you have you will have removed a useful name for an
optimized command-oriented version of an expression-oriented func.


> My code snippet below produces following compile error:
>
> foo.cpp(4 : error C2143: syntax error : missing ';' before '&'
> foo.cpp(4 : error C2501: foo<T>::Collection' : missing storage-class
> or type specifiers
> foo.cpp(4 : error C2065: 'T' : undeclared identifier
> foo.cpp(4 : error C2955: 'foo' : use of class template requires
> template argument list
>
> Hope you guys can help me...
>
> foo.h
> --------------------------------------


Here you need

#ifndef FOO_H
#define FOO_H

#include <vector>


> template <class T>
> class foo
> {
> public:
> typedef std::vector< foo<T> > Collection;


Are you aware that you're defining a tree structure? Each foo
contains a possibly non-empty collection of foo's.

>
> foo();
> virtual ~foo(void);


'void' is a C-ism: don't.


>
> Collection& getCollection();


Should probably also have a 'const' version of that function.


>
> private:
> Collection col;
> };


#endif

>
>
> foo.cpp


See above -- with most compilers you simply can't place these
definitons in a separately compiled cpp file: put them in the
header file.


> template <class T>
> foo<T>::foo(void)
> {
> }
>
> template <class T>
> foo<T>::~foo(void)
> {
> }
>
> template <class T>
> foo<T>::Collection& foo<T>::getCollection()


Here you need a 'typename' to tell the compiler that Collection is a type:

typename foo<T>::Collection& foo<T>::getCollection()

> {
> return col;
> };


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
krema2ren
Guest
Posts: n/a
 
      12-08-2005
>Are you aware that you're defining a tree structure? Each foo
>contains a possibly non-empty collection of foo's.


Yes, I'm aware of that.

>Technically, only one compiler supports placing template definitions
> in a separately compiled cpp file.


I'm using MSVC 7.1, and it seems that it is supported here. Does GCC
not support that?


Thanks a lot for your quick answer.

- Dennis

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-08-2005
* krema2ren:
> >Are you aware that you're defining a tree structure? Each foo
> >contains a possibly non-empty collection of foo's.

>
> Yes, I'm aware of that.
>
> >Technically, only one compiler supports placing template definitions
> > in a separately compiled cpp file.

>
> I'm using MSVC 7.1, and it seems that it is supported here. Does GCC
> not support that?


Neither MSVC 7.1 nor GCC support that, yet.

It will compile but won't link.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      12-08-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi
>
> Does some of you know how to declare getCollection() in the cpp file?
> My code snippet below produces following compile error:
>

http://www.parashift.com/c++-faq-lit...html#faq-35.12
http://www.parashift.com/c++-faq-lit...html#faq-35.13
http://www.parashift.com/c++-faq-lit...html#faq-35.14

Regards,
Sumit.
--
Sumit Rajan <(E-Mail Removed)>


 
Reply With Quote
 
krema2ren
Guest
Posts: n/a
 
      12-08-2005
>Neither MSVC 7.1 nor GCC support that, yet.
>It will compile but won't link.


Unfortunately you are right....

- Dennis

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template<> keyword - unexpected hides base typedefs Grizlyk C++ 4 01-15-2007 02:26 PM
template instantiation and typedefs. aiooua@gmail.com C++ 3 12-08-2006 10:14 AM
template id does not match any template declaration? Alex Buell C++ 2 02-21-2006 06:33 PM
template-id does not match any template declaration blueblueblue2005 C++ 6 07-19-2005 02:59 PM
Forward declaration and typedefs Simon Elliott C++ 5 01-07-2005 10:02 PM



Advertisments