Jump to content
  • Advertisement
Sign in to follow this  
Shakedown

How do I pass a vector through a parameter?

This topic is 4061 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

Here's my deal. I have a function that generates some objects (enemies) and stores them in a vector. Here's the code for that:
void GenEnemies()
{
     
     int GenEn = 0;
     int aa;                                 // Temp enemy health
     int bb;                                 // Temp enemy attack
     int cc;                                 // Temp enemy defense
     
     system("CLS");
     
     cout << "How many enemies would you like to generate? ";
     cin >> GenEn;
     
     vector<CCharacter> vGenEn( GenEn );
     
     srand( time( NULL ) );
     
     for ( int i = --GenEn; i >= 0; --i )
     {
         randomStats( aa, bb, cc );
         vGenEn.setAll( aa, bb, cc );
         vGenEn.setName( enemyname[ rand() % 10 ] );
         vGenEn.displayStats();
         cout << endl;
         }


This all works fine. My question is now how do I pass the vector vGenEn, including all of its elements (which are objects of CCharacter) on to another function? I want my next function to be able to access the vector vGenEn, and then display the names assigned to each object in each element of the vector. Here's what I have so far for my next function:
Fight( vGenEn, GenEn );
         
}

void Fight( CCharacter vGenEn, int GenEn )
{
     system("CLS");
     
     cout << "\tTime to FIGHT!" << endl;
     cout << "Here are your enemies: ";
     
     for( int k = --GenEn; k >= 0; --k )
     {
          cout << "\t";
          vGenEn[k].displayName;
          }
          
     system("PAUSE");
          
}

I feel like I'm omitting some iterators/pointers...but I'm not sure what to do. Thanks for any help! [edit: Added source tags - ApochPiQ] [Edited by - ApochPiQ on May 13, 2007 2:07:21 PM]

Share this post


Link to post
Share on other sites
Advertisement
If you don't mind making (or specifically want to make) a copy of the original vector (something that could waste extra CPU time and memory if you didn't really want it), then you can do this:
void Fight( vector<CCharacter> vGenEn, int GenEn )

If you want to avoid the copy, you can pass by const reference instead:
void Fight( const vector<CCharacter>& vGenEn, int GenEn )

If you want to also be able to modify the vector within the function, remove the const:
void Fight( vector<CCharacter>& vGenEn, int GenEn )

Share this post


Link to post
Share on other sites
K so here's what I tried:

Fight( vector<CCharacter>& vGenEn, int GenEn );

}

void Fight( vector<CCharacter>& vGenEn, int GenEn )
{
system("CLS");

cout << "\tTime to FIGHT!" << endl;
cout << "Here are your enemies: ";

for( int k = --GenEn; k >= 0; --k )
{
cout << "\t";
vGenEn[k].displayName;
}




But it gives me "expected primary-expression before '&' token " and "expected primary-expression before "int" " errors. I changed the protoype as well so that shouldn't be a problem for anything. Also, it doesn't seem to like me accessing the vector "statement cannot resolve address of overloaded function." Don't I need to use an iterator/pointer in Fight function to go through the vector and access the names of the objects?

[edit: Added source tags - ApochPiQ]

[Edited by - ApochPiQ on May 13, 2007 2:56:29 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Shakedown
K so here's what I tried:

Fight( vector<CCharacter>& vGenEn, int GenEn );

}
...


You probably already know that is not how you call a function, and perhaps you meant this?
     Fight( vGenEn, GenEn ); 


BTW, my usual convention is to use a pointer when I want to modify the parameter and a reference-to-const when I don't. That way, when the function is called, it is clearer which parameters are going to be modified and which aren't. For example,
    void foo( Bar * modified, Bar const & unmodified ); 
is called like this:
    Bar b1, b2;
foo( &b1, b2 );

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
Quote:
Original post by Shakedown
K so here's what I tried:

Fight( vector<CCharacter>& vGenEn, int GenEn );

}
...


