Jump to content
  • Advertisement
Sign in to follow this  
v0dKA

Passing Vectors to Functions

This topic is 5028 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Upgrade your compiler from VC++6, or use


#pragma warning(disable:4786)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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>&);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!