Jan 21, 2015 at 3:31pm UTC
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 Jan 21, 2015 at 3:41pm UTC
Jan 21, 2015 at 4:02pm UTC
I do not see any problems here. Areyou sure that this is the code which gives you this error? How it is used?
Jan 21, 2015 at 4:15pm UTC
Like than
str=stringify() + "my file " + 12 + "," + " is better than " + 5;
Last edited on Jan 21, 2015 at 4:17pm UTC
Jan 21, 2015 at 4:21pm UTC
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?
Jan 21, 2015 at 4:35pm UTC
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 Jan 21, 2015 at 4:36pm UTC
Jan 21, 2015 at 4:41pm UTC
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?
Jan 21, 2015 at 4:48pm UTC
I will look code before and reproduce an example.
Thank's ...
Jan 22, 2015 at 7:44am UTC
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 Jan 22, 2015 at 7:45am UTC
Jan 22, 2015 at 8:28am UTC
You do not have any operator+ overload which would accept class S1. What do you think will be executed here?
Jan 22, 2015 at 9:31am UTC
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 Jan 22, 2015 at 9:31am UTC
Jan 22, 2015 at 9:38am UTC
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 Jan 22, 2015 at 9:57am UTC