cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’

Hello.
In code in my project i have no problem. But sinse remove it in new header file compiler says cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’ and i dont know why.
Same code, C++11 gcc and Linux.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
struct stringify
{
    operator std::string() const
    {
        return str ;
    }
    std::string str ;
};

template <typename T>stringify operator+(stringify s,const T& object)
{
    std::ostringstream stm;
    stm << object;
    if( !s.str.empty() && !std::isspace(*s.str.rbegin()))s.str +=' ';
    s.str += stm.str();
    return s;
}

Last edited on
I do not see any problems here. Areyou sure that this is the code which gives you this error? How it is used?
Like than

str=stringify() + "my file "+ 12 + "," + " is better than " + 5;
Last edited on
1) You have problem with quotes.
2) is str a std::string?

If so, your code works. If this code is in separate header, do you have proper header guards? Can you post minimal code which gives you this error?
Yes str is a std::string.
About "my file "+ 12 + "," + " is better than " + 5; where is the problem?
Something else maybe wrong. I am lookin in code before.






Last edited on
wouldn't you need to do something like this:

"my file "+ std::to_string(12) + "," + " is better than " + std::to_string(5);

or am i missing something?
I did not see that you edited your code. This code works fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
struct stringify
{
    operator std::string() const
    { return str ;  }
    std::string str ;
};

template <typename T>stringify operator+(stringify s,const T& object)
{
    std::ostringstream stm;
    stm << object;
    if( !s.str.empty() && !std::isspace(*s.str.rbegin()))s.str +=' ';
    s.str += stm.str();
    return s;
}

int main()
{
	std::string str = stringify() + "my file "+ 12 + "," + " is better than " + 5;
	std::cout << str;
}
So find out how to reproduce your problem and then post an example of it.
I will look code before and reproduce an example.
Thank's ...
Here is the code in a minimal example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <sstream>
#include <cmath>

using namespace std;

struct S1
{
    int i1;
    int i2;
    S1(int i,int j)
    {
        i1=i;
        i2=j;
    };
    S1() {};
    /* std::string */ operator std::string()
    {
        return (std::to_string(i1) + "--" + std::to_string(i2));
    };
};

struct S2
{
    int i;
    S2(long int i1)
    {
        i=i1;
    };
    operator std::string()
    {
        return std::to_string(i);
    };
};

struct stringify
{
    operator std::string()const
    {
        return str;
    };
    std::string str;
};

template <typename T>stringify operator+(stringify s,const T& object)
{
    std::ostringstream stm;
    stm << object;
    if( !s.str.empty() && !std::isspace(*s.str.rbegin()))s.str+=' ';
    s.str += stm.str();
    return s;
}

int main()
{
    S1 a(10,15);
    S2 b(10);

    string s1=a;
    cout<<s1<<endl;

    string s2=b;
    cout<<s2<<endl;

    string s3;
    s3="find "+a+"find "; //here is the error ... ‘operator+’ (operand types are ‘const char [6]’ and ‘S1’)
}
Last edited on
You do not have any operator+ overload which would accept class S1. What do you think will be executed here?
Sorry my fault in code ... I just change code ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <sstream>
#include <cmath>

using namespace std;

struct S1
{
    int i1;
    int i2;
    S1(int i,int j)
    {
        i1=i;
        i2=j;
    };
    S1() {};
    /* std::string */ operator std::string()
    {
        return (std::to_string(i1) + "--" + std::to_string(i2));
    };
};

struct S2
{
    int i;
    S2(long int i1)
    {
        i=i1;
    };
    operator std::string()
    {
        return std::to_string(i);
    };
};

struct stringify
{
    operator std::string()const
    {
        return str;
    };
    std::string str;
};

template <typename T>stringify operator+(stringify s,const T& object)
{
    std::ostringstream stm;
    stm << object; //cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
    if( !s.str.empty() && !std::isspace(*s.str.rbegin()))s.str+=' ';
    s.str += stm.str();
    return s;
}

int main()
{
    S1 a(10,15);
    S2 b(10);

    string s1=a;
    cout<<s1<<endl;

    string s2=b;
    cout<<s2<<endl;

    string s3;
    s3=stringify()+"find "+a+"find "; 
}


Automaticaly must give "find 10--15 find " .
But compiler say.
cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’



It should give you a fairly large error message. I do not think that this is all it tells you about.

Your S1 do not have operator<< overloaded. So compiler does not know what to do with it. Create operator<< for your class, so it will work with stream insertion.
Last edited on
The all mesage ...

main.cpp||In instantiation of ‘stringify operator+(stringify, const T&) [with T = S1]’:|
66|required from here|
cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’|
/usr/include/c++/4.9/ostream|602|note: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = S1]’|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 2 second(s)) ===|

Ok with this last add.
Thank's

std::ostream& operator<<(std::ostream& os, const S1& obj)
{
os<<obj.i1;
return os;
}
Last edited on
Topic archived. No new replies allowed.