Reputation: 1459
I read related posts but still cannot figure it out. In my .h file, I defined a template class:
template <typename P, typename V>
class Item {
public:
P priority;
V value;
Item(P priority, V value): priority(priority), value(value){}
};
In my main function, I tried to make a vector of Items with specific type.
Item<int, string> Item1(18, "string 1");
Item<int, string> Item2(16, "string 2");
Item<int, string> Item3(12, "string 3");
Item<int, string> Item[3] = {Item1, Item2, Item3}
vector<Item<int, string> > Items(Item, Item + 3);
But I keep getting compile error saying:
expected '(' for function-style cast or type construction
vector<Item<int, string> > Items(Item, Item + 9);
~~~^
Upvotes: 1
Views: 485
Reputation: 5741
Here's working code
#include<iostream>
#include<vector>
using namespace std;
template <typename P, typename V>
class Item
{
public:
P priority;
V value;
Item(P priority, V value): priority(priority), value(value) {}
};
int main()
{
Item<int, string> Item1(18, "string 1");
Item<int, string> Item2(16, "string 2");
Item<int, string> Item3(12, "string 3");
Item<int, string> ItemL[3] = {Item1, Item2, Item3};
vector<Item<int, string> > Items(ItemL, ItemL+3);
}
You have several problem:
Item<int, string> Item[3] = {Item1, Item2, Item3}
lineItem<int, string> Item[3]
line Your class name Item
and array of Item named Item
ambiguous. So rename it other name, I rename it as ItemL
Upvotes: 1