templated std::vector variable as an argument by reference problem

Discussion in 'C++' started by mast2as@yahoo.com, Mar 2, 2007.

  1. Guest

    sorry i am too sure how to write a more explicit subject but this code
    doesn't compile for the type string and I am not sure why (and I am
    not sure either how to describe the problem but by looking at the
    program you should understand what I am trying to do easily, which is
    convert an array of string to an array of another type, either string,
    float or interger.)

    I am not sure what I am trying to do is legal. Obvisouly it doesn't
    seem to be as the compiler complains but I wonder if there's a way i
    can get it to work ?

    Thanks for your help.

    -mark

    template<typename T>
    void GetArray( std::vector<T> &array )
    {
    std::vector<std::string> strArray;
    strArray.push_back( "test1" );
    strArray.push_back( "test2" );

    if ( typeid( std::string ) == typeid( T ) )
    {

    for ( size_t i = 0; i < strArray.size(); ++i )
    {
    // DOESN'T WORK ??? <<< REFUSE TO COMPILE IF THE NEXT LINE IS
    COMMENTED OUT!
    //array.push_back( strArray );
    }
    }

    std::vector<std::string> intArray;
    intArray.push_back( "1" );
    intArray.push_back( "2" );

    if ( typeid( int ) == typeid( T ) )
    {
    for ( size_t i = 0; i < intArray.size(); ++i )
    {
    array.push_back( atoi( intArray.c_str() ) );
    }
    }

    }

    int main()
    {
    //std::vector<std::string> strArr;
    //GetArray( strArr );

    std::vector<int> intArr;
    GetArray<int>( intArr );

    return 0;
    }

    /////

    error: no matching function for call to 'std::vector<int,
    std::allocator<int> >::push_back(std::basic_string<char,
    std::char_traits<char>, std::allocator<char> >&)'
    /usr/include/c++/4.0.0/bits/stl_vector.h:602: note: candidates are:
    void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int,
    _Alloc = std::allocator<int>]

    ////
     
    , Mar 2, 2007
    #1
    1. Advertising

  2. Guest

    hum clearly what i am trying to do in the code is completly stupid, as
    of course if at runtime we call GetArray with a
    std::vector<std::string> argument the compiler still tries to make
    sense of the code where numbers or pushed to this array. So it
    compiles fines for numbers but then when i write the code for string
    there's a mismatch (the same array can't be used to push string &
    numbers).

    Anyway... if anyone has in mind a mechanism that would allo me to do
    that be great. Right now the only solution i see if to return a vector
    of strings and the type of the token which the strings encode (strings
    or number), then create a vector of that type (vector<int> or
    vector<string> and call a function that converts an array of string to
    an array of integers...

    i was just hoping for something a bit smarter... ;-)
     
    , Mar 2, 2007
    #2
    1. Advertising

  3. * :
    > sorry i am too sure how to write a more explicit subject but this code
    > doesn't compile for the type string and I am not sure why (and I am
    > not sure either how to describe the problem but by looking at the
    > program you should understand what I am trying to do easily, which is
    > convert an array of string to an array of another type, either string,
    > float or interger.)
    >
    > I am not sure what I am trying to do is legal. Obvisouly it doesn't
    > seem to be as the compiler complains but I wonder if there's a way i
    > can get it to work ?
    >
    > Thanks for your help.
    >
    > -mark
    >
    > template<typename T>
    > void GetArray( std::vector<T> &array )
    > {
    > std::vector<std::string> strArray;
    > strArray.push_back( "test1" );
    > strArray.push_back( "test2" );
    >
    > if ( typeid( std::string ) == typeid( T ) )
    > {
    >
    > for ( size_t i = 0; i < strArray.size(); ++i )
    > {
    > // DOESN'T WORK ??? <<< REFUSE TO COMPILE IF THE NEXT LINE IS
    > COMMENTED OUT!
    > //array.push_back( strArray );
    > }
    > }


    The code above is compiled also for a type T that isn't std::string.

    Instead of 'if' you can use a specialization of GetArray for type
    std::string.


    > std::vector<std::string> intArray;
    > intArray.push_back( "1" );
    > intArray.push_back( "2" );
    >
    > if ( typeid( int ) == typeid( T ) )
    > {
    > for ( size_t i = 0; i < intArray.size(); ++i )
    > {
    > array.push_back( atoi( intArray.c_str() ) );
    > }
    > }


    The code above is compiled also for a type T that isn't integer.

    Instead of 'if' and 'atoi' you can use a 'boost::lexical_cast' as a
    generic implementation.


    > }



    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 2, 2007
    #3
  4. iftekhar Guest

    you can use partial specialization.

    wrote:
    > hum clearly what i am trying to do in the code is completly stupid, as
    > of course if at runtime we call GetArray with a
    > std::vector<std::string> argument the compiler still tries to make
    > sense of the code where numbers or pushed to this array. So it
    > compiles fines for numbers but then when i write the code for string
    > there's a mismatch (the same array can't be used to push string &
    > numbers).
    >
    > Anyway... if anyone has in mind a mechanism that would allo me to do
    > that be great. Right now the only solution i see if to return a vector
    > of strings and the type of the token which the strings encode (strings
    > or number), then create a vector of that type (vector<int> or
    > vector<string> and call a function that converts an array of string to
    > an array of integers...
    >
    > i was just hoping for something a bit smarter... ;-)
     
    iftekhar, Mar 2, 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. Anonymous
    Replies:
    20
    Views:
    4,310
    Pete Becker
    Mar 30, 2005
  2. Jason Heyes
    Replies:
    8
    Views:
    731
    Andrew Koenig
    Jan 15, 2006
  3. Replies:
    8
    Views:
    1,932
    Csaba
    Feb 18, 2006
  4. Replies:
    3
    Views:
    3,062
  5. Rune Allnor
    Replies:
    4
    Views:
    955
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page