简体   繁体   中英

C++ snippet OK with MSVC but not with g++

I'm new to C++ and I try to adapt a program snippet which generates "weak compositions" or Multisets found here on stackoverflow but I run - to be quite frank - since hours into problems.

First of all, the program runs without any complaint under MSVC - but not on gcc.

The point is, that I have read many articles like this one here on stackoverflow , about the different behaviour of gcc and msvc and I have understood, that msvc is a bit more "liberal" in dealing with this situation and gcc is more "strict". I have also understood, that one should "not bind a non-const reference to a temporary (internal) variable."

But I am sorry, I can not fix it and get this program to work under gcc - again since hours.

And - if possible - a second question: I have to introduce a global variable total , which is said to be "evil", although it works well. I need this value of total, however I could not find a solution with a non-global scope.

Thank you all very much for your assistance.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int total = 0;

string & ListMultisets(unsigned au4Boxes, unsigned au4Balls, string & strOut = string(), string strBuild = string()) {
  unsigned au4;
  if (au4Boxes > 1) for (au4 = 0; au4 <= au4Balls; au4++)
  {
    stringstream ss;
    ss << strBuild << (strBuild.size() == 0 ? "" : ",") << au4Balls - au4;
    ListMultisets(au4Boxes - 1, au4, strOut, ss.str());
  }
  else
  {
    stringstream ss;
    ss << mycount << ".\t" << "(" << strBuild << (strBuild.size() == 0 ? "" : ",") << au4Balls << ")\n";
    strOut += ss.str();
    total++;
  }

return strOut;
}

int main() {
  cout << endl << ListMultisets(5,3) << endl;
  cout << "Total: " << total << " weak compositions." << endl;
  return 0;
}

Remove the default value for the strOut parameter.

Create a string in main and pass it to the function.

Change the return type of the function to be int.

Make total a local variable ListMultisets(). Return total rather than strOut (you are returning the string value strOut as a reference parameter.)

The signature of the new ListMultisets will look like:

int ListMultisets(unsigned au4Boxes, unsigned au4Balls, string & strOut) 

I'll let you figure out the implementation. It will either be easy or educational.

Your new main function will look like:

int main() {
  string result;
  int total = ListMultisets(5,3, result);
  cout << endl << result << endl;
  cout << "Total: " << total << " weak compositions." << endl;
  return 0;
}

C++ demands that a reference parameter to an unnamed temporary (like string() ) must either be a const reference or an r-value reference .

Either of those reference types will protect you from modifying a variable that you don't realize is going to be destroyed within the current expression.

Depending on your needs, it could would to make it a value parameter:

string ListMultisets( ... string strOut = string() ... ) {

or it could would to make it a function-local variable:

string ListMultisets(...) {
string strOut;

In your example program, either change would work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM