By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

template & typedefs declaration problem

P: n/a
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;
};

Dec 8 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* kr*******@gmail.com:
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

#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?
Dec 8 '05 #2

P: n/a
>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

Dec 8 '05 #3

P: n/a
* 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?
Dec 8 '05 #4

P: n/a

<kr*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr 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 <su****@msdc.hcltech.com>
Dec 8 '05 #5

P: n/a
>Neither MSVC 7.1 nor GCC support that, yet.
It will compile but won't link.


Unfortunately you are right....

- Dennis

Dec 8 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.