I’m using Visual Studio and I'm reacquainting myself with C++ before taking a few classes.
This is an old script that used to compile, but now istream is giving me a few syntax errors. If anyone can lead me in the right direction, that would be great.
Fraction.h
friend istream& operator >> (istream& in, const Fraction& f);
Fraction.cpp
istream& operator >> (istream& in, const Fraction& f) {
cout << "Enter numerator" << endl;
in >> f.num;// error line
cout << "Enter denominator" << endl;
in >> f.denom; // error line
return in;
}
Visual Studio Error Summary
Terminal Error Bottom Small Portion
/usr/include/c++/8/istream:803:5: note: template argument deduction/substitution failed:
/home/spamandsons/projects/laney/Fraction.cpp:188:13: note: cannot convert ‘f.Fraction::denom’ (type ‘const int’) to type ‘unsigned char*’
in >> f.denom;
~~^~~~~
In file included from /usr/include/c++/8/iostream:40,
from /home/spamandsons/projects/laney/Fraction.cpp:2:
/usr/include/c++/8/istream:808:5: note: candidate: ‘template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)’
operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
^~~~~~~~
/usr/include/c++/8/istream:808:5: note: template argument deduction/substitution failed:
/home/spamandsons/projects/laney/Fraction.cpp:188:13: note: cannot convert ‘f.Fraction::denom’ (type ‘const int’) to type ‘signed char*’
in >> f.denom;
~~^~~~~
In file included from /usr/include/c++/8/iostream:40,
from /home/spamandsons/projects/laney/Fraction.cpp:2:
/usr/include/c++/8/istream:980:5: note: candidate: ‘template<class _Istream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_istream<_Istream>, std::__is_extractable<typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type, _Tp&&, void> >::value, typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type>::type std::operator>>(_Istream&&, _Tp&&)’
operator>>(_Istream&& __is, _Tp&& __x)
^~~~~~~~
/usr/include/c++/8/istream:980:5: note: template argument deduction/substitution failed:
/usr/include/c++/8/istream: In substitution of ‘template<class _Istream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_istream<_Istream>, std::__is_extractable<typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type, _Tp&&, void> >::value, typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type>::type std::operator>>(_Istream&&, _Tp&&) [with _Istream = std::basic_istream<char>&; _Tp = const int&]’:
/home/spamandsons/projects/laney/Fraction.cpp:188:13: required from here
/usr/include/c++/8/istream:980:5: error: no type named ‘type’ in ‘struct std::enable_if<false, std::basic_istream<char>&>’
The terminal process terminated with exit code: -1.
Terminal will be reused by tasks, press any key to close it.
Because it takes reference to CONST type. const Fraction& f, it must not be (just remove "const" in the both declaration and definition), the stream input operator is about to modify this object. Instead, const should be used in the stream output operator (e.g. std::ostream& operator<<(std::ostream& os, const Fraction& f)), where outputted object is not supposed to be modified.
Related
Solved the problem already when checking a last time before posting this, but it looked somewhat evil to debug (at least for a newbie), so I'll post it anyway - feel free to delete.
The problem was that in the marked line below, ofstream seemed unable to write a simple string; the templating appeared to be the problem:
template <typename T>
void appendVectorToCSV(const std::string& header, std::vector<T> row,
const std::string& outfilename){
std::ofstream fout(outfilename);
fout << header;// << ","; /* The error line 80 */
...
This gives the error:
varUtils.hpp: In function ‘void appendVectorToCSV(std::string&, const std::vector<_RealType>&, const string&)’:
varUtils.hpp:80:10: error: no match for ‘operator<<’ (operand types are ‘std::ofstream {aka std::basic_ofstream<char>}’ and ‘std::string {aka std::basic_string<char>}’)
fout << header;// << ",";
^
varUtils.hpp:80:10: note: candidates are:
...
/usr/include/c++/4.8/complex:524:5: note: template<class _Tp, class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::complex<_Tp>&)
operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
^
/usr/include/c++/4.8/complex:524:5: note: template argument deduction/substitution failed:
...
varUtils.hpp:80:13: note: ‘std::ofstream {aka std::basic_ofstream<char>}’ is not derived from ‘std::basic_ostream<_CharT, _Traits>’
fout << header;// << ",";
^
So, solution:
A missing header.
#include <iostream>
was already there, but not
#include <fstream>
What is the rationale for selectively excising template member functions from class interfaces when the arguments do not satisfy various criteria, tested for with enable_if etc? If the member function templates were left in, attempting to use them would fail, and it seems to me, with a more useful compiler error than 'substitution failure' in the more complex case?
If compilation fails either way, what is the argument in favor of these extremely strict SFINAE-based template member function requirements?
Compiler errors 2-5 deep in template code have been found to be nearly impenetrable. You get a spew of template noise.
SFINAE substitution failures usually list a template and say it doesn't work because some argument cannot be deduced, often with the trait that failed displayed. Not perfect, but better than template spew.
What more, such templates can block other ones which are valid.
In addition, you can test if a given method exists and is valid at compile time if you use SFINAE-like techniques; you cannot if the body fails to compile.
Which error message do you find easier to understand. This one with SFINAE?
template <class C, class = decltype(begin(std::declval<C&>())[0])>
void sort_sfinae(C& c) {
std::sort(c.begin(), c.end());
}
main.cpp: In function 'int main()':
main.cpp:11:18: error: no matching function for call to 'sort_sfinae(std::__cxx11::list<int>&)'
sort_sfinae(l);
^
main.cpp:5:6: note: candidate: template<class C, class> void sort_sfinae(C&)
void sort_sfinae(C& c) {
^
main.cpp:5:6: note: template argument deduction/substitution failed:
main.cpp:4:62: error: no match for 'operator[]' (operand types are 'std::__cxx11::list<int>::iterator {aka std::_List_iterator<int>}' and 'int')
template <class C, class = decltype(begin(std::declval<C&>())[0])>
^
Or this one without?
template <class C>
void sort_no_sfinae(C& c) {
std::sort(c.begin(), c.end());
}
In file included from /usr/local/include/c++/5.3.0/algorithm:62:0,
from main.cpp:2:
/usr/local/include/c++/5.3.0/bits/stl_algo.h: In instantiation of 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::_List_iterator<int>; _Compare = __gnu_cxx::__ops::_Iter_less_iter]':
/usr/local/include/c++/5.3.0/bits/stl_algo.h:4698:18: required from 'void std::sort(_RAIter, _RAIter) [with _RAIter = std::_List_iterator<int>]'
main.cpp:6:14: required from 'void sort_no_sfinae(C&) [with C = std::__cxx11::list<int>]'
main.cpp:11:21: required from here
/usr/local/include/c++/5.3.0/bits/stl_algo.h:1964:22: error: no match for 'operator-' (operand types are 'std::_List_iterator<int>' and 'std::_List_iterator<int>')
std::__lg(__last - __first) * 2,
^
In file included from /usr/local/include/c++/5.3.0/bits/stl_algobase.h:67:0,
from /usr/local/include/c++/5.3.0/list:60,
from main.cpp:1:
/usr/local/include/c++/5.3.0/bits/stl_iterator.h:328:5: note: candidate: template<class _Iterator> typename std::reverse_iterator<_Iterator>::difference_type std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator-(const reverse_iterator<_Iterator>& __x,
^
/usr/local/include/c++/5.3.0/bits/stl_iterator.h:328:5: note: template argument deduction/substitution failed:
In file included from /usr/local/include/c++/5.3.0/algorithm:62:0,
from main.cpp:2:
/usr/local/include/c++/5.3.0/bits/stl_algo.h:1964:22: note: 'std::_List_iterator<int>' is not derived from 'const std::reverse_iterator<_Iterator>'
std::__lg(__last - __first) * 2,
^
In file included from /usr/local/include/c++/5.3.0/bits/stl_algobase.h:67:0,
from /usr/local/include/c++/5.3.0/list:60,
from main.cpp:1:
/usr/local/include/c++/5.3.0/bits/stl_iterator.h:380:5: note: candidate: template<class _IteratorL, class _IteratorR> decltype ((__y.base() - __x.base())) std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator-(const reverse_iterator<_IteratorL>& __x,
^
/usr/local/include/c++/5.3.0/bits/stl_iterator.h:380:5: note: template argument deduction/substitution failed:
In file included from /usr/local/include/c++/5.3.0/algorithm:62:0,
from main.cpp:2:
/usr/local/include/c++/5.3.0/bits/stl_algo.h:1964:22: note: 'std::_List_iterator<int>' is not derived from 'const std::reverse_iterator<_Iterator>'
std::__lg(__last - __first) * 2,
^
In file included from /usr/local/include/c++/5.3.0/bits/stl_algobase.h:67:0,
from /usr/local/include/c++/5.3.0/list:60,
from main.cpp:1:
/usr/local/include/c++/5.3.0/bits/stl_iterator.h:1138:5: note: candidate: template<class _IteratorL, class _IteratorR> decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)
operator-(const move_iterator<_IteratorL>& __x,
^
/usr/local/include/c++/5.3.0/bits/stl_iterator.h:1138:5: note: template argument deduction/substitution failed:
In file included from /usr/local/include/c++/5.3.0/algorithm:62:0,
from main.cpp:2:
/usr/local/include/c++/5.3.0/bits/stl_algo.h:1964:22: note: 'std::_List_iterator<int>' is not derived from 'const std::move_iterator<_Iterator>'
std::__lg(__last - __first) * 2,
^
In file included from /usr/local/include/c++/5.3.0/bits/stl_algobase.h:67:0,
from /usr/local/include/c++/5.3.0/list:60,
from main.cpp:1:
/usr/local/include/c++/5.3.0/bits/stl_iterator.h:1145:5: note: candidate: template<class _Iterator> decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
operator-(const move_iterator<_Iterator>& __x,
^
/usr/local/include/c++/5.3.0/bits/stl_iterator.h:1145:5: note: template argument deduction/substitution failed:
In file included from /usr/local/include/c++/5.3.0/algorithm:62:0,
from main.cpp:2:
/usr/local/include/c++/5.3.0/bits/stl_algo.h:1964:22: note: 'std::_List_iterator<int>' is not derived from 'const std::move_iterator<_Iterator>'
std::__lg(__last - __first) * 2,
^
In file included from /usr/local/include/c++/5.3.0/vector:65:0,
from /usr/local/include/c++/5.3.0/bits/random.h:34,
from /usr/local/include/c++/5.3.0/random:49,
from /usr/local/include/c++/5.3.0/bits/stl_algo.h:66,
from /usr/local/include/c++/5.3.0/algorithm:62,
from main.cpp:2:
/usr/local/include/c++/5.3.0/bits/stl_bvector.h:208:3: note: candidate: std::ptrdiff_t std::operator-(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)
operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
^
/usr/local/include/c++/5.3.0/bits/stl_bvector.h:208:3: note: no known conversion for argument 1 from 'std::_List_iterator<int>' to 'const std::_Bit_iterator_base&'
I'm working on a program that uses the Boost regular expressions library, and I'm running into an issue where while trying to call the boost::regex_match() function, I'm getting a strange error that I've never encountered before. Here is the relevant code.
boost::regex pattern("REGEX REDACTED");
boost::cmatch what;
XERCES_STD_QUALIFIER cout << "Enter XML Document-Building Commands" << XERCES_STD_QUALIFIER endl;
while(true) {
// Take the input from the user.
std::string input;
XERCES_STD_QUALIFIER cout << ">> ";
XERCES_STD_QUALIFIER getline(in, input);
if(boost::regex_match(input, what, pattern)) {
// whatever
}
}
This is almost exactly what I took out of example code from a similar program my instructor provided for this assignment. But when I try to compile, I get this error. If it helps, I'm using NetBeans 8.
XMLDoc.cpp: In member function ‘void XMLDoc::createDoc(std::istream&)’:
XMLDoc.cpp:164:51: error: no matching function for call to ‘regex_match(std::string&, boost::cmatch&, boost::regex&)’
XMLDoc.cpp:164:51: note: candidates are:
In file included from /usr/include/boost/regex/v4/regex.hpp:145:0,
from /usr/include/boost/regex.hpp:31,
from XMLDoc.cpp:13:
/usr/include/boost/regex/v4/regex_match.hpp:44:6: note: template<class BidiIterator, class Allocator, class charT, class traits> bool boost::regex_match(BidiIterator, BidiIterator, boost::match_results<Iterator, Allocator>&, const boost::basic_regex<charT, traits>&, boost::regex_constants::match_flag_type)
/usr/include/boost/regex/v4/regex_match.hpp:44:6: note: template argument deduction/substitution failed:
XMLDoc.cpp:164:51: note: deduced conflicting types for parameter ‘BidiIterator’ (‘std::basic_string<char>’ and ‘boost::match_results<const char*>’)
In file included from /usr/include/boost/regex/v4/regex.hpp:145:0,
from /usr/include/boost/regex.hpp:31,
from XMLDoc.cpp:13:
/usr/include/boost/regex/v4/regex_match.hpp:53:6: note: template<class iterator, class charT, class traits> bool boost::regex_match(iterator, iterator, const boost::basic_regex<charT, traits>&, boost::regex_constants::match_flag_type)
/usr/include/boost/regex/v4/regex_match.hpp:53:6: note: template argument deduction/substitution failed:
XMLDoc.cpp:164:51: note: deduced conflicting types for parameter ‘iterator’ (‘std::basic_string<char>’ and ‘boost::match_results<const char*>’)
In file included from /usr/include/boost/regex/v4/regex.hpp:145:0,
from /usr/include/boost/regex.hpp:31,
from XMLDoc.cpp:13:
/usr/include/boost/regex/v4/regex_match.hpp:68:13: note: template<class charT, class Allocator, class traits> bool boost::regex_match(const charT*, boost::match_results<const charT*, Allocator>&, const boost::basic_regex<charT, traits2>&, boost::regex_constants::match_flag_type)
/usr/include/boost/regex/v4/regex_match.hpp:68:13: note: template argument deduction/substitution failed:
XMLDoc.cpp:164:51: note: mismatched types ‘const charT*’ and ‘std::basic_string<char>’
In file included from /usr/include/boost/regex/v4/regex.hpp:145:0,
from /usr/include/boost/regex.hpp:31,
from XMLDoc.cpp:13:
/usr/include/boost/regex/v4/regex_match.hpp:77:13: note: bool boost::regex_match(const std::basic_string<charT, ST, SA>&, boost::match_results<typename std::basic_string<charT, ST, SA>::const_iterator, Allocator>&, const boost::basic_regex<charT, traits>&, boost::regex_constants::match_flag_type) [with ST = std::char_traits<char>; SA = std::allocator<char>; Allocator = std::allocator<boost::sub_match<const char*> >; charT = char; traits = boost::regex_traits<char>; typename std::basic_string<charT, ST, SA>::const_iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; boost::regex_constants::match_flag_type = boost::regex_constants::_match_flags]
/usr/include/boost/regex/v4/regex_match.hpp:77:13: note: no known conversion for argument 2 from ‘boost::cmatch {aka boost::match_results<const char*>}’ to ‘boost::match_results<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::allocator<boost::sub_match<const char*> > >&’
/usr/include/boost/regex/v4/regex_match.hpp:85:13: note: template<class charT, class traits> bool boost::regex_match(const charT*, const boost::basic_regex<charT, traits>&, boost::regex_constants::match_flag_type)
/usr/include/boost/regex/v4/regex_match.hpp:85:13: note: template argument deduction/substitution failed:
XMLDoc.cpp:164:51: note: mismatched types ‘const charT*’ and ‘std::basic_string<char>’
In file included from /usr/include/boost/regex/v4/regex.hpp:145:0,
from /usr/include/boost/regex.hpp:31,
from XMLDoc.cpp:13:
/usr/include/boost/regex/v4/regex_match.hpp:94:13: note: template<class ST, class SA, class charT, class traits> bool boost::regex_match(const std::basic_string<charT, ST, SA>&, const boost::basic_regex<charT, traits>&, boost::regex_constants::match_flag_type)
/usr/include/boost/regex/v4/regex_match.hpp:94:13: note: template argument deduction/substitution failed:
XMLDoc.cpp:164:51: note: ‘boost::cmatch {aka boost::match_results<const char*>}’ is not derived from ‘const boost::basic_regex<charT, traits>’
Can someone help me out here? I'm certain I included the library properly because I have another project with the exact same Linker properties that uses the boost::regex_match function (minus the cmatch object parameter), and it works just fine.
If you're going to use a std::string input, you have to use boost::smatch instead of boost::cmatch. See http://www.boost.org/doc/libs/1_57_0/libs/regex/doc/html/boost_regex/ref/match_results.html. So all you need to do is change
boost::cmatch what;
to
boost::smatch what;
i'm playing around with inheritance. I'm completely baffled by the compiler
error i'm getting and looking it up it seems completely unrelated to what i'm trying
to do which is simply initialize my class.
here are the errors:
In file included from main.cpp:2:0:
student.h: In constructor ‘Student::Student(std::string, int, std::string, double)’:
student.h:13:3: error: class ‘Student’ does not have any field named ‘gnu_dev_major’
student.h: In function ‘std::ostream& operator<<(std::ostream&, const Student&)’:
student.h:25:8: error: no match for ‘operator<<’ in ‘os << "Name\011: "’
student.h:25:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [8]’ to ‘const Student&’
/usr/include/c++/4.6/bits/basic_string.h:2693:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
student.h:25:32: error: ‘endl’ is not a member of ‘std’
student.h:26:8: error: no match for ‘operator<<’ in ‘os << "Age\011: "’
student.h:26:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [7]’ to ‘const Student&’
/usr/include/c++/4.6/bits/basic_string.h:2693:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
student.h:26:30: error: ‘endl’ is not a member of ‘std’
student.h:27:8: error: no match for ‘operator<<’ in ‘os << "Major\011: "’
student.h:27:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [9]’ to ‘const Student&’
/usr/include/c++/4.6/bits/basic_string.h:2693:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
student.h:27:34: error: ‘endl’ is not a member of ‘std’
student.h:28:8: error: no match for ‘operator<<’ in ‘os << "GPA\011: "’
student.h:28:8: note: candidates are:
student.h:24:16: note: std::ostream& operator<<(std::ostream&, const Student&)
student.h:24:16: note: no known conversion for argument 2 from ‘const char [7]’ to ‘const Student&’
/usr/include/c++/4.6/bits/basic_string.h:2693:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
student.h:28:30: error: ‘endl’ is not a member of ‘std’
main.cpp: In function ‘int main()’:
main.cpp:8:2: error: ‘cout’ was not declared in this scope
whats confusing me is the very first error i'm getting:
student.h: In constructor ‘Student::Student(std::string, int,
std::string, double)’: student.h:13:3: error: class ‘Student’ does not
have any field named ‘gnu_dev_major’
here is my code:
1 #ifndef STUDENT_H
2 #define STUDENT_H
3
4 #include <iostream>
5 #include <string>
6 #include "person.h"
7
8 class Student : public Person {
9 friend std::ostream & operator<<(std::ostream &,const Student &);
10 public:
11 Student(std::string name, int age, std::string m="undecided", double gpa=0.0) :
12 Person::Person(name,age),
13 major(m),
14 gpa(gpa)
15 {}
16
17
18
19 protected:
20 double gpa;
21 std::string major;
22 };
23
24 std::ostream & operator<<(std::ostream &os,const Student &s){
25 os << "Name\t: " << s.name << std::endl;
26 os << "Age\t: " << s.age << std::endl;
27 os << "Major\t: " << s.major << std::endl;
28 os << "GPA\t: " << s.gpa << std::endl;
29 }
30
31 #endif
also if anyone has a pointer as to what might be going wrong in my overloaded
operator << help with that is also appreciated =).
You need to declare your overloaded << operator as friend inside your class:
Inside your Student class declare it as follows:
class Student {
// rest of code
friend std::ostream & operator<<(std::ostream &os,const Student &s);
}
I define the operator<< for std::string objects:
std::string & operator<< (std::string & left, std::string & right){
return left += right;
}
Then I use it:
std::string t1("t1"), t2("t2"), t3;
t3 = t2 << t1;
And get from a compiler:
t.cpp: In function 'int main()':
t.cpp:44:28: error: no matching function for call to 'operator<<(std::string&, std::string&)'
t.cpp:44:28: note: candidates are:
In file included from d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
from connection.h:10,
from login.cpp:1:
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT
, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template argument deduction/substitution failed:
t.cpp:44:28: note: 'std::string {aka std::basic_string<char>}' is not derived from 'std::basic_ostream<_CharT, _Traits>'
In file included from d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
Why talks it about ostream and does not talk about string? I.e. why it does not take in account my definition of the operator<< ?
Thanks.
Update. For those who is able just to say "why do you create operator<< for strings?" and is not able to say any helpful things:
std::string & operator<< (std::string & left, const int num){
return left += std::to_string(num);
}
std::string t3;
t3 << 3 << 5;
std::cout << t3 << std::endl;
And log:
t.cpp: In function 'int main()':
t.cpp:45:12: error: no match for 'operator<<' in 't3 << 3'
t.cpp:45:12: note: candidates are:
In file included from d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream:40:0,
from connection.h:10,
from login.cpp:1:
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT
, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
d:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:600:5: note: template argument deduction/substitution failed:
t.cpp:45:12: note: 'std::string {aka std::basic_string<char>}' is not derived from 'std::basic_ostream<_CharT, _Traits>'
It does work:
#include <iostream>
#include <string>
std::string & operator<< (std::string & left, std::string & right){
return left += right;
}
int main()
{
std::string t1("t1"), t2("t2"), t3;
t3 = t2 << t1;
std::cout << t3;
}
Output: t2t1 [GCC 4.8.1]
The compiler output, as you say yourself, indicates that your operator overload is not even visible. You must not have your declaration in the right place.
This is not really a good idea, anyway: you will simply confuse the heck out of anybody who reads your code. Strings are not streams.