*
krema2ren@gmail.com:[color=blue]
> Hi
>
> Does some of you know how to declare getCollection() in the cpp file?[/color]
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.
[color=blue]
> 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
> --------------------------------------[/color]
Here you need
#ifndef FOO_H
#define FOO_H
#include <vector>
[color=blue]
> template <class T>
> class foo
> {
> public:
> typedef std::vector< foo<T> > Collection;[/color]
Are you aware that you're defining a tree structure? Each foo
contains a possibly non-empty collection of foo's.
[color=blue]
>
> foo();
> virtual ~foo(void);[/color]
'void' is a C-ism: don't.
[color=blue]
>
> Collection& getCollection();[/color]
Should probably also have a 'const' version of that function.
[color=blue]
>
> private:
> Collection col;
> };[/color]
#endif
[color=blue]
>
>
> foo.cpp[/color]
See above -- with most compilers you simply can't place these
definitons in a separately compiled cpp file: put them in the
header file.
[color=blue]
> template <class T>
> foo<T>::foo(void)
> {
> }
>
> template <class T>
> foo<T>::~foo(void)
> {
> }
>
> template <class T>
> foo<T>::Collection& foo<T>::getCollection()[/color]
Here you need a 'typename' to tell the compiler that Collection is a type:
typename foo<T>::Collection& foo<T>::getCollection()
[color=blue]
> {
> return col;
> };[/color]
--
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?