Template question: list<Bar<double>> ...

J

janzon

Hi

I have a class Foo that I use both as a Foo<double> and a Foo<float>. I
want to put instances of this class in a list container. The list
container itself is a template and need a type argument. The naive way
below does not work.

list<Bar<double>> myList;

It produces the error message: Error: "," expected instead of ">>". How
do I solve this problem?


Thanks in advance, Daniel
 
T

Thomas Tutone

Hi

I have a class Foo that I use both as a Foo<double> and a Foo<float>.

Those are two _different_ classes, instantiated from the same class
template.
I want to put instances of this class in a list container.

Since they are two different classes, you can't put both in a single
list container without resorting to some tricks that probably would
make this more complicated than it ought to be.
The list
container itself is a template and need a type argument. The naive way
below does not work.

list<Bar<double>> myList;

Change the above line to:

std::list said:
It produces the error message: Error: "," expected instead of ">>". How
do I solve this problem?

Well, that will solve _one_ of your problems.

Best regards,

Tom
 
M

Michael

list said:
It produces the error message: Error: "," expected instead of ">>". How
do I solve this problem?

The lexer is reading >> as one token. You need to put a space between
them:
list<Bar<double> > myList;

Michael
 
T

Thomas Tutone

Thomas said:
Change the above line to:

std::list<Bardouble> > myList; // note space between the two ">"

Sorry, typo. Should be:

std::list<Bar<double> > myList; // note space between the two ">"

Best regards,

Tom
 
J

janzon

Change the above line to:

std::list<Bardouble> > myList; // note space between the two ">"

Adding the missing '<' it works perfectly. So one need to add a space
not to confuse the compiler? I mean, so it doesn't think you want to
use the >> stream operator.
Well, that will solve _one_ of your problems.

Well, the other is quite similar :)
 
M

Marcus Kwok

Adding the missing '<' it works perfectly. So one need to add a space
not to confuse the compiler? I mean, so it doesn't think you want to
use the >> stream operator.

As of now, yes. Usually, when it requires it, I will also add an extra
space at the beginning for symmetry:

std::list< Bar<double> > myList;

IIRC, this will be fixed in C++0x.
 
P

Pete Becker

I have a class Foo that I use both as a Foo<double> and a Foo<float>.

You have a template named Foo, from which you create two classes,
Foo said:
I
want to put instances of this class in a list container. The list
container itself is a template and need a type argument. The naive way
below does not work.

list<Bar<double>> myList;

It produces the error message: Error: "," expected instead of ">>". How
do I solve this problem?

typedef Foo<double> descriptive_name;
list<descriptive_name> myList;

or:

list< Foo<double> > myList;

or wait for a C++0x compiler, which will be available sometime in the
next ten years, and will accept:

list<Foo<double>> myList;

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top