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;
}
}
}