Jump to content
  • Advertisement
Sign in to follow this  
gah_ribaldi

syntax error 'return' - c++

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

The compiler's having new year fun with me right?
std::string Order::getPaymentsSaveString(void)
{
	std::string outString;
	std::ostringstream oss;

	outString = "";
	ListOfPayments::iterator paymentIter = paymentList.begin();
	while (paymentIter != paymentList.end())
	{
		outString.append((*paymentIter)->getSaveString());
		
		}
		paymentIter++;
		if (paymentIter == paymentList.end()){
			outString.append("\n");
		}
		else {
			outString.append(" ");
		}
	}
	return outString;
}

gives error C2059: syntax error : 'return' on the return at the bottom of the function. Have the machines finally turned against me, or is it my mind?

Share this post


Link to post
Share on other sites
Advertisement
You have too many closing braces, the return is actually outside of the method's body!..


std::string Order::getPaymentsSaveString(void)
{
std::string outString;
std::ostringstream oss;

outString = "";
ListOfPayments::iterator paymentIter = paymentList.begin();
while (paymentIter != paymentList.end())
{
outString.append((*paymentIter)->getSaveString());

} // <<<<<<<<<<<<<<< Delete this brace <<<<<<<<<<<<<<<
paymentIter++;
if (paymentIter == paymentList.end()){
outString.append("\n");
}
else {
outString.append(" ");
}
}
return outString;
}

Share this post


Link to post
Share on other sites

// In C++ we typically leave out the 'void' when the argument list
// is empty - just write getPaymentsSaveString()
std::string Order::getPaymentsSaveString(void)
{
std::string outString;
std::ostringstream oss;

// This line is unnecessary (the string constructor leaves the string
// 'empty' by default)
outString = "";
ListOfPayments::iterator paymentIter = paymentList.begin();
while (paymentIter != paymentList.end())
{
outString.append((*paymentIter)->getSaveString());

// Errant bracket here (most likely the cause of the error)
}
paymentIter++;
if (paymentIter == paymentList.end()){
outString.append("\n");
}
else {
outString.append(" ");
}
}
return outString;
}

Share this post


Link to post
Share on other sites
Thanks both.

It never ceases to amaze me how easy it is to spot errors like that in the code of others, yet the huge amount of time it takes to find these things in one's own code...

Share this post


Link to post
Share on other sites
Consistent formatting ftw. No, seriously. You have a mix of K&R and Block style indentation and bracing. That's a recipe for trouble. Stay consistent.

Share this post


Link to post
Share on other sites
A simple change to your algorithm enables a much cleaner expression of it:


std::string Order::getPaymentsSaveString() {
std::string outString;
// Instead of checking for the last item, we'll just handle all items,
// and then *replace* the last space with a return.
for (ListOfPayments::iterator paymentIter = paymentList.begin();
paymentIter != paymentList.end();
++paymentIter) {
// operator overloading FTW
outString += (*paymentIter)->getSaveString() + " ";
}
outString[outString.length() - 1] = '\n';
return outString;
}

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!