templated vector build-up

N

newbie

Thanks for helping on this problem

suppose I have the following class
class MyContent {

vector<MyType1> v1;
vector<MyType2> v2;
}

And I have a templated function
template <class Type> foo(vector<Tpye>& a, int type_code, MyContent&
content) {

if(type_code) {
//insert elements in 'a' into content.v1
for(int i = 0; i < a.size(); i++)
content.v1.push_back(a);
} else {
// insert elements in 'a' into content.v2
for(int i = 0; i < a.size(); i++)
content.v2.push_back(a);
}

}

But the compiler complains unmatched type!! How can I solve it?
 
N

newbie

Thanks for helping on this problem

suppose I have the following class
class MyContent {

vector<MyType1> v1;
vector<MyType2> v2;

}

And I have a templated function
template <class Type> foo(vector<Tpye>& a, int type_code, MyContent&
content) {

if(type_code) {
//insert elements in 'a' into content.v1
for(int i = 0; i < a.size(); i++)
content.v1.push_back(a);
} else {
// insert elements in 'a' into content.v2
for(int i = 0; i < a.size(); i++)
content.v2.push_back(a);
}

}

But the compiler complains unmatched type!! How can I solve it?


I should mention that: Type passed into this function will always be
either MyType1 or MyType2.
Thanks
 
J

John Harrison

newbie said:
Thanks for helping on this problem

suppose I have the following class
class MyContent {

vector<MyType1> v1;
vector<MyType2> v2;
}

And I have a templated function
template <class Type> foo(vector<Tpye>& a, int type_code, MyContent&
content) {

if(type_code) {
//insert elements in 'a' into content.v1
for(int i = 0; i < a.size(); i++)
content.v1.push_back(a);
} else {
// insert elements in 'a' into content.v2
for(int i = 0; i < a.size(); i++)
content.v2.push_back(a);
}

}

But the compiler complains unmatched type!! How can I solve it?


You should post the code that calls the template function. You should
post the exact error message. And you should post the line number that
the error message refers to.

It possible you should really post a complete program, that will get
your question answered in seconds flat.

john
 
M

mlimber

Thanks for helping on this problem

But we haven't done anything yet. ;-)
suppose I have the following class
class MyContent {

vector<MyType1> v1;
vector<MyType2> v2;

}

And I have a templated function
template <class Type> foo(vector<Tpye>& a, int type_code, MyContent&
content) {

if(type_code) {
//insert elements in 'a' into content.v1
for(int i = 0; i < a.size(); i++)
content.v1.push_back(a);
} else {
// insert elements in 'a' into content.v2
for(int i = 0; i < a.size(); i++)
content.v2.push_back(a);
}

}

But the compiler complains unmatched type!! How can I solve it?


The problem is that when the function is instantiated, "a" is
(presumably) either of MyType1 or MyType2. The former cannot be put in
v2 and the latter cannot be put in v1, but clearly you are trying to
do just that. Remember, all of the code in the function must compile,
regardless of what path will actually be taken at run-time.

Try this:

class MyContent
{
vector<MyType1> v1;
vector<MyType2> v2;
public:
void Add( const MyType1& a ) { v1.push_back( a ); }
void Add( const MyType2& a ) { v2.push_back( a ); }
}

template <class Type>
void foo(
const vector<Type>& a,
MyContent& content )
{
//insert elements in 'a' into content.v1
for(int i = 0; i < a.size(); i++)
content.Add( a );
}

Note that type_code is gone. Static and dynamic polymorphism are
intended to replace such "type switch" constructs.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top