template & typedefs declaration problem

K

krema2ren

Hi

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

foo.cpp(48) : error C2143: syntax error : missing ';' before '&'
foo.cpp(48) : error C2501: foo<T>::Collection' : missing storage-class
or type specifiers
foo.cpp(48) : error C2065: 'T' : undeclared identifier
foo.cpp(48) : 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;
};
 
A

Alf P. Steinbach

* (e-mail address 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(48) : error C2143: syntax error : missing ';' before '&'
foo.cpp(48) : error C2501: foo<T>::Collection' : missing storage-class
or type specifiers
foo.cpp(48) : error C2065: 'T' : undeclared identifier
foo.cpp(48) : 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

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

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:
 
K

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?


Thanks a lot for your quick answer.

- Dennis
 
A

Alf P. Steinbach

* krema2ren:
Yes, I'm aware of that.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top