Sign in to follow this  
gah_ribaldi

syntax error 'return' - c++

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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this