Patrick
Patrick

Reputation: 51

Stringstream compile error occurs only in linux environment

I wrote a program in C++ using Visual Studio on my desktop. In that environment, the program compiles and executes without error, and the output is exactly as expected.

However, when I try to move my source code to a linux system and compile it, the I encounter errors related to my use of stringstream. The code has the form:

#include <sstream>
#include <string>
using namespace std;

int main() {
    string line;
    stringstream ssline;
    ssline = stringstream(line); //where the error occurs
}

I use this assignment operation many times throughout my program, and like I said - I didn't cause any problems when I used Visual Studio to compile. On my linux system, both the gnu compiler and the intel compiler throw the same error, which reads:

ProgramName.cpp:73:12: error: use of deleted function
‘std::basic_stringstream<char>& std::basic_stringstream<char>::operator=
(const std::basic_stringstream<char>&)’
    ssline = stringstream(line);
                ^
In file included from ProgramName.cpp:13:0:
/usr/include/c++/4.8.2/sstream:502:11: note:
‘std::basic_stringstream<char>& std::basic_stringstream<char>::operator=(const std::basic_stringstream<char>&)’
is implicitly deleted because the default definition would be ill-formed:
    class basic_stringstream : public basic_iostream<_CharT, _Traits>

I do not know what to make of this error, nor why it seems to be system dependent. I can rewrite my source code to avoid the use of stringstream, but I would prefer not to. Because again, I know it works on my desktop environment.

I appreciate any help that can be offered in resolving this difficulty. Thanks in advance.

Upvotes: 0

Views: 1096

Answers (2)

Kenneth Davis
Kenneth Davis

Reputation: 167

Using ssline.str(line); instead will accomplish what you want (including the continued use of std::stringstream).

TL;DR Your code is not compiling because stringstream instances are not copyable (in your GCC compiler, refs @HolyBlackCat's answer).

Take a look at https://en.cppreference.com/w/cpp/language/copy_assignment. There is a section titled "Deleted implicitly-declared copy assignment operator". What's happening here in your code is that since std::stringstream does not have an assignment operator and you're trying to use it the compiler tries to generate one. However, because std::stringstream instances are not copyable the implicitly-declared operator is defined as deleted.

Upvotes: 1

HolyBlackCat
HolyBlackCat

Reputation: 96791

It seems that GCC has move assignment for std::stringstream since GCC 5, and you use GCC 4.8.2.

Your GCC is too old, you need to upgrade.

Upvotes: 4

Related Questions