templated vector build-up

Discussion in 'C++' started by newbie, Mar 1, 2007.

  1. newbie

    newbie Guest

    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?
    newbie, Mar 1, 2007
    #1
    1. Advertising

  2. newbie

    newbie Guest

    On Mar 1, 1:18 pm, "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);
    > } 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
    newbie, Mar 1, 2007
    #2
    1. Advertising

  3. 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);
    > } 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
    John Harrison, Mar 1, 2007
    #3
  4. newbie

    mlimber Guest

    On Mar 1, 4:18 pm, "newbie" <> 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);
    > } 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
    mlimber, Mar 1, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA Scheltema
    Replies:
    3
    Views:
    394
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    454
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,227
  4. Replies:
    8
    Views:
    1,914
    Csaba
    Feb 18, 2006
  5. Amadeus W. M.
    Replies:
    2
    Views:
    391
    Amadeus W. M.
    Jul 4, 2006
Loading...

Share This Page