You probably already know that is not how you call a function, and perhaps you meant this?
     Fight( vGenEn, GenEn ); 


I tried that, but it didn't work and Agony suggested I use Fight( vector<CCharacter>& vGenEn, int GenEn );

But that doesn't work either.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
You have to declare (or define) a function before it is used. Did you do that?


Ha yes, I said in an earlier post that my protoype was correct.

Share this post


Link to post
Share on other sites
Can you post your actual code again? What you're saying doesn't quite add up.

Share this post


Link to post
Share on other sites
Ok this is what I have, after what Agony suggested:

Prototype:

void Fight( vector<CCharacter> &vGenEn, int GenEn );


Two functions:

void GenEnemies()
{

int GenEn = 0;
int aa; // Temp enemy health
int bb; // Temp enemy attack
int cc; // Temp enemy defense

system("CLS");

cout << "How many enemies would you like to generate? ";
cin >> GenEn;

//vector<CCharacter>::iterator pvGenEn;
vector<CCharacter> vGenEn( GenEn );

srand( time( NULL ) );

for ( int i = --GenEn; i >= 0; --i )
{
randomStats( aa, bb, cc );
vGenEn.setAll( aa, bb, cc );
vGenEn.setName( enemyname[ rand() % 10 ] );
vGenEn.displayStats();
cout << endl;
}

system("PAUSE");

Fight( vector<CCharacter> &vGenEn, int GenEn );

}

void Fight( vector<CCharacter> &vGenEn, int GenEn )
{
system("CLS");

cout << "\tTime to FIGHT!" << endl;
cout << "Here are your enemies: ";

for( int k = --GenEn; k >= 0; --k )
{
cout << "\t";
vGenEn[k].displayName;
}

system("PAUSE");

}



And the problem again:

I want to create a vector of GenEn (Generated Enemies, user defined) number of enemies, each element an object of class CCharacter (Class Character).
Each of these objects is an enemy with 4 stats (Name, Health, Attack, Defense). The function GenEn() randomly chooses a name for each of the enemies, then randomly assigns values to each of their attributes (Health, Attack, Defense).
(This all works fine)
(Now the problem ... )
Once this is complete, I want to allow the function Fight(...) to be able to access all of these enemies that GenEn created and stored in a vector. Once Fight(...) is able to access the vector, I want it to run through each element of the vector and print the Name of each of the objects.

[edit: Added source tags - ApochPiQ]

Share this post


Link to post
Share on other sites
At the end of the GenEnemies function you have the line "Fight( vector<CCharacter> &vGenEn, int GenEn );". What is that supposed to do? If you actually try to call Fight then the proper syntax is "Fight(vGenEn,GenEn );" You don't have to (and it's an error to) specify the type. If it was supposed to be your prototype then it shouldn't be inside the function, but outside it (not within the scope of any function). Like (all I did was move the line from the end of GenEnemies to the top of the source):
void Fight( vector<CCharacter> &vGenEn, int GenEn );

void GenEnemies()
{

int GenEn = 0;
int aa; // Temp enemy health
int bb; // Temp enemy attack
int cc; // Temp enemy defense

system("CLS");

cout << "How many enemies would you like to generate? ";
cin >> GenEn;

//vector<CCharacter>::iterator pvGenEn;
vector<CCharacter> vGenEn( GenEn );

srand( time( NULL ) );

for ( int i = --GenEn; i >= 0; --i )
{
randomStats( aa, bb, cc );
vGenEn.setAll( aa, bb, cc );
vGenEn.setName( enemyname[ rand() % 10 ] );
vGenEn.displayStats();
cout << endl;
}

system("PAUSE");

}

void Fight( vector<CCharacter> &vGenEn, int GenEn )
{
system("CLS");

cout << "\tTime to FIGHT!" << endl;
cout << "Here are your enemies: ";

for( int k = --GenEn; k >= 0; --k )
{
cout << "\t";
vGenEn[k].displayName;
}

system("PAUSE");

}

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!