Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with Template class and Types

Status
Not open for further replies.

jdf442

Programmer
Oct 17, 2010
10
AU
hey guys, I have created my own templated container class called Container to hold other class objects in my program.

My program has two classes:
1. Numbers
2. Letters

When the program runs the user enters a filename with the firstline being the class type (being a 1 for class type Numbers and a 0 for class type Letters). eg: 1 or 0
The second line has the number of Numbers/Letters. eg: 4
And the last line has the Letters/Numbers seperated by a space. eg: 1 2 3 4 or a b c d

//******* CODE ***********
Code:
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <typeinfo>
#include <string>
#include <iomanip>

using namespace std;

class Numbers
{
  friend istream& operator>>(istream& in, Numbers& num);
private:
     int value;
 public:
        Numbers();
        Numbers(int);      
};

istream& operator>>(istream& in, Numbers& num)
{
 in>>num.value;
 return in;         
}

Numbers::Numbers()
{
 value = 0;    
}

Numbers::Numbers(int val)
{
 if(val < 0)
 {
  cout<<"Error - Must be a number > 0!"<<endl;
  exit(1);     
 }
 else
     value = val;              
}

//**********************************************

class Characters
{
friend istream& operator>>(istream& in, Characters& chars);
 private:
         char value;
 public:
        Characters();
        Characters(char);     
};
 
istream& operator>>(istream& in, Characters& chars)
{
 in>>chars.value;
 return in;         
}

Characters::Characters()
 {
  value = 'a';           
 }
 
 Characters::Characters(char mychar)
 {
  if(!islower(mychar))
  {
   cout<<"Error - Must be a lowercase letter!"<<endl;
   exit(1);                  
  }
  value = mychar;               
 }

//**********************************************

template<class T>
class Container
{
 private:
         T *data;
         int size;
 public:
        Container();
        Container(T *d, int s);
        ~Container();
        void display();  
};

template <class T>
Container<T>::Container()
{
 data = NULL;
 size = 0;                    
}

template <class T>
Container<T>::Container(T *d, int s)
{
 data = d;
 size = s;                 
}

template <class T>
Container<T>::~Container()
{
 delete [] data;                        
}

template <class T>
void Container<T>::display()
{
 cout<<"Container data: ";
 for(int i = 0; i < size; i++)
 {
  cout<<data[i]<<" ";        
 }
 cout<<endl;    
}

//************************** MAIN ***************************

int main()
{
 int classType;
 int size;
 char filename[100];

 cout<<"Enter filename: ";
 cin>>filename;
 ifstream input;
 
 input.open(filename);
 if(!input.good())
 {
  cout<<"Error - Invalid filename: "<<filename<<endl;
  exit(1);                 
 }
 
 input>>classType>>size;
 
 if(classType == 0)
 {
  Numbers *temp;
  temp = new Numbers[size];             
 }
 else if(classType == 1)
 {
  Characters *temp;
  temp = new Characters[size];    
 }
 else
 {
  cout<<"Error - Invlaid class type: "<<classType<<", must be 1 or 0!"<<endl;
  exit(1);    
 }
 
 for(int i = 0; i < size; i++)
 {
  input>>temp[i];        
 }
 Container<typeid(temp)> mycontainer;

    return 0;
}

//******* END CODE ***********
I want to be able to open the text file and based on what the classtype is (1 or 0) create an array of objects to read the data from the file into class and then store these class objects (Numbers/Letters) in my containier class so i can proccess them.

I am having trouble with how to declare what type of class it is based on the classtype i read in from the file and also how to declare what the container class is going to hold based on the classtype???????

Does anyone have any suggestions??? You help will be much appreciated!

Cheers!
 
It's not a good policy to post the same message in Unix C++ and Microsoft C++ forums ;)
 
Code:
 if(classType == 0)
 {
  Numbers *temp;
  temp = new Numbers[size];             
 }
 else if(classType == 1)
 {
  Characters *temp;
  temp = new Characters[size];    
 }
This is useless since the temp variables go out of scope after the } and therefore only leaks memory.

Code:
 Container<typeid(temp)> mycontainer;
What do you want to do with the container after this?
Why not pass your temp variable to a function that takes a templated Container<T> reference and does something with it...

Also, don't forget to delete any memory you allocate with new.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top