>> operator not working for ifstream

So I have this function I'm working and I've been stuck on it for hours, in short I'm trying to import a file and add it contents to a vector but when ever I try to run the program I get an error saying

"Error C2664 'voidstd::vector<std::string,std::allocator<std::string>>::push_back(const _Ty &)': cannot convert argument 1 from 'int' to 'const _Ty &' "

I'm not really sure what could be the problem, I'm thinking it could be the file it's self containing both names and numbers and its not able to convert that. or it could be that the file is not open.


Here's the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

template< typename V, typename W >
void Digraph<V, W>::getDigraph(ifstream& inFile)
{

	vertex<V, W>  v;
	edge_representation<V, W> e;
	std::list< edge_representation<V, W> > edgeList;
	std::vector<vertex<V, W> > graph;


	
	std::vector<string> test;


	while(inFile.eof)
	{	
		

		for(int i =0; i < 54; i++)
		{
			inFile >> test.push_back(i);
			cout << test[i] << endl;
		}
	}

	cout << "calling getDigraph()";
}
Last edited on
@binoNocino,
There are several lines in "your" code that are causing me the jitters.
push_back() is a member function, and you are (presumably) inputting a string, not a member function. Probably you can replace
inFile >> test.push_back(i);
with
1
2
3
string something;
inFile >> something;
test.push_back( something );

Alternatively, if (and only if) you had predeclared test[] with the appropriate number of entries then you could input directly into it without using push_back() at all:
inFile >> test[i];
But there is not enough information about what else you are doing or the format of the input file to decide which is more appropriate.


As far as I can see there is no way of getting the values in vector<string> test out of this function, so something needs to be refactored. You haven't shown enough code to advise.


The line
while(inFile.eof)
is giving me nightmares. But if you know that there are 54 strings to input (54 is another magic number, BTW) then you don't need that outer loop anyway.


This looks a bit of an odd way to input a directed graph: why would the entries in the input file be strings?


Your code looks as if you are trying to amend somebody else's function.
Last edited on
Consider this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
template< typename V, typename W >
void Digraph<V, W>::getDigraph(ifstream& inFile)
{

	vertex<V, W>  v;
	edge_representation<V, W> e;
	std::list< edge_representation<V, W> > edgeList;
	std::vector<vertex<V, W> > graph;


	
	std::vector<string> test;


		for(int i =0; i < 54; i++)
		{
string s;
if(inFile >> s)
{
			test.push_back(s);
			cout << test.back() << endl;
}
else
  break;
		}

	cout << "calling getDigraph()";
}
Topic archived. No new replies allowed.