Vector of Templates Problem
I want to create a vector of templates but I'm getting 2 linker errors.
I am doing the following :
template<typename T>
int main()
{
vector<T> exp;
return 0;
}
I really need to store elements of different types in the same vector. Please help thank you.
Re: Vector of Templates Problem
Re: Vector of Templates Problem
Quote:
Originally Posted by
VictorN
I don't know anything about boost so can u provide code on how to use it to make vector which can hold any data type using the code I mentioned in the post
Re: Vector of Templates Problem
Quote:
I really need to store elements of different types in the same vector.
Why? What are you trying to accomplish? c++ is a strongly typed language so mixing types within the same variable (whether a container or basic type) is not really supported by the languauge.
If these different types are related - eg different derived classes from a base class then this is fairly simple. If the types are not related then it is a whole different scenario. The easiest way to do this is to use Boost any or Boost variant. If you're not using Boost then you'll need to 'roll your own'. This is doable but non-trivial.
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
I don't know anything about boost ...
I don't it either...
The main point is you cannot "make vector which can hold any data type". Period.
You could, however, make vector which can hold pointers to "any data type" if you cast these pointers to something like void*
Re: Vector of Templates Problem
Quote:
Originally Posted by
VictorN
I don't it either...
The main point is you cannot "make vector which can hold any data type". Period.
You could, however, make vector which can hold pointers to "any data type" if you cast these pointers to something like void*
and some way of knowing to what data type to re-cast these void* pointers when they are needed to be used.
Re: Vector of Templates Problem
Quote:
Originally Posted by
2kaud
and some way of knowing to what data type to re-cast these void* pointers when they are needed to be used.
Tell me how please with the code example
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
Tell me how please with the code example
Possibly in the next couple of days when I have some time I'll take a look.
But you haven't answered my question in post #4. Why do you want to this? What are you trying to accomplish?
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
template<typename T>
int main()
{
vector<T> exp;
return 0;
}
templatizing main() ? ? ?
Re: Vector of Templates Problem
Quote:
Originally Posted by
OReubens
templatizing main() ? ? ?
That's how we create templates I guess but I need a vector which supports all data types and can store them
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
... I need a vector which supports all data types and can store them
Then, please, explain why you need "a vector which supports all data types".
What are these types?
And, BTW, what linker errors did you get?
Re: Vector of Templates Problem
Quote:
Originally Posted by
Fateslayer
That's how we create templates I guess but I need a vector which supports all data types and can store them
main() cannot be used with a template - hence the errors!
but you still haven't explained why and what you are trying to achieve. :(
Re: Vector of Templates Problem
Quote:
Originally Posted by
2kaud
main() cannot be used with a template - hence the errors!
but you still haven't explained why and what you are trying to achieve. :(
I think it's okay to use templates with main() function. I don't know if there is any limitations on where I can use it.
Anyways I'm trying to read a mathematical expression into a vector. First I take the expression into a string for example
(5+5)*9
As you can see there are two data types in this expression the first is double or int for numbers and second is char for operators like +, -, %, (,/ etc. Copying the string into vector is not a problem nor converting the string numbers into double. The problem is vector can only hold data of same type hence I was trying to use templates with vector so that I can use it to store both numbers and characters. That's all I wanna do to store elements of different data types into single vector
Re: Vector of Templates Problem
What you are trying to do is parse an expression and then evaluate it. There are various ways of doing this in c++ but none require a vector that holds different types.
Have a look at
http://stackoverflow.com/questions/6...pressions-in-c
http://stackoverflow.com/questions/1...ression-in-c-c
http://stackoverflow.com/questions/1...ression-parser
http://www.speqmath.com/tutorials/ex...cpp/index.html
and associated links
There are also numerous other internet sites that explain expression parsing and evaluation - just do a search.
Re: Vector of Templates Problem
Quote:
Originally Posted by
2kaud
Thank you so much. I really appreciate it so that means vector which can hold any data type will maybe come in the future versions of c++.