Feb 5, 2013 at 8:03am Feb 5, 2013 at 8:03am UTC
Hey all, I've been writing a program for a week. I have a class with private string vectors and a few public set and get functions to set and call them up. This program is going to read a file and store the info in the different vectors in the class. Also if its any consolation it's written in three parts; header, cpp, and main.
This is where it gets tricky (at least for me)
I haven't a clue how to set up a function that calls the vector from main and has a int parameter to call a specific spot within the vector.
Feb 5, 2013 at 8:10am Feb 5, 2013 at 8:10am UTC
Can you clarify; this is a vector of strings, yes?
If so, a function like this would do in your class.
1 2 3 4 5 6 7 8 9
// Assuming vector object called myVector in class
std::string GetStringAtIndex( unsigned int idx )
{
return idx < myVector.size() ? myVector[idx] : " " ;
}
// Example call
// Assuming you have an instance of your class
std::string someString = yourObject.GetStringAtIndex( 0 );
Last edited on Feb 5, 2013 at 8:15am Feb 5, 2013 at 8:15am UTC
Feb 5, 2013 at 10:24am Feb 5, 2013 at 10:24am UTC
Don't missunderstand the usage of references and pointers.
Of course, even if you pass it by reference, it could be null!
passing by reference, passing by value and passing by pointer are topics quite far from what the post did ask.
Feb 5, 2013 at 10:34am Feb 5, 2013 at 10:34am UTC
If you want to extract it from instances, you may do something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
class foo
{
public :
foo(){}
~foo(){}
std::vector<std::string> GetVector()
{
return myVector;
}
private :
std::vector<std::string>myVector;
}
//In Main
foo* myFoo = new foo();
for (int i = 0; i<10; i++)
myFoo->GetVector().push_back( itoa(i)); //This fills your vector.
std::string myString = getStringAtIndex(myFoo, 2);
//outside main
std::string getStringAtIndex(foo* fooToParse, unsigned int idx)
{
return idx < fooToParse->GetVector().size() ? fooToParse->GetVector()[idx] : "ERROR!" ;
}
//If you prefer:
std::string getStringAtIndex(foo* fooToParse, unsigned int idx)
{
if (idx>fooToParse->GetVector().size())
return "ERROR!" ;
else
return fooToParse->GetVector()[idx] ;
}
Last edited on Feb 5, 2013 at 10:42am Feb 5, 2013 at 10:42am UTC
Feb 5, 2013 at 10:42am Feb 5, 2013 at 10:42am UTC
If you want to use references instead of pointers, you may change the way you instance your objects, or even how you pass values to your functions
using references:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
class foo
{
public :
foo(){}
~foo(){}
std::vector<std::string> GetVector()
{
return myVector;
}
private :
std::vector<std::string>myVector;
}
//In Main
foo* myFoo = new foo();
for (int i = 0; i<10; i++)
myFoo->GetVector().push_back(itoa(i)); //This fills your vector.
std::string myString = getStringAtIndex(*myFoo, 2);
//this is using references, and allocating in heap trough "new"
//It could be like:
foo myFoo;
for (int i = 0; i<10; i++)
myFoo.GetVector().push_back(itoa(i)); //This fills your vector.
std::string myString = getStringAtIndex(myFoo, 2);
//outside main
std::string getStringAtIndex(foo& fooToParse, unsigned int idx)
{
return idx < fooToParse.GetVector().size() ? fooToParse.GetVector()[idx] : "ERROR!" ;
}
//If you prefer:
std::string getStringAtIndex(foo& fooToParse, unsigned int idx)
{
if (idx>fooToParse.GetVector().size())
return "ERROR!" ;
else
return fooToParse.GetVector()[idx] ;
Last edited on Feb 5, 2013 at 10:43am Feb 5, 2013 at 10:43am UTC
Feb 6, 2013 at 1:03am Feb 6, 2013 at 1:03am UTC
I finally got it work. Sorry for the late response. Thank you all for your replies they have helped and given me some new ideas to implement.