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;
}
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};
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;
}