CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2009
    Posts
    7

    Creating template class

    I am new to c++. I have been trying to create a template class ADT but keep getting syntax error.I am stuck please look at my files and see whats wrong. thanks

    header file:

    Code:
    #ifndef BOX
    #define BOX
    
    #include <list>
    #include <string>
    
    using namespace std;
    
    //********************************************************************
    //  
    //          Class Declaration for box ADT
    //
    //********************************************************************
    
    template<typename T>
    class box{
          public :
                 box();
                 
                 //constructs box object with user input size
                 box(int userBoxSize);
                 
                 
                 //accessors
                 int getSize(); //returns the number of items in the box
                 list<T> getItems();//returns a linked list of items in the box
                 void printBox();
                 //modifiers
                 void emptyBox(); //empties the box
                 T removeItem(T userItem);//parameters should say which items is to be removed
                 void addItem(T userItem); // add an items to the box
                 T checkReps(T userItem); //checks reps of item in box and returns the number
                 void mergeBox(box userBox); //takes the box specified and merges it with box
                 
                 
                 
          private :      
                  int size,i,rep;
                  list<T> boxList,tempList;
                  list<T>:: iterator listIter,tempIter;
                         
          };
    
    
    
    #endif
    implementation:

    Code:
    #include<list>
    #include<iostream>
    #include"box.h"
    
    //********************************************************
    //
    //  Implementation of box ADT
    //
    //********************************************************
    
    //default constructor
    template <typename T>
    box<T>::box()
    {}
    
    //overloaded constructor
    template <typename T>
    box<T>::box(int userSize)
    {
        list<T> boxList(userSize);
                                                  
    }
    
    //accessor
    template <typename T>
    int box<T>::getSize()
    {
        return boxList.size();        
    }
    
    list<T> box::getItems()
    {
         return boxList<T>;
         
    }
    
    //modifiers
    
    //empty box
    template <typename T>
    void box<T>::emptyBox()
    {
         listIter<T> = boxList.begin();
         while(!boxList.empty())
         {
            boxList.erase(listIter); 
            listIter++;
         }     
    }
    
    //remove an item
    template <typename T>
    T box<T>::removeItem(int userItem)
    {
        listIter = boxList.begin();
        while(*listIter != userItem)
        {
           listIter++;
        }
        boxList.erase(listIter);
    }
    
    //add an item
    template <typename T>
    void box<type>::addItem(T userItem)
    {
       boxList.push_back(userItem);
    }
    
    //check reps
    
    template <typename T>
    type box<T>::checkReps(T userItem)
    {
        rep=0;
        listIter = boxList.begin();
        while(listIter!= boxList.end())
        {
           if(*listIter == userItem)
           {
              rep++;
           }
           listIter++;
        }    
        return rep;
    }
    
    //merge box
    template <typename T>
    void box<T>::mergeBox(box userBox)
    {
         tempList = userBox.getItems();
         tempIter = tempList.begin();
         while(!tempList.empty())
         {
            boxList.push_back(*tempIter);
            tempIter++;
            tempList.pop_front();
         }
    }
    
    //prtype box
    
    template <typename T>
    void box<T>::printBox()
    {
         listIter = boxList.begin();
         cout<<"The number of items in the box is "<< boxList.size()<<"\n";
         
         i=0;
         while(i<boxList.size())
         {
            cout<<*listIter<<"  \n";
            listIter++;
            i++;
         } 
    }
    main file

    Code:
    #include <cstdlib>
    #include <iostream>
    #include"box.h"
    using namespace std;
    
    int main()
    {
        box<int> mohs(10);
        mohs.addItem(2);
        mohs.addItem(4);
        mohs.addItem(6);
        mohs.addItem(8);
        mohs.addItem(12);
        mohs.addItem(12);
        mohs.printBox();
        
        cout<<endl;
        cout<<"number of repetitions of 12 : "<<mohs.checkReps(12)<<endl;
        
        box<int> hsn(5);
        hsn.addItem(3);
        hsn.addItem(5);
        hsn.addItem(7);
        hsn.addItem(9);
        hsn.printBox();
        
        
        mohs.mergeBox(hsn);
        mohs.printBox();
        mohs.removeItem(7);
        mohs.printBox();
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Creating template class

    Quote Originally Posted by codefreaq View Post
    I am new to c++. I have been trying to create a template class ADT but keep getting syntax error.I am stuck please look at my files and see whats wrong. thanks
    Err... if you already know what the error is, then tell us. Why should we try to find it when you know what it is?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Creating template class

    Post the error message here.
    Thanks for your help.

  4. #4
    Join Date
    Mar 2009
    Posts
    7

    Re: Creating template class

    Quote Originally Posted by Mybowlcut View Post
    Err... if you already know what the error is, then tell us. Why should we try to find it when you know what it is?
    sorry, I forgot

    In file included from box.cpp:3:
    box.h:40: error: expected `;' before "listIter"

    box.cpp:31: error: `T' was not declared in this scope
    box.cpp:31: error: template argument 1 is invalid
    box.cpp:31: error: template argument 2 is invalid
    box.cpp:31: error: `template<class T> class box' used without template parameters

    box.cpp:32: error: ISO C++ forbids declaration of `getItems' with no type
    box.cpp: In function `int getItems()':
    box.cpp:33: error: `boxList' undeclared (first use this function)
    box.cpp:33: error: (Each undeclared identifier is reported only once for each function it appears in.)
    box.cpp:33: error: `T' undeclared (first use this function)
    box.cpp:33: error: expected primary-expression before ';' token
    box.cpp: In member function `void box<T>::emptyBox()':
    box.cpp:43: error: `listIter' undeclared (first use this function)
    box.cpp:43: error: expected primary-expression before '>' token
    box.cpp:43: error: expected primary-expression before '=' token
    box.cpp: At global scope:
    box.cpp:54: error: prototype for `T box<T>::removeItem(int)' does not match any in class `box<T>'
    box.h:30: error: candidate is: T box<T>::removeItem(T)

    box.cpp:54: error: template definition of non-template `T box<T>::removeItem(int)'
    box.cpp: In member function `T box<T>::removeItem(int)':
    box.cpp:55: error: `listIter' undeclared (first use this function)
    box.cpp: At global scope:
    box.cpp:65: error: `type' was not declared in this scope
    box.cpp:65: error: template argument 1 is invalid
    box.cpp: In function `void addItem(T)':
    box.cpp:67: error: `boxList' undeclared (first use this function)
    box.cpp: At global scope:
    box.cpp:73: error: expected constructor, destructor, or type conversion before "box"
    box.cpp:73: error: expected `;' before "box"
    box.cpp: In member function `void box<T>::mergeBox(box<T>)':
    box.cpp:93: error: `tempIter' undeclared (first use this function)
    box.cpp: In member function `void box<T>:rintBox()':
    box.cpp:107: error: `listIter' undeclared (first use this function)

    make.exe: *** [box.o] Error 1

    Execution terminated

  5. #5
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Creating template class

    Well, I'm not sure, but is it legal to have a space between 'list<T>::' and 'iterator' in box.h?
    Quote Originally Posted by codefreaq View Post
    Code:
                  list<T>:: iterator listIter,tempIter;
    Edit:
    Also, in box.cpp, you forgot the 'template<typename T>' for the method 'getItems', and the '<T>' after the 'box' in the definition of the same method, and when you return from the same method, you don't specify template type. That member's already been defined as having that template type.
    The 'removeItem' method in the class definition takes type 'T' as a parameter, and the one in the implementation takes and 'int'
    In implementation: 'void box<type>::addItem(T userItem)' should be changed to 'void box<T>::addItem(T userItem)'
    In implementation: 'type box<T>::checkReps(T userItem)' should be changed to 'T box<T>::checkReps(T userItem)'
    Last edited by Etherous; March 21st, 2009 at 01:00 PM.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  6. #6
    Join Date
    Mar 2009
    Posts
    7

    Re: Creating template class

    Thanks Etherous, I am still getting errors on instantiation of List iterators after removing the space between list<T>::iterator. This seems to be the only problem left.

    In file included from box.cpp:3:
    box.h:40: error: expected `;' before "listIter"
    box.cpp:32: error: `template<class T> class box' used without template parameters
    box.cpp: In function `std::list<T, std::allocator<_CharT> > getItems()':

    box.cpp:34: error: `boxList' undeclared (first use this function)
    box.cpp:34: error: (Each undeclared identifier is reported only once for each function it appears in.)
    box.cpp: In member function `void box<T>::emptyBox()':
    box.cpp:44: error: `listIter' undeclared (first use this function)
    box.cpp:44: error: expected primary-expression before '>' token
    box.cpp:44: error: expected primary-expression before '=' token
    box.cpp: In member function `T box<T>::removeItem(T)':
    box.cpp:56: error: `listIter' undeclared (first use this function)
    box.cpp: In member function `T box<T>::checkReps(T)':
    box.cpp:77: error: `listIter' undeclared (first use this function)

    box.cpp: In member function `void box<T>::mergeBox(box<T>)':
    box.cpp:94: error: `tempIter' undeclared (first use this function)
    box.cpp: In member function `void box<T>:rintBox()':
    box.cpp:108: error: `listIter' undeclared (first use this function)

    make.exe: *** [box.o] Error 1

    Execution terminated

  7. #7
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Creating template class

    Oh the irony! Just maybe an hour ago I posted on this same forum with this same problem. On your line in box.h where you define your iterator members, change that line to this:
    Code:
    typename list<T>::iterator listIter,tempIter;
    That should fix that problem.

    Explanation: To avoid ambiguity in syntax for C++, any Qualified, dependent identifier is assumed to be a value, not a type. To explicitly tell C++ that 'list<T>::iterator' refers to a type, we must use the keyword 'typename'
    Last edited by Etherous; March 21st, 2009 at 02:41 PM.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  8. #8
    Join Date
    Mar 2009
    Posts
    7

    Unhappy Re: Creating template class

    great! that fixed it!

    now I am getting linker errors. I think may not be calling box with the right syntax in my main

    main.o(.text+0x187):main.cpp: undefined reference to `box<int>::box(int)'
    main.o(.text+0x1a4):main.cpp: undefined reference to `box<int>::addItem(int)'
    main.o(.text+0x1b7):main.cpp: undefined reference to `box<int>::addItem(int)'
    main.o(.text+0x1ca):main.cpp: undefined reference to `box<int>::addItem(int)'
    main.o(.text+0x1dd):main.cpp: undefined reference to `box<int>::addItem(int)'
    main.o(.text+0x1f0):main.cpp: undefined reference to `box<int>::addItem(int)'

    main.o(.text+0x203):main.cpp: more undefined references to `box<int>::addItem(int)' follow
    main.o(.text+0x20e):main.cpp: undefined reference to `box<int>:rintBox()'
    main.o(.text+0x235):main.cpp: undefined reference to `box<int>::checkReps(int)'
    main.o(.text+0x284):main.cpp: undefined reference to `box<int>::box(int)'
    main.o(.text+0x2a1):main.cpp: undefined reference to `box<int>::addItem(int)'
    main.o(.text+0x2b4):main.cpp: undefined reference to `box<int>::addItem(int)'
    main.o(.text+0x2c7):main.cpp: undefined reference to `box<int>::addItem(int)'
    main.o(.text+0x2da):main.cpp: undefined reference to `box<int>::addItem(int)'
    main.o(.text+0x2e5):main.cpp: undefined reference to `box<int>:rintBox()'
    main.o(.text+0x319):main.cpp: undefined reference to `box<int>::mergeBox(box<int>)'
    main.o(.text+0x3a8):main.cpp: undefined reference to `box<int>:rintBox()'

    main.o(.text+0x3bb):main.cpp: undefined reference to `box<int>::removeItem(int)'
    main.o(.text+0x3c6):main.cpp: undefined reference to `box<int>:rintBox()'
    collect2: ld returned 1 exit status

    make.exe: *** [box.exe] Error 1

    Execution terminated

  9. #9
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Creating template class

    Put your template implementations into the header or include them from the header.
    Kurt

  10. #10
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Creating template class

    Right. You have two options here:
    1) (Easiest):
    Include the box.cpp from either box.h or main.cpp, after the class itself is defined. I personally prefer the latter.

    2) (More "Proper"):
    Compile box.h and box.cpp as a library object (.o, .so, .a, .dll, .lib, etc.) and when you build the main.cpp, just include box.h. Then in the linking process, link main.o with box.o (or whatever extensions apply). This form is harder, but is considered more "proper" by "experts". Either way will work, though

    Edit: Actually, Zuk has me here. Form two is out of the question here because, as he says, when it comes to templates, nothing is 'created' until it's used, so it's pointless to compile it separately.
    Last edited by Etherous; March 21st, 2009 at 03:30 PM.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  11. #11
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Creating template class

    You cannot compile box.cpp. After all it is no actual code it's just a template and only becomes code when it is instantiated with some type.
    Kurt

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