Thread: error: ... does not name a type

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    error: ... does not name a type

    Hi,


    I do not understand why this does not work:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    
    template <typename Tint>
    class D{
      typedef struct XX{
        int x;
      }XX;
      XX* size (Tint z);
      public:
      D();
      Tint get(Tint);
    };
    
    template <typename Tint>
    D<Tint>::D(){}
    
    template <typename Tint>
    XX* D<Tint>::size(Tint z){
      XX* h;
      Tint j = sizeof(Tint);
       h->x = j*z;
      return h;
    }
    
    template <typename Tint>
    Tint D<Tint>::get(Tint){
      XX* d;
      d=size(10);
      return d->x;
    }
    
    
    int main (){
      D<int> f;
      cout << f.get(10) <<endl;
      return 0;
    }
    error i get is:

    Code:
    g++ -std=c++0x -o test test.cpp 
    test.cpp:22:1: error: ‘XX’ does not name a type
    how to make it work ???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to qualify it as typename D<Tint>::XX
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    thnx!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, this:
    Code:
    typedef struct XX{
        int x;
    }XX;
    Is just plain not necessary. It's a relic from C.
    Just write
    Code:
    struct XX
    {
        int x;
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-02-2014, 05:29 PM
  2. Type name error?
    By Pztar in forum C Programming
    Replies: 4
    Last Post: 06-12-2011, 09:30 AM
  3. Replies: 2
    Last Post: 05-23-2011, 02:04 PM
  4. error: array type has incomplete element type
    By gerger in forum C Programming
    Replies: 8
    Last Post: 10-05-2010, 07:40 AM