Class Template and Variable Constructor?

My current code is at http://codepad.org/Eyo42441

I have a class template OListType. I want to create an instance of this class that will hold variable input depending on what type of input the user wants to store in the array. I have it set up to have three different data types to be held in the array (char, int, and string), and depending on user input it will create it - no problem yet. Later on in the program to test the class, I get hit with an error every time I try to call one of the class functions, with the error message I receive being "error: 'items' undeclared identifier".

My question is, is it even POSSIBLE to have a class template accept and manipulate various types of data without having to basically write a separate case for every single type of data I want it to use?
Scope (the curly braces were added to illustrate the problem, they do not change your code)
1
2
3
4
5
6
7
8
9
        if(num == 1){
         OListType<char> *items = new OListType<char>(100);
         } //items of char die here.
        else if(num == 2){
         OListType<int> *items = new OListType<int>(100);
         }//items of int die here
        else if(num == 3){
         OListType<string> *items = new OListType<string>(100);
         }//items of string (?) die here 
Maybe you could use something like this http://www.cplusplus.com/forum/beginner/36370/#msg196845 not good, you could not templatize a virtual method
Last edited on
The whole point of templates is to seperate implementation from type. I haven't read your code but whether or not this is possible depends entirely on the functionality of your class. If it is not possible however, then a template class is not a suitable design solution.
One solution could be put all the menu code (after "create a list") in a templated function.
Thanks for catching the scope thing ne - I was in a meeting when I got an email about your response, and a couple of funny looks when I facepalmed. What I was hoping to do, was declare an OListType pointer globally, without having to assign it to a particular datatype, that way I could do the instantiation using JUST that.
Is this so bad?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template<class T> void foo(); //sorry for the name

int main(){
	cout << "\nWhat type of data would you like the list to hold?: ";
	cout << "\n1. Char\n";
	cout << "2. Int\n";
	cout << "3. Float\n";
	cout << "Please make your selection: ";
	cin >> num;
	
	switch(num){
	case Type_int /*an enum*/: foo<int>(); break;
	case Type_char : foo<char>(); break;
	case Type_string : foo<std::string>(); break;
	default : std::cout << "Invalid selection.\n";
	}
}

template<class T>
void foo(){ 
	OListType<T> items(100); //an object, not a pointer
	int choice;
	
	cout << cat("menu.txt");
	while( cin>>choice )
		switch(choice){
		//do what you want with your list
		}
}

Note: cat is a function that returns a string with the content of the file passed. (you need to implement it)
ne,

I tried putting the constructors in a switch statement, but get an error that initialization of 'items' is skipped by 'case' label. Are you suggesting with the code to basically get the data type of the list first and create it, then put everything else nested within that level so that it doesn't go out of scope?
Ok...kinda starting over from scratch here... here is my class - through the constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class T> 
class OListType {
  private:
    T *items;
    int count;
    int maxsize;
  public:



    OListType<T>::OListType( int size_t) {
    maxsize = size_t;
    count = 0;
    T *items[size_t];
    }



What I'm wanting to do is use one variable name (in this case 'items'), and feed it into the constructor. The constructor would then create an object of an array of items of type T. I'm not allowed to create extra functions for this.


Attempt 1: Tried using OListType<T> *items; for a declaration of *items pointing at an OListType<T> object.
Result 1: T isn't declared so it's a no go.

Attempt 2: Switch statement.
Lesson learned: Can't put constructors in switch statements unless they're within brackets - however as soon as the block is done, they go out of scope.


Thoughts: If there was a way to set T to 'int' 'char' or 'string' (the data types I've decided to use), it might work. But is that possible?

Exactly.
With the type you know what function to call. That function is responsible to create the proper list, and to handle the operations on that list.
Again, scope
1
2
3
4
5
    OListType<T>::OListType( int size_t) {
    maxsize = size_t;
    count = 0;
    T *items[size_t]; //local variable items
    } //local variable dies 

I'm not allowed to create extra functions for this.
Sorry, but that is an stupid restriction.

cout << "2. Insert an element into the list.\n"; For now, don't think in the list. How could you fulfil this requirement? How could you read the element?
items[element] would read the element. So items[0] would read the first, items[1] second, etc.
No, I mean items.insert(element); how do you construct element? (not in the list, in main)
Last edited on
1
2
cin >> num;
items->insert(num);


Is how I call the insert function. Is that you're meaning?

But what is the type of num? (it should correspond with your list choice)
Topic archived. No new replies allowed.