0

What is wrong in the following code:

Point2D.h

template <class T> 
class Point2D 
{     
   private:
         T x;
         T y; 
   ... 
 };

PointsList.h

template <class T>
class Point2D;

template <class T>
struct TPointsList
{
    typedef std::vector <Point2D <T> > Type;
};

template <class T>
class PointsList
{
    private:
            TPointsList <T>::Type points;  //Compiler error
 ...
};

I would like to create new user type TPointsList without direct type specification...

6
  • 1
    Can you please copy in the error? Commented Dec 21, 2010 at 17:46
  • I vote we don't answer until they edit their question to show compiler vomit. Commented Dec 21, 2010 at 17:48
  • @Noah, well I usually try to be tolerant. @MMS, you seem to be new on this site, so be careful with your question and try to explain as much as possible, because people will vote down for you and you will lose points! Commented Dec 21, 2010 at 17:52
  • -1 because: 1) no compiler error shown, 2) no declaration for TPoints2DList shown, 3) wall of psudocode, 4) you ask us specifically what is wrong with code that wont compile, doesnt have complete declarations and doesnt even have constructors. Commented Dec 21, 2010 at 17:55
  • Post complete code that will compile (except for the prblem you're having) Commented Dec 21, 2010 at 17:56

5 Answers 5

5

Add typename:

...
typename TPointsList<T>::Type points;
...

See Why do we need typename here?

Sign up to request clarification or add additional context in comments.

Comments

4

have you tried using the typename keyword?

template <class T>
class Points2DList
{
    private:
            typename TPoints2DList <T>::Type points;  //using the typename keyword
 ...
};

Comments

2

Others have already answered your question, but I think, if you want to know why typename is required there, then you may see this topic:

Use of typename keyword with typedef and new

Comments

1

The question is a bit unclear, but it appears that you are trying to instantiate a vector of Point2D without having the definition of the Point2D template available. Try adding #include "Point2D.h" to the top of PointsList.h. As other answerers have mentioned, you are also attempting to use a qualified dependant type without a typename, so you should also add change the line

TPointsList <T>::Type points;  //Compiler error

to:

typename TPointsList <T>::Type points;

Comments

-1

What is TPoints2DList? It's not declared anywhere.

Now that TPoints2DList is declared as a struct, it has be referenced as such:

   private:
            struct TPointsList <T>::Type points;  //should compile now

8 Comments

Even I'm not jerk enough to give erroneous answers, no matter how annoying I find terribly posed questions.
@Noah: huh? This change will make the OP's code work, provided #include <vector> is added.
It certainly shouldn't, even if you've found a compiler that accepts it as valid.
The first one at hand which accepted the repaired code is GNU g++ (GCC) 4.4.4.
Times like this make me wish there was a -5.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.