Solved C++ code compiles on Ubuntu but not on FreeBSD

Hello I am trying to compile an application that I wrote (and it compiles) on an Ubuntu machine.

When I compile it on a FreeBSD machine it tells me this anytime I try to use SSTR

Code:
error: non-const lvalue refference to type 'basic_ostringstream<...>' cannot bind to a temporary of type 'basic_ostringstring<...>
Code:
#include <sstream>
#define SSTR( x ) static_cast< std::eek:stringstream & >(( std::eek:stringstream() << std::dec << x ) ).str()

Thanks for any advice I can get




To make it as simple as I can
Code:
#include <iostream>
#include <sstream>
#include <string>

#define SSTR( x ) static_cast< std::ostringstream & >(( std::ostringstream() << std::dec << x ) ).str()

using namespace std;

int main()
{
    int iTest = 789;
    cout << SSTR(iTest) << endl;
    return 0;
}

compile
Code:
c++ -Wall -std=c++11 -fexceptions -std=c++11 -g -Iinclude -c main.cpp -o main.o

error is
Code:
main.cpp:12:13: error: non-const lvalue refrence to type
'basic_ostringstream<...>' cannot bind to a temporary of type
'basic_ostringstream<...>'
cout << SSTR(iTest) << endl;

main.cpp:5:19: note: expanded from macro 'SSTR'
... static_cast< std::ostringstream & > (( std::ostringstream() << std::dec << x)...

~~
1 error generated.
 
Its a bit weird with that MACRO. Perhaps try something like:

Code:
#include <sstream>
#include <string>
#include <iostream>

int main()
{
  std::stringstream ss;
  int someVal = 99;

  ss << someVal;

  std::cout << "Value: " << ss.str() << std::endl;

  return 0;
}
 
Why all this complexity? What's wrong with just
C++:
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    int iTest = 789;
    std::cout << iTest << std::endl;
    return 0;
}
Am I missing something?
 
Heh, yours is most direct. Good point. I just assumed that based on the OPs code, he wanted a way to convert an int into a std::string. I thought the std::cout was just an example of using the final generated string.
 
Thanks for everyones reply. The actual application is very large and uses the SSTR macro several times.
The example above I was able to recreate with just a few lines of code.

changing c++ to g++ fixed the issue.

Thanks for helping
 
Back
Top