Public Group

# Passing Vectors to Functions

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

## 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 on other sites

#pragma warning(disable:4786)

##### 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 on other sites
Quote:
 Original post by v0dKAIs 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 on other sites

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 on other sites
you shouldnt have any problems sending filestreams (or any other objects for that matter) as reference.

##### Share on other sites
Quote:
 Original post by v0dKAA 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.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 20
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632145
• Total Posts
3004381

×