CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52

    template classes and types

    Hello,
    Can anybody please tell me what I'm doing wrong here. When I do something like:
    Code:
    template<class T> 
    class MyNode
    {
      public:
        
        MyNode(){key=-1;}
        friend class MyClass;  
       private :
        T info;
        int key; 
     
       
    };
    
    template<class T>
    class MyClass
    {
      public:
        MyClass(){count=0; }
         
      private:
        MyNode Myarray [Item_Max];
        int count;
    };
    I get an error message saying: syntax error before `[' token
    in the line: MyNode Myarray [Item_Max];
    while it runs normal when I do something like T Myarray[Item_Max];
    What I need is an array of type MyNode but I seem to be confusing the types there
    Thanks in advance,
    Sofia.

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Try this:

    Code:
    template<class T> 
    class MyNode
    {
      public:
        
        MyNode(){key=-1;}
        friend class MyClass;  
       private :
        T info;
        int key; 
     
       
    };
    
    template<class T>
    class MyClass
    {
      public:
        MyClass(){count=0; }
         
      private:
        MyNode<T> Myarray [Item_Max];   // <<<-------------
        int count;
    };
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Oh, and use std::vector rather than a fixed array...
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52
    Thanks Graham. I could have sworn that I tried that but I guess I didn't
    I have to use an array as I can't insert in a vector of size i.e 10 in position 12. It gets messy everytime I start pushing back in the vector when i have to insert to a position that doesn't exist yet.
    Thanks again.
    Sofia

  5. #5
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52
    sorry to bother you again but I'm really stuck as I see no reason for the errors I've been having. Using the same class prototypes I have posted before with the correction from Graham i declare in my main() the following: MyClass <int> Myobj;
    but I get a bunch of errors that I don't fully understand:
    C:/prog/sofia.cpp: In instantiation of `MyNode<int>':
    C:/prog/sofia.cpp:27: instantiated from `MyClass<int>'
    C:/prog/sofia.cpp:38: instantiated from here
    C:/prog/sofia.cpp:13: ` struct MyClass' redeclared as different kind of symbol
    C:/prog/sofia.cpp:27: previous declaration of `template<class T> class MyClass'

    Can anybody clear this for me please.
    Thanks a bunch,
    Sofia.

  6. #6
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Code:
    // Forward declaration
    template <class T>
    class MyClass;
    
    template<class T> 
    class MyNode
    {
      public:
        
        MyNode(){key=-1;}
        friend class MyClass<T>;
       private :
        T info;
        int key; 
     };
    
    
    // Need to declare Item_Max somewhere.
    const int Item_Max=10;
    
    template<class T>
    class MyClass
    {
      public:
        MyClass(){count=0; }
         
      private:
        MyNode<T> Myarray [Item_Max];
        int count;
    };

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Originally posted by megabyte
    <snip>
    I have to use an array as I can't insert in a vector of size i.e 10 in position 12. It gets messy everytime I start pushing back in the vector when i have to insert to a position that doesn't exist yet.
    Thanks again.
    Sofia
    Have you investigated std::vector::resize()?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52
    Thanks a lot Kheun! I really appreciate it.
    That <T> in friend class MyClass<T>; saved me a lotta Aspirin

    Thanks for the tip Graham, I've knew that resize existed but thought that it was used only to resize down and not to fill up a vector as well.Now that I know I can use it, I tried to use a vector in my program but then I have no idea how the syntax should be as I'm getting the same problems I had with MyNode<T> Myarray [Item_Max];
    How would I use a vector instead of an array here? I try vector<MyNode> MyVector; but then I know that the <T> should go somewhere
    Any help would be really appreciated.
    Sofia.

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Code:
    std::vector<MyNode<T> > My Vector;
    The space between the two ">"s is important.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured