Thread: an exercise gone wrong

  1. #1
    Registered User GiraffeMan's Avatar
    Join Date
    Feb 2002
    Posts
    20

    Angry an exercise gone wrong

    well then, ppl.. we got an exercise in class to make a one way connected list that will have different objects in it; ...
    in this case a string and a box, but the box has to be a template class and give the user a choice of int, or float...
    well I've done all that but for some odd reason my compiler keeps prompting up this error line:

    "undefined symbols newbox<int>::setbox() in module First.cpp"

    could you please take a look at this code and tell me why???

    header file
    Code:
    #ifndef proj
    #define proj
    
    #include <iostream.h>
    #include <string.h>
    
    class block
    {
     public:
     block *next;
     block(){next=NULL;}
     void setnexto(block *nblock){next=nblock;}
     virtual void show()=0;
    };
    
    class list
    {
     block *head,*tail;
     int size;
     public:
     list(){size=0; head=tail=NULL;}
     void add(block *nblock);
     void print();
     void clear();
    };
    
    class string
    {
     public:
     char *ch;
     string(){ch=new char[40];}
     friend istream &operator>>(istream &in, string &a);
     friend ostream &operator<<(ostream &out, string a);
     void setstring(char *str);
    };
    
    template <class T>
    class box
    {
     public:
     T a,b,h;
     box(){}
     box(T _a,T _b,T _h):a(_a),b(_b),h(_h){}
     T volume(){return a*b*h;}
     T sides(){return (a*b+a*h+b*h);}
    };
    
    class newstring:public string, public block
    {
     public:
     newstring():block(),string(){}
     void show();
    };
    
    template <class T>
    class newbox:public box<T>, public block
    {
     public:
     newbox():block(),box<T>(){}
     void setbox();
     void show();
    };
    
    #endif

    defenitions file .cpp
    Code:
    #include "New.h"
    
    istream &operator>>(istream &in, string &a)
    {
    	cout<<"Enter string: ";
    	cin>>a.ch;
    	return in;
    }
    
    template <class T>
    void newbox<T>::setbox()
    {
     cout<<"\nenter a: ";
     cin>>a;
     cout<<"\nenter b: ";
     cin>>b;
     cout<<"\nenter h: ";
     cin>>h;
    }
    
    template <class T>
    void newbox<T>::show()
    {
     cout<<"\nbox is:";
     if(sizeof(T)==sizeof(float)) cout<<" float";
     else cout<<" int";
     cout<<"\na: "<<a;
     cout<<"\nb: "<<b;
     cout<<"\nh: "<<h;
     cout<<"\nvolume: "<<volume();
     cout<<"\nsides:  "<<sides();
    }
    
    void newstring::show()
    {
     cout<<"\nstring is: "<<ch;
    }
    
    void string::setstring(char *str)
    {
     strcpy(ch,str);
    }
    
    void list::clear()
    {
     block *p;
     for(p=head;p;p=p->next) {delete p; cout<<"\ndeleted one";}
    }
    
    void list::add(block *nblock)
    {
     size++;
     if(head)
      {
       tail->next=nblock;
       tail=nblock;
      }
     else
       tail=head=nblock;
    }
    
    void list::print()
    {
     block *p;
     if(head)
      {
       cout<<"There are "<<size<<" objects in this list"<<endl;
       for(p=head;p;p=p->next) {p->show();}
      }
     else
       cout<<"The list is empty"<<endl;
    }

    main file .cpp also called First.cpp
    Code:
    #include "New.h"
    
    void main()
    {
     int flag=1;
     newstring *p;
     newbox<int> *nb;
     newbox<float> *nf;
     list l;
     char key;
     while(flag)
    	{
    		cout<<"\n";
    		cout<<"enter your choice\n";
    		cout<<"1. add box (float)\n";
    		cout<<"2. add box (int)\n";
    		cout<<"3. add string\n";
    		cout<<"4. printout list\n";
    		cout<<"5. exit\n";
    		cin>>key;
    		switch(key)
    		{
    		case '1':
    			nf=new newbox<float>;
    			nf->setbox();
    			l.add(nf);
    			break;
    		case '2':
    			nb=new newbox<int>;
    			nb->setbox();
    			l.add(nb);
    			break;
    		case '3':
    			p=new newstring();
    			cin>>*p;
    			l.add(p);
    			break;
    		case '4':
    			l.print();
    			break;
    		case '5':
    			l.clear();
    			flag=0;
    			break;
    		default: break;
    		}
    	}
    }
    THE GIRAFFE MAN
    "our heads in the skies, but our feet on the ground"....

    http://dagan.150m.com

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    12
    your main file (First.cpp) should #include "New.cpp" , not New.h

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    your main file (First.cpp) should #include "New.cpp" , not New.h
    I don't agree

    The beginning of your .h file has
    Code:
    #ifndef proj
    #define proj
    
    #include <iostream.h>//Put these in your .cpp file
    #include <string.h>//As above
    
    class block
    //...
    get rid of "void main" and use "int main".
    Then include your system libraries (iostream.h etc.) into the main file.

    I only briefly viewed your source.

    kwigibo
    Last edited by kwigibo; 04-08-2002 at 11:35 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Location
    South Africa
    Posts
    35
    Unless i have it horribly wrong, here is your problem:
    Because you are using a template, the compiler has to generate the code (the actual class definition used to instantiate objects with) at compile time.
    In order to do this, the client code (the file that uses the template class) has to have access to all of the source code for the template class.
    In other words, either put the implimentation code (.cpp file) in the header, or include it as well (in main).

  5. #5
    Registered User GiraffeMan's Avatar
    Join Date
    Feb 2002
    Posts
    20
    yo thanks Koedoe man.. you really saved my ass, you were right ..
    I inserted the defenition .cpp file into the header and everything works fine.. though I am having a bit of a problem understanding that..
    do you mean to say that when ever I use a template I can not use it in another cpp file???
    is there a way to declare a template class in a header file and define it in another cpp file (not main)>>???
    THE GIRAFFE MAN
    "our heads in the skies, but our feet on the ground"....

    http://dagan.150m.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone check my code and tell me what I'm doing wrong
    By jlmac2001 in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2003, 08:14 PM
  2. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  4. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM
  5. What did i do wrong? Code inside
    By The Rookie in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 08:14 AM