473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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

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::stringstrArray;
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[i] );
}
}

std::vector<std::stringintArray;
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[i].c_str() ) );
}
}

}

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

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

return 0;
}

/////

error: no matching function for call to 'std::vector<int,
std::allocator<int::push_back(std::basic_string<ch ar,
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 '07 #1
3 1837
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::stringargument 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<intor
vector<stringand 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 '07 #2
* ma*****@yahoo.com:
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::stringstrArray;
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[i] );
}
}
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::stringintArray;
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[i].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?
Mar 2 '07 #3
you can use partial specialization.

ma*****@yahoo.com 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::stringargument 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<intor
vector<stringand 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 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: scooter | last post by:
what happens memory wise with this scenerio: std::vector<int> vecInt(20, 999); VecInt.assign(20, 0); do all the 999 values get overwritten or does the vector reassign the memory lso?
27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
0
by: Jason Heyes | last post by:
I wrote a previous post that asked whether there was a reference-counted implementation of std::vector. Apparantly there wasn't. So my next question is, is it possible to write your own shared...
18
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
7
by: digz | last post by:
template<typename _Tp, typename _Alloc> void vector<_Tp, _Alloc>:: _M_insert_aux(iterator __position, const _Tp& __x) { if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) {...
23
by: Mike -- Email Ignored | last post by:
In std::vector, is reserve or resize required? On: Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri Jul 27 18:10:34 EDT 2007 i686 athlon i386 GNU/Linux Using: g++ (GCC) 4.1.2 20070502 (Red Hat...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.