So I came on here earlier asking about a school assignment that involved reading each character from a file. That issue was fixed, but now I have encountered another problem.
Part of my assignment is to take in a user inputted file name, then set that file name to a text file that will be outputted with the results of the program.
Here is the program itself:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
const int fileNameSize = 20;
int fileCount = 0;
int testcount = 0;
const string outName;
std::ifstream inFile;
std::ofstream outFile;
<a bunch of other stuff>
outFile.open(outName);
int chartotal = acount + tcount + ecount + ocount + icount + ncount + scount + hcount + rcount;
outFile << "File name: " << inputFileName << endl;
outFile << "Number of E's: " << ecount << endl;
outFile << "Number of T's: " << tcount << endl;
outFile << "Number of A's: " << acount << endl;
outFile << "Number of O's: " << ocount << endl;
outFile << "Number of I's: " << icount << endl;
outFile << "Number of N's: " << ncount << endl;
outFile << "Number of S's: " << scount << endl;
outFile << "Number of H's: " << hcount << endl;
outFile << "Number of R's: " << rcount << endl;
outFile << "Total Number of Characters: " << chartotal << endl;
outFile << "--------------------------------" << endl;
outFile.close();
return 0;
}
inFile.close();
inFile.clear();
}
//cout << "exit colm flag";
//cout << "exit row flag\n";
}
It's my understanding that outFile.open(outName); should create a new file with the file name set as outName, however I just get a huge long error whenever I compile.
What am I doing wrong?
Edit: Here is the error I recieve
closed4Ahrens.cpp: In function 'int main()':
closed4Ahrens.cpp:19:9: error: no match for 'operator>>' in 'std::cin >> outName'
closed4Ahrens.cpp:19:9: note: candidates are:
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:122:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:122:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:126:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:126:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:133:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:133:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'std::ios_base& (*)(std::ios_base&)'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:169:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:169:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'bool&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:173:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:173:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'short int&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:176:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:176:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'short unsigned int&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:180:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:180:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'int&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:183:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:183:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'unsigned int&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:187:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:187:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'long int&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:191:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:191:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'long unsigned int&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:196:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:196:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'long long int&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:200:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:200:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'long long unsigned int&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:205:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:205:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'float&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:209:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:209:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'double&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:213:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:213:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'long double&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:217:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:217:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'void*&'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:241:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:241:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}'
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/bits/basic_string.tcc:998:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/bits/istream.tcc:957:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/bits/istream.tcc:925:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:709:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:714:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:756:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/istream:761:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/iomanip:70:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Resetiosflags)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/iomanip:100:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setiosflags)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/iomanip:131:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setbase)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/iomanip:169:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setfill<_CharT>)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/iomanip:199:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setprecision)
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/iomanip:229:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setw)
closed4Ahrens.cpp:68:24: error: no matching function for call to 'std::basic_ofstream<char>::open(const string&)'
closed4Ahrens.cpp:68:24: note: candidate is:
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/fstream:702:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char, _Traits = std::char_traits<char>, std::ios_base::openmode = std::_Ios_Openmode]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/fstream:702:7: note: no known conversion for argument 1 from 'const string {aka const std::basic_string<char>}' to 'const char*'
Update: I deleted the const in const string outName, now the error message is much shorter
closed4Ahrens.cpp:68:24: error: no matching function for call to 'std::basic_ofstream<char>::open(std::string&)'
closed4Ahrens.cpp:68:24: note: candidate is:
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/fstream:702:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char, _Traits = std::char_traits<char>, std::ios_base::openmode = std::_Ios_Openmode]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/fstream:702:7: note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'
Update 2: I've done as Paul Evans suggested
string outFileName;
ifstream inFile;
ofstream outFile;
char inputFileName [fileNameSize+1];
cout << "How many files are being processed?\n";
cin >> fileCount;
cout << "What is the output file name?\n";
cin >> outFileName;
const string outName(outFileName);
and
outFile.open(outName);
And now I am receiving this error message:
closed4Ahrens.cpp: In function 'int main()':
closed4Ahrens.cpp:70:17: error: no matching function for call to 'std::basic_ofstream<char>::open()'
closed4Ahrens.cpp:70:17: note: candidate is:
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/fstream:702:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char, _Traits = std::char_traits<char>, std::ios_base::openmode = std::_Ios_Openmode]
/opt/csw/lib/gcc/i386-pc-solaris2.10/4.6.3/../../../../include/c++/4.6.3/fstream:702:7: note: candidate expects 2 arguments, 0 provided
Your telling the compiler that outfile is const without initializing it, don't do that. Do this instead:
const string outName{"The path of the outfile"};
or
string outFileName;
// get outFileName
const string outName{outFileName};
Related
I was writing a simple piece of c++ code to print out input till the input is not 42. Here is my code:
#include <iostream>
using namespace std;
int main(){
while(true){
int inp;
cin >> inp >> endl;
if(inp == 42){
break;
}
cout << inp << endl;
}
return 0;
}
However when I compile it ,I get a list of errors:
testlife.cpp: In function ‘int main()’:
testlife.cpp:9:14: error: no match for ‘operator>>’ (operand types are ‘std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}’ and ‘<unresolved overloaded function type>’)
cin >> inp >> endl;
~~~~~~~~~~~^~~~~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:120:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(__istream_type& (*__pf)(__istream_type&))
^~~~~~~~
/usr/include/c++/6/istream:120:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}’
/usr/include/c++/6/istream:124:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
operator>>(__ios_type& (*__pf)(__ios_type&))
^~~~~~~~
/usr/include/c++/6/istream:124:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/6/istream:131:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(ios_base& (*__pf)(ios_base&))
^~~~~~~~
/usr/include/c++/6/istream:131:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/6/istream:168:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(bool& __n)
^~~~~~~~
/usr/include/c++/6/istream:168:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘bool&’
/usr/include/c++/6/istream:172:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>]
operator>>(short& __n);
^~~~~~~~
/usr/include/c++/6/istream:172:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short int&’
/usr/include/c++/6/istream:175:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(unsigned short& __n)
^~~~~~~~
/usr/include/c++/6/istream:175:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short unsigned int&’
/usr/include/c++/6/istream:179:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>]
operator>>(int& __n);
^~~~~~~~
/usr/include/c++/6/istream:179:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int&’
/usr/include/c++/6/istream:182:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(unsigned int& __n)
^~~~~~~~
/usr/include/c++/6/istream:182:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘unsigned int&’
/usr/include/c++/6/istream:186:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(long& __n)
^~~~~~~~
/usr/include/c++/6/istream:186:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long int&’
/usr/include/c++/6/istream:190:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(unsigned long& __n)
^~~~~~~~
/usr/include/c++/6/istream:190:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long unsigned int&’
/usr/include/c++/6/istream:195:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(long long& __n)
^~~~~~~~
/usr/include/c++/6/istream:195:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long int&’
/usr/include/c++/6/istream:199:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(unsigned long long& __n)
^~~~~~~~
/usr/include/c++/6/istream:199:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long unsigned int&’
/usr/include/c++/6/istream:214:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(float& __f)
^~~~~~~~
/usr/include/c++/6/istream:214:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘float&’
/usr/include/c++/6/istream:218:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(double& __f)
^~~~~~~~
/usr/include/c++/6/istream:218:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘double&’
/usr/include/c++/6/istream:222:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(long double& __f)
^~~~~~~~
/usr/include/c++/6/istream:222:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long double&’
/usr/include/c++/6/istream:235:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
operator>>(void*& __p)
^~~~~~~~
/usr/include/c++/6/istream:235:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void*&’
/usr/include/c++/6/istream:259:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
operator>>(__streambuf_type* __sb);
^~~~~~~~
/usr/include/c++/6/istream:259:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
In file included from /usr/include/c++/6/string:53:0,
from /usr/include/c++/6/bits/locale_classes.h:40,
from /usr/include/c++/6/bits/ios_base.h:41,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from testlife.cpp:2:
/usr/include/c++/6/bits/basic_string.tcc:1437:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator>>(basic_istream<_CharT, _Traits>& __in,
^~~~~~~~
/usr/include/c++/6/bits/basic_string.tcc:1437:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: couldn't deduce template parameter ‘_Alloc’
cin >> inp >> endl;
^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:799:5: note: candidate: std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char; _Traits2 = std::char_traits<char>; _CharT = char; _Traits = std::char_traits<char>]
operator>>(basic_istream<char>& __in, char* __s);
^~~~~~~~
/usr/include/c++/6/istream:799:5: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘char*’
In file included from /usr/include/c++/6/istream:934:0,
from /usr/include/c++/6/iostream:40,
from testlife.cpp:2:
/usr/include/c++/6/bits/istream.tcc:923:5: note: candidate: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char; _Traits = std::char_traits<char>]
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
^~~~~~~~
/usr/include/c++/6/bits/istream.tcc:923:5: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘char&’
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:756:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
^~~~~~~~
/usr/include/c++/6/istream:756:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘unsigned char&’
cin >> inp >> endl;
^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:761: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& __c)
^~~~~~~~
/usr/include/c++/6/istream:761:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘signed char&’
cin >> inp >> endl;
^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:803:5: note: candidate: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
^~~~~~~~
/usr/include/c++/6/istream:803:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘unsigned char*’
cin >> inp >> endl;
^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/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++/6/istream:808:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: cannot convert ‘std::endl’ (type ‘<unresolved overloaded function type>’) to type ‘signed char*’
cin >> inp >> endl;
^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from testlife.cpp:2:
/usr/include/c++/6/istream:924:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&)
operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
^~~~~~~~
/usr/include/c++/6/istream:924:5: note: template argument deduction/substitution failed:
testlife.cpp:9:17: note: couldn't deduce template parameter ‘_Tp’
cin >> inp >> endl;
I have never encountered such error before nor I am unable to debug it given the simple block of code I am writing. I am using g++ compiler installed on ubuntu system.
Remove the endl from the cin statement:
cin >> inp;
Instead of:
cin >> inp >> endl;
There's no overload of the std::endl I/O manipulator for std::istream as the error message clearly states.
Also you should handle errors when taking input, otherwise your loop will run forever:
int main(){
while(true){
int inp;
if(cin >> inp && inp == 42) {
break;
}
else {
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}
cout << inp << endl;
}
return 0;
}
Working off Chris's answer in this post How to print out the contents of a vector? I am trying to overload ostream << to print a vector vectors. Here's what I'm reading in from a file, where each line becomes a vector:
1, 2, 3
4, 4, 4, 4, 4
7, 7
41, 52, 632
So the structure looks something like
v[0] = [1, 2, 3]
and so on.
I thought Chris's solution should apply perfectly to this situation, and it certainly works for a 1d vector, but I'm trying to understand why it doesn't work for a 2d vector. The code's below:
template <typename T>
std::ostream& operator << (std::ostream& out, const std::vector<T>& v){
if(! v.empty()){
std::cout<< '[';
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout<< ']' << std::endl;
}
else{
std::cout << "[ ]" << std::endl;
}
}
I'm thinking I need to replace the function in std::ostream_iterator to be << since that is what I am overriding in the first place, but I have tried << and ostream::<< and std:: << and these all yield compile-time errors. What would be the proper way to do this?
If I try to run the code above as is on a vector of vectors of ints, I have the following compile-time error:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h: In instantiation of ‘std::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [with _Tp = std::vector<int>; _CharT = char; _Traits = std::char_traits<char>]’:
/usr/include/c++/4.9/bits/stl_algobase.h:336:18: required from ‘static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const std::vector<int>*; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
/usr/include/c++/4.9/bits/stl_algobase.h:396:70: required from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const std::vector<int>*; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
/usr/include/c++/4.9/bits/stl_algobase.h:434:38: required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int> > >; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
/usr/include/c++/4.9/bits/stl_algobase.h:466:17: required from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int> > >; _OI = std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >]’
ctest.cpp:15:75: required from ‘std::ostream& operator<<(std::ostream&, const std::vector<T>&) [with T = std::vector<int>; std::ostream = std::basic_ostream<char>]’
ctest.cpp:70:18: required from here
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: error: no match for ‘operator<<’ (operand types are ‘std::ostream_iterator<std::vector<int>, char, std::char_traits<char> >::ostream_type {aka std::basic_ostream<char>}’ and ‘const std::vector<int>’)
*_M_stream << __value;
^
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: candidates are:
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:108:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^
/usr/include/c++/4.9/ostream:108:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’
/usr/include/c++/4.9/ostream:117:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
operator<<(__ios_type& (*__pf)(__ios_type&))
^
/usr/include/c++/4.9/ostream:117:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/4.9/ostream:127:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(ios_base& (*__pf) (ios_base&))
^
/usr/include/c++/4.9/ostream:127:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/4.9/ostream:166:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long __n)
^
/usr/include/c++/4.9/ostream:166:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long int’
/usr/include/c++/4.9/ostream:170:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long __n)
^
/usr/include/c++/4.9/ostream:170:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long unsigned int’
/usr/include/c++/4.9/ostream:174:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(bool __n)
^
/usr/include/c++/4.9/ostream:174:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘bool’
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:91:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.9/bits/ostream.tcc:91:5: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘short int’
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:181:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned short __n)
^
/usr/include/c++/4.9/ostream:181:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘short unsigned int’
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:105:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.9/bits/ostream.tcc:105:5: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘int’
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:192:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned int __n)
^
/usr/include/c++/4.9/ostream:192:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘unsigned int’
/usr/include/c++/4.9/ostream:201:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long long __n)
^
/usr/include/c++/4.9/ostream:201:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long long int’
/usr/include/c++/4.9/ostream:205:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(unsigned long long __n)
^
/usr/include/c++/4.9/ostream:205:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long long unsigned int’
/usr/include/c++/4.9/ostream:220:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(double __f)
^
/usr/include/c++/4.9/ostream:220:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘double’
/usr/include/c++/4.9/ostream:224:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(float __f)
^
/usr/include/c++/4.9/ostream:224:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘float’
/usr/include/c++/4.9/ostream:232:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(long double __f)
^
/usr/include/c++/4.9/ostream:232:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘long double’
/usr/include/c++/4.9/ostream:245:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(const void* __p)
^
/usr/include/c++/4.9/ostream:245:7: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘const void*’
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:119:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
basic_ostream<_CharT, _Traits>::
^
/usr/include/c++/4.9/bits/ostream.tcc:119:5: note: no known conversion for argument 1 from ‘const std::vector<int>’ to ‘std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
In file included from /usr/include/c++/4.9/string:52:0,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/basic_string.h:2772: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>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/include/c++/4.9/bits/basic_string.h:2772:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: ‘const std::vector<int>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:471:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^
/usr/include/c++/4.9/ostream:471:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘std::vector<int>’)
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:476:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^
/usr/include/c++/4.9/ostream:476:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:482:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
operator<<(basic_ostream<char, _Traits>& __out, char __c)
^
/usr/include/c++/4.9/ostream:482:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:488:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^
/usr/include/c++/4.9/ostream:488:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘signed char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:493:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^
/usr/include/c++/4.9/ostream:493:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘unsigned char’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:513:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^
/usr/include/c++/4.9/ostream:513:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: mismatched types ‘const _CharT*’ and ‘std::vector<int>’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/ostream:612:0,
from /usr/include/c++/4.9/iostream:39,
from ctest.cpp:1:
/usr/include/c++/4.9/bits/ostream.tcc:321:5: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^
/usr/include/c++/4.9/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const char*’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:530:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^
/usr/include/c++/4.9/ostream:530:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const char*’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:543:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^
/usr/include/c++/4.9/ostream:543:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const signed char*’
*_M_stream << __value;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from ctest.cpp:1:
/usr/include/c++/4.9/ostream:548:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^
/usr/include/c++/4.9/ostream:548:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.9/iterator:66:0,
from ctest.cpp:6:
/usr/include/c++/4.9/bits/stream_iterator.h:198:13: note: cannot convert ‘__value’ (type ‘const std::vector<int>’) to type ‘const unsigned char*’
*_M_stream << __value;
^
And this is the full file:
template <typename T>
std::ostream& operator << (std::ostream& out, const std::vector<T>& v){
if(! v.empty()){
std::cout<< '[';
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout<< ']' << std::endl;
}
else{
std::cout << "[ ]" << std::endl;
}
}
int main()
{
std::ifstream infile("filetest.txt");
std::string output;
std::string line;
std::vector<std::vector<int> > vec;
while(std::getline(infile, line))
{
std::vector<int> subvec;
int i;
std::istringstream iss(line);
while(iss >> i)
{
subvec.push_back(i);
if(iss.peek()==','|| iss.peek()== ' ')
iss.ignore();
}
std::cout << subvec;
vec.push_back(subvec);
}
std::cout << vec;
}
It depends heavily upon how you want your output formatted. You can just use the operator as-is, and it'll work (though you may or may not like the formatting):
#include <vector>
using std::vector;
#include <iostream>
using std::ostream;
template<typename T>
ostream& operator<< (ostream& out, const vector<T>& v) {
out << "[";
size_t last = v.size() - 1;
for (size_t i = 0; i < v.size(); ++i) {
out << v[i];
if (i != last)
out << ", ";
}
out << "]";
return out;
}
int main() {
std::vector<std::vector<int> > x { { 1, 2, 3 }, { 4, 5, 6 } };
std::cout << x;
}
Produces:
[[1, 2, 3], [4, 5, 6]]
Things only get more difficult if you need/want to change the formatting between one instance and another, such as having commas between the numbers, and new-lines between the rows. This is somewhat non-trivial to solve well, but I'll leave it at this for now, since it's not at all clear you really even want that.
This idea uses std::enable_if to iterate through members of what we choose to define as is_container. I wrote the code more out of curiosity than out of practicality though.
#include <iostream>
#include <type_traits>
#include <vector>
// by default you declare that no type is a container
template<typename T>
struct is_container {
static constexpr bool value = false;
};
// then you specify which types are actually containers.. I'm just declaring
// std::vector<T, A>, but you could make a similar declaration for list, array,
// and so on.
template<typename T, typename Allocator>
struct is_container<std::vector<T, Allocator> > {
static constexpr bool value = true;
};
// then you enable a print function to print all elements in a container (you
// can modify the actual formatting to your liking)
template<typename T>
typename std::enable_if<
is_container<T>::value,
std::ostream& >::type
inline operator<<(std::ostream& os, const T& container) {
const auto N = container.size();
std::size_t current = 0;
os << "[";
for(const auto & item : container) {
const char* separator = current++ == N - 1 ? "" : ", ";
os << item << separator;
}
os << "]";
return os;
}
// then the code will work for any level of nesting
int main() {
int scalar = 1;
std::cout << "Scalar: " << scalar << std::endl;
std::vector<int> vector {1, 2, 3, 4};
std::cout << "Vector: " << vector << std::endl;
std::vector< std::vector<int> > vvector {
{1, 2, 3, 4},
{5, 6, 7},
{8, 9, 10, 11, 12}
};
std::cout << "Vector<Vector>: " << vvector << std::endl;
std::vector< std::vector< std::vector<int> > > vvvector {
{
{1, 2, 3, 4},
{5, 6, 7},
{8, 9, 10, 11, 12}
},
{
{-1, -2, -3, -4},
{-5, -6, -7},
{-8, -9, -10}
}
};
std::cout << "Vector<Vector<Vector>>: " << vvvector << std::endl;
// you get the idea...
return 0;
}
Sample run:
$ g++ example.cpp -std=c++14 -Wall -Wextra -O3
$ ./a.out
Scalar: 1
Vector: [1, 2, 3, 4]
Vector<Vector>: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10, 11, 12]]
Vector<Vector<Vector>>: [[[1, 2, 3, 4], [5, 6, 7], [8, 9, 10, 11, 12]], [[-1, -2, -3, -4], [-5, -6, -7], [-8, -9, -10]]]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The code is given as follows:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }
int main()
{
string operation;
double num1, num2, val;
cout << "Enter the operation followed by the numbers\n";
cin >> operation >> num1 >> num2 >> endl;
if (operation == "+") {
cout << operation << num1 << num2 << endl;
val = num1 + num2;
cout << val << endl;
}
if (operation == "-") {
cout << operation << num1 << num2 << endl;
val = num1 - num2;
cout << val << endl;
}
if (operation == "*") {
cout << operation << num1 << num2 << endl;
val = num1 * num2;
cout << val << endl;
}
if (operation == "/") {
cout << operation << num1 << num2 << endl;
val = num1 / num2;
cout << val << endl;
}
}
I am getting the following error:
operands.cpp: In function ‘int main()’:
operands.cpp:13:39: error: no match for ‘operator>>’ in ‘(&(& std::operator>> [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]((* & std::cin), (* & operation)))->std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]((* & num1)))->std::basic_istream<_CharT, _Traits>::operator>> [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]((* & num2)) >> std::endl’
operands.cpp:13:39: note: candidates are:
/usr/include/c++/4.6/istream:122:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:122:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}’
/usr/include/c++/4.6/istream:126:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/include/c++/4.6/istream:126:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/4.6/istream:133:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:133:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/4.6/istream:169:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:169:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘bool&’
/usr/include/c++/4.6/istream:173:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.6/istream:173:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short int&’
/usr/include/c++/4.6/istream:176:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:176:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘short unsigned int&’
/usr/include/c++/4.6/istream:180:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.6/istream:180:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int&’
/usr/include/c++/4.6/istream:183:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:183:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘unsigned int&’
/usr/include/c++/4.6/istream:187:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:187:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long int&’
/usr/include/c++/4.6/istream:191:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:191:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long unsigned int&’
/usr/include/c++/4.6/istream:196:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:196:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long int&’
/usr/include/c++/4.6/istream:200:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:200:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long long unsigned int&’
/usr/include/c++/4.6/istream:205:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:205:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘float&’
/usr/include/c++/4.6/istream:209:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:209:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘double&’
/usr/include/c++/4.6/istream:213:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:213:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘long double&’
/usr/include/c++/4.6/istream:217:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
/usr/include/c++/4.6/istream:217:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void*&’
/usr/include/c++/4.6/istream:241:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
/usr/include/c++/4.6/istream:241:7: note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}’
/usr/include/c++/4.6/bits/basic_string.tcc:998:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/istream:752:5: note: std::basic_istream<_CharT2, _Traits2>& std::operator>>(std::basic_istream<_CharT2, _Traits2>&, _CharT2*) [with _CharT2 = char, _Traits2 = std::char_traits<char>, _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.6/istream:752:5: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘char*’
/usr/include/c++/4.6/bits/istream.tcc:925:5: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.6/bits/istream.tcc:925:5: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘char&’
/usr/include/c++/4.6/istream:709:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
/usr/include/c++/4.6/istream:714:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
/usr/include/c++/4.6/istream:756:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
/usr/include/c++/4.6/istream:761:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
Don't put >> endl after cin ! That is, replace
cin >> operation >> num1 >> num2 >> endl;
with
cin >> operation >> num1 >> num2;
hello i'm trying to overload my operator >> to my class but i ecnouter an error in eclipse.
code:
friend istream& operator>>(const istream& is, const RAngle& ra){
return is >> ra.x >> ra.y;
}
code2:
friend istream& operator>>(const istream& is, const RAngle& ra)
{
is >> ra.x;
is >> ra.y;
return is
}
Both crash and i don't know why, please help.
EDIT:
ra.x & ra.y are both 2 private ints of my class;
Full error:
error:
..\/rightangle.h: In function 'std::istream& operator>>(std::istream&, const RAngle&)':
..\/rightangle.h:65:12: error: ambiguous overload for 'operator>>' in 'is >> ra.RAngle::x'
..\/rightangle.h:65:12: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:122:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:122:7: note: no known conversion for argument 1 from 'const int' to 'std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:126:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>] <near match>
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:126:7: note: no known conversion for argument 1 from 'const int' to 'std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:133:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:133:7: note: no known conversion for argument 1 from 'const int' to 'std::ios_base& (*)(std::ios_base&)'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:241:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>] <near match>
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:241:7: note: no known conversion for argument 1 from 'const int' to 'std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}'
..\/rightangle.h:66:12: error: ambiguous overload for 'operator>>' in 'is >> ra.RAngle::y'
..\/rightangle.h:66:12: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:122:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:122:7: note: no known conversion for argument 1 from 'const int' to 'std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:126:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>] <near match>
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:126:7: note: no known conversion for argument 1 from 'const int' to 'std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:133:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:133:7: note: no known conversion for argument 1 from 'const int' to 'std::ios_base& (*)(std::ios_base&)'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:241:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>] <near match>
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/istream:241:7: note: no known conversion for argument 1 from 'const int' to 'std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}''
Since operator>> is going to modify the parameters you pass to it, they cannot be references to const, so what you want is something like:
friend std::istream &operator>>(std::istream &is, RAngle &ra) {
return is >> ra.x >> ra.y;
}
Note that almost any I/O operation also (at least potentially) changes the state of the stream, so you (nearly?) always want to pass stream parameters by non-const reference.
One problem is in the signature of the operator:
friend istream& operator>>(const istream& is, const RAngle& ra){
// ^^^^^ ^^^^^
return is >> ra.x >> ra.y;
}
If the arguments are const you will not be able to modify them. As of the error messages, they seem to indicate that the compiler is failing to find an appropriate overload for is >> ra.x and is >> ra.y. This might be related to the previous issue (cannot find an overload that takes a const-lvalue) or not, depending on what the types of ra.x and ra.y are (which is not shown in the question).
Please remove const from the parameter "const RAngle& ra". You cannot modify a constant reference from a method.
I have an enum in my code that is the following: enum Status {In-Active, Active};.
A status object is passed to a Person object as a parameter, so I want the user to enter Active or In-Active and save them in a Status variable. How can I do this? I tried it at first but it didnt work.
Here is my code:
#include <iostream>
using namespace std;
enum Status {InActive, Active};
class Person {
private:
string name;
int age;
Status status;
public:
Person (const string &_name, const int _age, const Status _status) : name(_name), age(_age), status(_status) {}
string &getName () { return name; }
int getAge () { return age; }
Status getStatus () { return status; }
void setName (const string &_name) { name = _name; }
void setAge (const int _age) { age = _age; }
void setStatus (const Status _status) { status = _status; }
};
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Main Method //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int main () {
string name;
int age;
Status status;
cout << "Enter a name: "; getline(cin, name);
cout << "Enter age: "; cin >> age;
cout << "Enter status: "; cin >> status;
Person p(name, age, status);
return 0;
}
and here is the error I got (this is the build log):
Compiling: C:\Users\KiKo-SaMa\Desktop\C++\Person.cpp
C:\Users\KiKo-SaMa\Desktop\C++\Person.cpp: In function 'int main()':
C:\Users\KiKo-SaMa\Desktop\C++\Person.cpp:36:38: error: no match for 'operator>>' in 'std::cin >> status'
C:\Users\KiKo-SaMa\Desktop\C++\Person.cpp:36:38: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:122:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:122:7: note: no known conversion for argument 1 from 'Status' to 'std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:126:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:126:7: note: no known conversion for argument 1 from 'Status' to 'std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:133:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:133:7: note: no known conversion for argument 1 from 'Status' to 'std::ios_base& (*)(std::ios_base&)'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:169:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:169:7: note: no known conversion for argument 1 from 'Status' to 'bool&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:173:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:173:7: note: no known conversion for argument 1 from 'Status' to 'short int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:176:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:176:7: note: no known conversion for argument 1 from 'Status' to 'short unsigned int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:180:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:180:7: note: no known conversion for argument 1 from 'Status' to 'int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:183:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:183:7: note: no known conversion for argument 1 from 'Status' to 'unsigned int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:187:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:187:7: note: no known conversion for argument 1 from 'Status' to 'long int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:191:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:191:7: note: no known conversion for argument 1 from 'Status' to 'long unsigned int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:196:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:196:7: note: no known conversion for argument 1 from 'Status' to 'long long int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:200:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:200:7: note: no known conversion for argument 1 from 'Status' to 'long long unsigned int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:205:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:205:7: note: no known conversion for argument 1 from 'Status' to 'float&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:209:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:209:7: note: no known conversion for argument 1 from 'Status' to 'double&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:213:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:213:7: note: no known conversion for argument 1 from 'Status' to 'long double&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:217:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:217:7: note: no known conversion for argument 1 from 'Status' to 'void*&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:241:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:241:7: note: no known conversion for argument 1 from 'Status' to 'std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.tcc:998:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/istream.tcc:957:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/istream.tcc:925:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:709:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:714:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:756:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:761:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
Process terminated with status 1 (0 minutes, 0 seconds)
43 errors, 0 warnings
where did my code go wrong?
Thanks
You can't read enum values directly, you'll need a std::map that maps user input to an enum value.
std::map<std::string,Status> m;
m["In-Active"] = In-Active;
m["Active"] = Active;
std::string sstatus;
cin >> sstatus;
Status status = m[sstatus];
Enum objects are being parsed at compile time and it can only contain constant integer values.
Person p(name, age, status); //Here without using status object you should send a value directly like "In-Active" or "Active". So there would be no input from the user regarding th status field.
enum Status {InActive, Active};
Here Status is a enum class and any object created would contain values "Inactive" or "Active". If you try to assign some other value you would get an compiler error.
Status status;
Here status is an enum object created and it contains garbage value.
cout << "Enter status: "; cin >> status;
Here you are asking user for an input for an enum object which is totally invalid for the compiler.
You can also wrap the enum inside a class and provide operator>>. This is a modified example from this link http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Type_Safe_Enum
#include <iostream>
template<typename def>
class safe_enum : public def
{
public:
int val;
safe_enum(int v) : val(v) {}
// inner underlying() const { return val; }
bool operator == (const safe_enum & s) const { return this->val == s.val; }
bool operator != (const safe_enum & s) const { return this->val != s.val; }
bool operator < (const safe_enum & s) const { return this->val < s.val; }
bool operator <= (const safe_enum & s) const { return this->val <= s.val; }
bool operator > (const safe_enum & s) const { return this->val > s.val; }
bool operator >= (const safe_enum & s) const { return this->val >= s.val; }
};
struct status_def {
enum type { NoValue, InActive, Active };
};
typedef safe_enum<status_def> Status;
std::istream& operator>>(std::istream& Istr, Status& other)
{
std::cout << "Enter Status value: " << std::endl;
//here the logic to handle input error
Istr >> other.val;
return Istr;
}
std::ostream& operator<<(std::ostream& os,const Status& toPrint)
{
if (toPrint.val == Status::Active)
return os << "Active";
if (toPrint.val == Status::NoValue)
return os << "NoValue";
if (toPrint.val == Status::InActive)
return os << "InActive";
throw;
}
int main(void)
{
Status test(Status::NoValue);
std::cout << test << std::endl;
std::cin >> test;
std::cout << test << std::endl;
}