INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Trouble with Template class and Types

Trouble with Template class and Types

(OP)
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!    
 

RE: Trouble with Template class and Types

It's not a good policy to post the same message in Unix C++ and Microsoft C++ forums ;)
 

RE: Trouble with Template class and Types

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close