473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

template & typedefs declaration problem

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
5 1797
* 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
>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
* 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

<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
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Me | last post by:
I am not understanding an aspect of how to implement a class template in a ..cpp file. What I do know is that there are at least two problems with my understanding of how to accomplish the...
14
by: John Harrison | last post by:
The following code does not compile template <class Derived> struct X { typedef typename Derived::type type; }; struct Y : public X<Y> {
14
by: G Patel | last post by:
Pg. 140 of K&R2 shows an example of mutually referential structure declarations... struct t { struct s *p; }; struct s {
13
by: imutate | last post by:
Hi, I am migrating some std::vectors to use a template instead, but I get an incomplete type error in a struct declaration. #include <vector> template < typename T > class Vec : public...
4
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the example below //using Tparent::Tptr; //typedef...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
5
by: Paul J. Lucas | last post by:
Given: template<typename T,typename Uclass C { }; template<typename Ttypedef C<T,intC2; I get: test.cpp:2: error: template declaration of 'typedef' Are template typedefs still not...
8
by: 2b|!2b==? | last post by:
I have the ff template class: template <class T1, class T2class Periodic ; I have two questions/problems Q1). I want to specialize it further into two template classes:
29
by: Bob Nelson | last post by:
After completing KNK2, my interest in C was reignited, prompting a re-reading of Traister's dubious book on pointers. Then I dusted off the fine ``C Traps and Pitfalls'' by Andy Koenig. Nowadays I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.