return stringstream from a function?

How to return a stringstream from a function?

There are many examples online of passing stringstream into a function; but I did not find anything about returning stringstream from a function.

Thank you for helping.

This example compiles when line 21 is commented:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sstream>
using namespace std;

class A
{
	private:
		stringstream ss;
	public:
		stringstream& getSs()
		{
			return ss;
		}
};

int main()
{
	stringstream ss;
	A a;

	ss = a.getSs(); // compiles when commented, otherwise gets errors
	cout << ss.str();
}


First two errors when compiling above example:

C:\>g++ pass_stringstream.cpp
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:42:0,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
                 from pass_stringstream.cpp:1:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h: In member function '
std::basic_ios<char>& std::basic_ios<char>::operator=(const std::basic_ios<char>
&)':
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h:789:5: error: 'std::i
os_base& std::ios_base::operator=(const std::ios_base&)' is private
     operator=(const ios_base&);
     ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:44:0,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
                 from pass_stringstream.cpp:1:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_ios.h:66:11: error: within
 this context
     class basic_ios : public ios_base
           ^
Streams cannot be copied.

In C++98 this rule was enforced by making the copy constructor and copy assignment operator private.
(Which is what the error messages say.)

In C++11 this rule is enforced more clearly by marking the copy constructor and copy assignment operator as deleted.

http://www.cplusplus.com/reference/sstream/stringstream/stringstream/
http://www.cplusplus.com/reference/sstream/stringstream/operator=/

If you want this to work without using the move assignment operator (see above) then you will have to declare the ss object in main() as a reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <sstream>
// using namespace std; // this is a bad habit

class A
{
    private:
        std::stringstream ss;

    public:
        std::stringstream& getSs()
        {
            return ss;
        }
};

int main()
{
    A a;
    std::stringstream &ss = a.getSs();

    ss << "Hello, World!";
    std::cout << ss.str() << std::endl;
}


http://www.parashift.com/c++-faq/using-namespace-std.html
http://www.cprogramming.com/tutorial/references.html
http://en.cppreference.com/w/cpp/language/reference
Last edited on
You can return a stringstream from a function by value, as long as you are not trying to make a copy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <sstream>

std::stringstream getSs()
{
    std::stringstream ss;
    ss << "abc";
    return ss;  // this is not a copy in C++11
}

int main()
{
    std::stringstream ss;

    ss = getSs(); // the move assignment mentioned by Catfish above
    std::cout << ss.str();
}


see it live: http://coliru.stacked-crooked.com/a/f5ecd8578fd2fbd2

(note that GCC didn't implement this part of C++11 yet)
Last edited on
Thank you again Catfish666!
Topic archived. No new replies allowed.