list with overloading operator and a constructor

hey, if I have to call a file.txt to open it and pick up all it's content, should I use a constructor for the name of the file?
I tried this for the constructor.
1
2
Countries::Countries(const string& fileName):nameFile(nameFile){
}

and for the overloading operator , i just get the content of the file repeating many times.
And i don't know what is wrong with it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ostream& operator <<(ostream& out, const Countries& counName){
		list <string> myList;
		ifstream myflux("countries.txt");
		if(myflux){
                        string data;
                        while(myflux>>data)
                               myList.push_back(data);
			out<<countName;
			list <string>::const_iterator it;
			
			for(it = myList.begin(); it != myList.end(); it++){
				out<<*it<<endl;
			
			}
		}
	return out;
}

can somebody guide me through this !?
In response to your first question it LOOKS as if you are intending to put fileName into the data member called nameFile, in which case the constructor should read
Countries::Countries(const string& fileName):nameFile(fileName){}
and not
Countries::Countries(const string& fileName):nameFile(nameFile){}
(Be careful not to confuse nameFile and fileName!)

To be honest, I'm not quite sure what your second code fragment is doing, but I would be surprised if it compiled whilst countName is referred to in the body, but counName in the argument list: this may be a typo.

Also, you don't appear to close your input stream anywhere - I would expect to see
myflux.close()
somewhere.

I would be inclined to return a list<string> from the function and let whatever called it deal with the output.
hi,
Thank you for the answer.
I will try to make the first one working.

The second code is supposed to display the content of the linked list i created.
The linked list contains the countries names that i Collected from the file "countries.txt"

I think i figured it out by removing the line #8
I don't know if it is correct, but it is working.
the overloading operator is displaying the content of my linked list
Last edited on
Topic archived. No new replies allowed.