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...

MMS
  • 57
  • 4
  • 1
    Can you please copy in the error? – Rafid Dec 21 '10 at 17:46
  • I vote we don't answer until they edit their question to show compiler vomit. – Edward Strange Dec 21 '10 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! – Rafid Dec 21 '10 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. – John Dibling Dec 21 '10 at 17:55
  • Post *complete* code that will compile (except for the prblem you're having) – John Dibling Dec 21 '10 at 17:56
  • Read the typename FAQ: http://stackoverflow.com/questions/610245/where-to-put-the-template-and-typename-on-dependent-names/613132#613132 – Johannes Schaub - litb Dec 22 '10 at 09:57

5 Answers5

5

Add typename:

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

See Why do we need typename here?

Community
  • 1
  • 1
AnT
  • 291,388
  • 39
  • 487
  • 734
4

have you tried using the typename keyword?

template <class T>
class Points2DList
{
    private:
            typename TPoints2DList <T>::Type points;  //using the typename keyword
 ...
};
Stephane Rolland
  • 34,892
  • 31
  • 111
  • 159
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

Community
  • 1
  • 1
Nawaz
  • 327,095
  • 105
  • 629
  • 812
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;
Mankarse
  • 37,343
  • 9
  • 88
  • 138
-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
wallyk
  • 53,902
  • 14
  • 79
  • 135
  • Even I'm not jerk enough to give erroneous answers, no matter how annoying I find terribly posed questions. – Edward Strange Dec 21 '10 at 18:01
  • @Noah: huh? This change will make the OP's code work, provided `#include ` is added. – wallyk Dec 21 '10 at 18:05
  • It certainly shouldn't, even if you've found a compiler that accepts it as valid. – Edward Strange Dec 21 '10 at 18:06
  • The first one at hand which accepted the repaired code is GNU g++ (GCC) 4.4.4. – wallyk Dec 21 '10 at 18:08
  • Good for g++. There's numerous issues with your "fix". First and foremost, you imply that 'struct' applies to TPointsList and it just doesn't. Second, did you actually try to instantiate the type? Third, did you try with MSVC? Fact is that your "fix" is an actual syntax error and shouldn't compile in ANY implementation, but good for g++ for eating it anyway. I've *never* seen ANY compiler that would happily compile something completely illegal (HAH). – Edward Strange Dec 21 '10 at 18:15
  • Error 1 error C2242: typedef name cannot follow class/struct/union e:\dev_workspace\experimental\2010scratch\2010scratch\scratch.cpp 24 ---- Warning 2 warning C4099: 'std::vector>' : type name first seen using 'class' now seen using 'struct' e:\dev_workspace\experimental\2010scratch\2010scratch\scratch.cpp 24 – Edward Strange Dec 21 '10 at 18:19
  • 2
    Times like this make me wish there was a -5. – Edward Strange Dec 21 '10 at 18:20
  • Putting `struct` before the dependent type won't help on a standard's compliant compiler. Lesson learned: Don't just try slapping together an answer by testing your first thoughts on the first available compiler. – Johannes Schaub - litb Dec 22 '10 at 10:38