Dec 9, 2010 at 4:20pm UTC
Hello,
This is my operator overloading :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
istream& operator >>(istream & in, vector<int > & final) {
string a;
int aa;
int num;
in>>a;
for (int i=0; i<a.size(); i++) {
aa=a[i];
num=aa-47;
final.push_back(num);
}
return in;
}
and in my main program :
1 2 3 4 5 6 7 8
ifstream fin("numbers.txt" );
if (!fin) {
cout << "Not open" ;
return 0;
}
vector<int > a;
fin>>a;
The file has numbers each on a separate line. I want to input each number into a vector of integers as one digit integers.
What am I missing ?
Cheers,
Last edited on Dec 9, 2010 at 4:22pm UTC
Dec 9, 2010 at 5:59pm UTC
1 2 3 4 5 6
for (size_t i = 0; i != a.size(); ++i)
{
char tmp[] = { a[i], 0 }; // a null terminated string of the char
int num = atoi(tmp); // convert char to a number
final.push_back(num);
}
Last edited on Dec 9, 2010 at 5:59pm UTC
Dec 9, 2010 at 6:22pm UTC
Ok, thanks. Although I still get this error :
Ambiguous overload for 'operator>>' in 'fin>>a'
when I try fin>>a; in the main.
Cheers,
Dec 9, 2010 at 8:05pm UTC
You must have something else in your program that can pass as a match.
Dec 9, 2010 at 8:21pm UTC
Found the mistake. Some stupid typo...
Thanks,
Cheers