OK, so as C++ is new to me I am confused. Generally I would pass an object using the "ref" keyword and I'd have no issues retaining the information that was changed in my object when I got back to the main.

Well C++ is different. I have a class that accepts a custom object and while that class has this object I fill in information (vector of objects, a few strings, etc.) Problem is when I get back to the main that information was destroyed. I am thinking I need to send a pointer to the object and not the object itself, but all the samples seem a bit vague or I am just too green to understand them.

So let's say I have a class

class createSurvey
{
public:
   Survey createNewSurvey(Survey * S)
   {
     //do something to fill the survey object
     return S;
   }
}

Main function call looks like this:

//call the function from the class sending the survey object
//get the survey object back
S = createSurvey.createNewSurvey(S);

OK, Like I said I attempted to send a pointer but I keep getting compiler errors. Any help would be fantastic.

Best Regards,
William

Instead of using a pointer you can use a reference and get the same behavior. For instance, if the original signature of the function that compiled was

void createNewSurvey (Survey S) { ... }

then you can make the following change and keep any modifications to S within createNerSurvey

void createNewSurvey(Survey& S) { ... }

The & means pass as a reference to the original object.

OK, I will try that now. Thanks for your quick response.

OK, that worked. Now I have one more small issue. I hope you'd be kind enough to help again?

I have an object called survey. This object is the main "container" for an entire survey. This object has a vector that contains other objects (the questions) called surveyquestions. For each question a person enters I create a new object and drop it into the vector. This works great. Now each question can have many answers based on what the users type in. So to handle is I created another vector in each surveyquestion object. So basically a have a vector of objects and each object has a string vector of answers.

So I wrote a method to return questions and loop them asking each question in order. When the user answers I add their answer into the answer string vector. This information will get written to disk, but for now it is in memory. My problem is that the questions come over and loop fine but the answer is not saved into the original survey object. The questions are still there, but the answers are not. I assume this might be a similar problem so here is the code.

//this is the code that gets the questions from the survey
		Survey takeSurvey(MySurvey::Survey& S)
		{
			questions = S.getSurveyQuestions();
			int size = questions.size();
			for (int i = 0; i < size; i++)
			{
				string input = "";
				cout << questions[i].getQuestion() << endl;
				getline(cin, input);
				questions[i].addAnswer(input);
			}	
			return S;
		}

//this is the code that is returning the questions to the above code:
		vector<MySurvey::SurveyQuestion>  getSurveyQuestions()
		{
			return &questions;
		}

//this is the definition of the vector
vector<MySurvey::SurveyQuestion> questions;

Thanks again for all your help.

William

//this is the code that gets the questions from the survey
		Survey takeSurvey(MySurvey::Survey& S)
		{
			questions = S.getSurveyQuestions();
			int size = questions.size();
			for (int i = 0; i < size; i++)
			{
				string input = "";
				cout << questions[i].getQuestion() << endl;
				getline(cin, input);
				questions[i].addAnswer(input);
			}	
			return S;
		}

I cant see what addAnswer does.

//this is the code that is returning the questions to the above code:
		vector<MySurvey::SurveyQuestion>  getSurveyQuestions()
		{
			return &questions;
		}

//this is the definition of the vector
vector<MySurvey::SurveyQuestion> questions;

That is problematic in that you are returning the address of the vector where a vector is expected. You probably mean to do

vector<MySurvey::SurveyQuestion>&  getSurveyQuestions()
{
   return questions;
}

The reference will allow you to update the returned vector instead of a copy of it.

Note you should expose your vector like that instead return it by const-reference and if you need to update it, then provide proper interface. Also typedefs also might come in handy, as it is more clearer.

typedef std::vector<MySurvey::SurveyQuestion> Questions;
//...
const Questions& getSurveyQuestions()const{ return questions; }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.