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;
}
Related
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};
This question already has an answer here:
Dependent name resolution & namespace std / Standard Library
(1 answer)
Closed 8 years ago.
I am trying to construct an ifstream_iterator on pair. My code is as follows:
typedef pair<float, int> T;
istream& operator>>(istream& stream, T& in) {
stream >> in.first >> in.second;
return stream;
}
int main(int argc, char **argv) {
ifstream infile("dummy2");
istream_iterator<T> iit(infile);
istream_iterator<T> eos;
while (iit != eos) {
cout << (*iit).first << endl;
++iit;
}
return 0;
}
I have a dummy file "dummy2" as follows:
"a" 4 "b" 5
I would like to output pairs {"a", 4} and {"b", 5}.
However, I get nasty compilation error
In file included from /usr/include/c++/4.6/iterator:66:0,
from filestream.cpp:9:
/usr/include/c++/4.6/bits/stream_iterator.h: In member function ‘void std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::_M_read() [with _Tp = std::pair<float, int>, _CharT = char, _Traits = std::char_traits<char>, _Dist = long int]’:
/usr/include/c++/4.6/bits/stream_iterator.h:70:9: instantiated from ‘std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type&) [with _Tp = std::pair<float, int>, _CharT = char, _Traits = std::char_traits<char>, _Dist = long int, std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type = std::basic_istream<char>]’
filestream.cpp:32:33: instantiated from here
/usr/include/c++/4.6/bits/stream_iterator.h:121:6: error: no match for ‘operator>>’ in ‘*((std::istream_iterator<std::pair<float, int> >*)this)->std::istream_iterator<std::pair<float, int> >::_M_stream >> ((std::istream_iterator<std::pair<float, int> >*)this)->std::istream_iterator<std::pair<float, int> >::_M_value’
/usr/include/c++/4.6/bits/stream_iterator.h:121:6: 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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 ‘std::pair<float, int>’ 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/bits/random.tcc:923:5: note: template<class _IntType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::uniform_int_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.tcc:968:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::uniform_real_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.tcc:1890:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::cauchy_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.h:3381:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::bernoulli_distribution&)
/usr/include/c++/4.6/bits/random.tcc:1733:5: note: template<class _RealType1, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::normal_distribution<_RealType1>&)
/usr/include/c++/4.6/bits/random.tcc:1063:5: note: template<class _IntType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::geometric_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.tcc:2085:5: note: template<class _RealType1, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::gamma_distribution<_RealType1>&)
/usr/include/c++/4.6/bits/random.tcc:1622:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::exponential_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.tcc:2143:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::weibull_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.tcc:2200:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::extreme_value_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.tcc:194:5: note: template<class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::linear_congruential_engine<_UIntType, __a, __c, __m>&)
/usr/include/c++/4.6/bits/random.tcc:491:5: note: template<class _UIntType, long unsigned int __w, long unsigned int __n, long unsigned int __m, long unsigned int __r, _UIntType __a, long unsigned int __u, _UIntType __d, long unsigned int __s, _UIntType __b, long unsigned int __t, _UIntType __c, long unsigned int __l, _UIntType __f, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>&)
/usr/include/c++/4.6/bits/random.tcc:642:5: note: template<class _UIntType, long unsigned int __w, long unsigned int __s, long unsigned int __r, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::subtract_with_carry_engine<_UIntType, __w, __s, __r>&)
/usr/include/c++/4.6/bits/random.tcc:709:5: note: template<class _RandomNumberEngine, long unsigned int __p, long unsigned int __r, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::discard_block_engine<_RandomNumberEngine, __p, __r>&)
/usr/include/c++/4.6/bits/random.tcc:815:5: note: template<class _RandomNumberEngine, long unsigned int __k, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::shuffle_order_engine<_RandomNumberEngine, __k>&)
/usr/include/c++/4.6/bits/random.tcc:1138:5: note: template<class _IntType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::negative_binomial_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.tcc:1328:5: note: template<class _IntType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::poisson_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.tcc:1577:5: note: template<class _IntType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::binomial_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.tcc:1782:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::lognormal_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.tcc:1827:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::chi_squared_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.tcc:1936:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::fisher_f_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.tcc:1981:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::student_t_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.tcc:2308:5: note: template<class _IntType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::discrete_distribution<_IntType>&)
/usr/include/c++/4.6/bits/random.tcc:2488:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::piecewise_constant_distribution<_RealType>&)
/usr/include/c++/4.6/bits/random.tcc:2690:5: note: template<class _RealType, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::piecewise_linear_distribution<_RealType>&)
/usr/include/c++/4.6/bits/istream.tcc:957:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
/usr/include/c++/4.6/bits/istream.tcc:925:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
/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*)
/usr/include/c++/4.6/istream:852:5: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = std::pair<float, int>] <near match>
/usr/include/c++/4.6/istream:852:5: note: no known conversion for argument 1 from ‘std::istream_iterator<std::pair<float, int> >::istream_type {aka std::basic_istream<char>}’ to ‘std::basic_istream<char>&&’
/usr/include/c++/4.6/iomanip:70:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Resetiosflags)
/usr/include/c++/4.6/iomanip:100:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setiosflags)
/usr/include/c++/4.6/iomanip:131:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setbase)
/usr/include/c++/4.6/iomanip:169:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setfill<_CharT>)
/usr/include/c++/4.6/iomanip:199:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setprecision)
/usr/include/c++/4.6/iomanip:229:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Setw)
/usr/include/c++/4.6/iomanip:263:5: note: template<class _CharT, class _Traits, class _MoneyT> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::_Get_money<_MoneyT>)
make: *** [filestream] Error 1
I cannot quite make sense of what is happening here. Any help is appreciated.
Thanks a lot!
You'll note that simply doing this:
T t;
infile >> t;
Works like a charm! What is up?
std::pair is declared in namespace std.
istream_iterator<> uses operator>> indeed, and it does so using ADL at instantiation time (this is known as two-phase-lookup).
However, at instantiation time, it will not consider your operator>> as it doesn't exist in the namespace that implements istream_iterator and neither was it declared in any of the associated namespaces implied by the arguments (this is the ADL step).
You could be tempted "fix" it by defining your overload in the std namespace (and it works) but that is not allowed by the standard and technically results in Undefined Behaviour.
So you'd do better to introduce a type that allows ADL to kick in:
struct T : public std::pair<int,float> {};
Live On Coliru
#include <utility>
#include <iostream>
#include <iterator>
struct T : std::pair<float, int> {
using std::pair<float, int>::pair;
};
std::istream& operator>>(std::istream& stream, T& in) {
return stream >> in.first >> in.second;
}
int main() {
std::istream_iterator<T> iit(std::cin);
std::istream_iterator<T> eos;
while (iit != eos) {
std::cout << (*iit).first << std::endl;
++iit;
}
}
I suspect that theoretically you should be able to trick thing by reordering includes and injecting the overload early enough so it is "visible" during the first phase of name lookup. However, whether this option is available depends on the implementation and if you're out of luck you still couldn't achieve it without altering the standard library headers (don't do that).
This is a fairly common problem, for which there's no really clean solution. One that normally works is to put your operator into namespace std:
typedef pair<float, int> T;
namespace std {
istream& operator>>(istream& stream, T& in) {
stream >> in.first >> in.second;
return stream;
}
}
int main(int argc, char **argv) {
ifstream infile("dummy2");
istream_iterator<T> iit(infile);
istream_iterator<T> eos;
while (iit != eos) {
cout << (*iit).first << endl;
++iit;
}
return 0;
}
Officially, this gives undefined behavior--you can add some specializations to namespace std, but you can't add overloads there. Unofficially, I've yet to see a compiler/library where it caused a problem.
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 am constantly getting an ambiguous overload error no matter what I do ....
here's my code
COMPLEX.cpp code
template<class C> class complex
{
C real,imag;
public:
//constructors
complex(C real=0,C imag=0)
{
complex::real=real;
complex::imag=imag;
}
complex(const complex& cmplx)
{
real=cmplx.real;
imag=cmplx.imag;
}
//functions available
complex<C> operator+(const complex&);
complex<C> operator+(double);
void operator=(const complex&);
//friend functions available
template<class F> friend complex<F> operator+(double,const complex<F>&);
template<class F> friend istream& operator>>(istream&,const complex<F>&);
template<class F> friend ostream& operator<<(ostream&,const complex<F>&);
};
OPERATOR>> implementation
template<class F> istream& operator>>(istream& cin,const complex<F>& cmplx)
{
cout<<"\nenter real part followed by imaginary part : ";
cin>>cmplx.real>>cmplx.imag;
return cin;
}
MAIN method
int main(void)
{
complex<int> C1;
complex<int> C2;
complex<int> C3;
cin>>C1>>C2>>C3;
cout<<C1<<C2<<C3;
C1=C1+3;
C2=2+C2;
C3=C1+C2;
cout<<C1<<C2<<C3;
complex<int> C4(C3);
cout<<C4;
return 0;
}
COMPILER ERROR
COMPLEX.cpp: In function ‘std::istream& operator>>(std::istream&, const complex<C>&) [with F = int, std::istream = std::basic_istream<char>]’:
COMPLEX.cpp:91:7: instantiated from here
COMPLEX.cpp:75:2: error: ambiguous overload for ‘operator>>’ in ‘cin >> cmplx.complex<int>::real’
COMPLEX.cpp:75:2: 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>] <near match>
/usr/include/c++/4.6/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>&)}’
/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>] <near match>
/usr/include/c++/4.6/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>&)}’
/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>] <near match>
/usr/include/c++/4.6/istream:133:7: note: no known conversion for argument 1 from ‘const int’ to ‘std::ios_base& (*)(std::ios_base&)’
/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>] <near match>
/usr/include/c++/4.6/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>*}’
before this I wrote a MATRIX.cpp and istream overload works fine there even though its nearly the same as here....
template<class F> istream& operator>>(istream& cin,const complex<F>& cmplx)
^^^^^
That const is a problem - you need to change cmplx here, it can't be const. That's what the compiler is complaining about: cmplx.real is const int inside that function.