Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > templated vector build-up

Reply
Thread Tools

templated vector build-up

 
 
newbie
Guest
Posts: n/a
 
      03-01-2007
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[i]);
} else {
// insert elements in 'a' into content.v2
for(int i = 0; i < a.size(); i++)
content.v2.push_back(a[i]);
}

}

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

 
Reply With Quote
 
 
 
 
newbie
Guest
Posts: n/a
 
      03-01-2007
On Mar 1, 1:18 pm, "newbie" <(E-Mail Removed)> wrote:
> 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[i]);
> } else {
> // insert elements in 'a' into content.v2
> for(int i = 0; i < a.size(); i++)
> content.v2.push_back(a[i]);
> }
>
> }
>
> 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

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-01-2007
newbie wrote:
> 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[i]);
> } else {
> // insert elements in 'a' into content.v2
> for(int i = 0; i < a.size(); i++)
> content.v2.push_back(a[i]);
> }
>
> }
>
> 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
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      03-01-2007
On Mar 1, 4:18 pm, "newbie" <(E-Mail Removed)> wrote:
> 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[i]);
> } else {
> // insert elements in 'a' into content.v2
> for(int i = 0; i < a.size(); i++)
> content.v2.push_back(a[i]);
> }
>
> }
>
> 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[i] );
}

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

Cheers! --M

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
templated function as parameter of another templated function Amadeus W. M. C++ 2 07-04-2006 09:59 PM
ASP.NET Templated User Controls - Limit child controls allowable within a templated control JohnyStyles@gmail.com ASP .Net 0 05-29-2006 06:00 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments