How to copy a pointer List to another pointer list?

I have a template class containing a pointer to a list, the constructor for this class takes in a pointer List as parameter, but i am stuck in how i copy the parameter to the classes datamember?

1
2
3
4
5
6
7
8
 template<typename T>

class ListManipulator {
private:
	std::list<T> *theList;

public:
	ListManipulator(std::list<T> *aList) : theList(aList);


Your code looks correct except that you need a constructor body instead of a semicolon.

1
2
3
4
5
6
7
template<typename T>
class ListManipulator {
private:
	std::list<T> *theList;
public:
	ListManipulator(std::list<T> *aList) : theList(aList) {}
};
Still cannot initialize it from my UI:

1
2
3
4
5
6
7
8
9
10
class UserInterface
{
private:
	template<typename T>
	ListManipulator<T> myList;
public:

	void chooseList();

};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void UserInterface::chooseList()
{
	std::list<int> *intList;
	std::list<double> *doubleList;
	int choice;
	std::cout << "Choose type of list." << std::endl
		<< "1. Int." << std::endl
		<< "2. Double." << std::endl;

	std::cin >> choice;
	std::cin.get();

	switch (choice)

		case 1: myList = ListManipulator<int>(intList);
			break;


Saying myList ist not defined..
This part is not correct.

1
2
template<typename T>
ListManipulator<T> myList;

Which type of ListManipulator do you want UserInterface to contain. A ListManipulator<int>, a ListManipulator<double>, you need to pick one.

1
2
// E.g.
ListManipulator<int> myList;
Last edited on
Ouu, so i can not choose it by throwing in the type of list I want?

I need to let the user choose what kind of list they want, and fillthis list with random number. The usual guessing game but here the jser first chooses int oe double. So i have to have two datamemvers of ListManipulator in UserInterface to make this happen?
If you are familiar with inheritance and virtual functions you might be able to accomplish something that is relatively close to what you just tried by creating a base class that every ListManipulator<T> inherit from. That way you could let your UserInterface contain a base pointer that could point to any ListManipulator<T>, but that only works if all of them have the same interface.

So i have to have two datamemvers of ListManipulator in UserInterface to make this happen?

That is one way of doing it. You will probably end up with some code repetition but it should work.
Thank you Peter. I tried this, but myList still comes up as "undefined". I did let ListManipulator be the base and created 2 derived classes from it for Int and Double. UI uses ListManipulator, but its object stays as undefined.
What do you mean by undefined?

Whenever you declare a pointer you must make it point to something before you can use it to call functions or access data.
Yes but myList is no pointer?
OK, but then it will not be able to point to objects of the derived classes.
Topic archived. No new replies allowed.