Connecting Tech Pros Worldwide Forums | Help | Site Map

template & typedefs declaration problem

krema2ren@gmail.com
Guest
 
Posts: n/a
#1: Dec 8 '05
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;
};


Alf P. Steinbach
Guest
 
Posts: n/a
#2: Dec 8 '05

re: template & typedefs declaration problem


* 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?
krema2ren
Guest
 
Posts: n/a
#3: Dec 8 '05

re: template & typedefs declaration problem


>Are you aware that you're defining a tree structure? Each foo[color=blue]
>contains a possibly non-empty collection of foo's.[/color]

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

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

Alf P. Steinbach
Guest
 
Posts: n/a
#4: Dec 8 '05

re: template & typedefs declaration problem


* krema2ren:[color=blue][color=green]
> >Are you aware that you're defining a tree structure? Each foo
> >contains a possibly non-empty collection of foo's.[/color]
>
> Yes, I'm aware of that.
>[color=green]
> >Technically, only one compiler supports placing template definitions
> > in a separately compiled cpp file.[/color]
>
> I'm using MSVC 7.1, and it seems that it is supported here. Does GCC
> not support that?[/color]

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?
Sumit Rajan
Guest
 
Posts: n/a
#5: Dec 8 '05

re: template & typedefs declaration problem



<krema2ren@gmail.com> wrote in message
news:1134034807.648459.115390@z14g2000cwz.googlegr oups.com...[color=blue]
> Hi
>
> Does some of you know how to declare getCollection() in the cpp file?
> My code snippet below produces following compile error:
>[/color]
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 <sumitr@msdc.hcltech.com>


krema2ren
Guest
 
Posts: n/a
#6: Dec 8 '05

re: template & typedefs declaration problem


>Neither MSVC 7.1 nor GCC support that, yet.[color=blue]
>It will compile but won't link.[/color]

Unfortunately you are right....

- Dennis

Closed Thread