Passing Vectors to Functions

Started by
5 comments, last by Fruny 19 years, 3 months ago
I get some scary warning messages when I attempt to pass a vector to a function. Consider this snippet:

void Function( std::vector<std::string>* SomeVector );
...
int main()
{
...
std::vector<std::string> aVector;
aVector.push_back( string1 );
aVector.push_back( string2 );
aVector.push_back( string3 );
Function( &aVector );
...
return 0;
}
...
void Function( std::vector<std::string>* SomeVector )
{
...
return;
}


This gives 4 warnings, which I'm not sure how to deal with. They're quite inexplicable, but here they are: warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char >,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug info rmation warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std: :allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information Two of those warnings point to the end of main(), and the other two point somewhere within the vector include file. Anyone know what this means? Is there a better way to pass pointers to vectors to functions?
.:<<-v0d[KA]->>:.
Advertisement
Upgrade your compiler from VC++6, or use

#pragma warning(disable:4786)
i dont know about those warnings, but why are you sending a pointer to the vector? use a reference, this is what they were made for. you should also make it const unless Function will modify the vector.
FTA, my 2D futuristic action MMORPG
Quote:Original post by v0dKA
Is there a better way to pass pointers to vectors to functions?


Use references when you can, and pointers when you have to.

Use constant references if your not modifying the state of the instance, if you are then use non constant reference e.g.

void Function(const std::vector<std::string>&);void Function(std::vector<std::string>&);
OK, thanks for the advice.

A subquestion, if you will, is there anything wrong with passing references to ifstream or ofstream objects? I remember doing this some time ago, and something was a little odd - I had to set up a dummy function to read an empty line because every time I passed an ifstream object to a function, its pointer either incremented or decremented, but it pointed to the wrong line. I deleted the project and wrote a new one, so I can't quite dig it up.
.:<<-v0d[KA]->>:.
you shouldnt have any problems sending filestreams (or any other objects for that matter) as reference.
FTA, my 2D futuristic action MMORPG
Quote:Original post by v0dKA
A subquestion, if you will, is there anything wrong with passing references to ifstream or ofstream objects?


You don't have a choice, stream objects are non-copyable. You cannot pass them by value.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement