error C2513 ifstream declaration

Pages: 12
so I need to edit the 'in' statement to 'ios::in' ?

and, why are they redefining 'in'?
I do not know about which statement you arre speaking. And I do not understand defining in as = in the header above.
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
29
30
/add words from file into lexicon
void Lexicon::addWordsFromFile(string filename)
{
	ifstream ios::in;
	ios::in.open(filename.c_str());

	string line; //read in line
	
	//opens xml text file and reads in activity code, location and date fields
	while (true) {
		getline(ios::in, line); //read each line
		if (ios::in.fail()) break; //boilerplate check for error
		
		line.erase(0, 1); //remove first tab
		if(line == "</ScheduleTasks>") break; //check for end of document
		
		//add readin of location codes and locations to vector array for matching later

		line.erase(0, 1); //remove second tab
		if(line.substr(1, 15) == "ScheduleTask ID") //look for activity
			add (line.substr(42,16)); //store activity code
		line.erase(0, 2); //remove third and fourth tab
		if(line.substr(1, 17) == "Timing LocationID")//look for location
			add1 (line.substr(19,16)); //store location code
		line.erase(0, 1); //remove fifth tab
		if(line.substr(1, 13) == "Planned Begin") //look for dates
			add2 (line.substr(17,26), line.substr(41,50)); //store start date yyyy-mm-dd and store finish date yyyy-mm-dd
	}
	ios::in.close(); //boilerplate close file line
}


is this correct?
Last edited on
No, it is an invalid declaration

ifstream 'ios::in;


Your shall insert a name of the variable. You can use any other name instead of in.

But iit is much better to remove declaration of the manifest constant "in".
Last edited on
so something like this will work,

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
29
30
void Lexicon::addWordsFromFile(string filename)
{
	ifstream ontology;
	ontology.open(filename.c_str());

	string line; //read in line
	
	//opens xml text file and reads in activity code, location and date fields
	while (true) {
		getline(ontology, line); //read each line
		if (ontology.fail()) break; //boilerplate check for error
		
		line.erase(0, 1); //remove first tab
		if(line == "</ScheduleTasks>") break; //check for end of document
		
		//add readin of location codes and locations to vector array for matching later

		line.erase(0, 1); //remove second tab
		if(line.substr(1, 15) == "ScheduleTask ID") //look for activity
			add (line.substr(42,16)); //store activity code
		line.erase(0, 2); //remove third and fourth tab
		if(line.substr(1, 17) == "Timing LocationID")//look for location
			add1 (line.substr(19,16)); //store location code
		line.erase(0, 1); //remove fifth tab
		if(line.substr(1, 13) == "Planned Begin") //look for dates
			add2 (line.substr(17,26), line.substr(41,50)); //store start date yyyy-mm-dd and store finish date yyyy-mm-dd
	}
	ontology.close(); //boilerplate close file line
}


but changing the foreach.h file and commenting the line
//static const ios::openmode IOS_IN = ios::in;

is better?
I meant the definition of manifest constant in.
I can hack at this and //#define in = but I'd like to understand why is this here in the first place and why i should comment it out.
but I'd like to understand why is this here


It's there because someone was trying to be cute and redefine the C++ language by abusing macros. This is always a bad idea because it leads to problems like the ones you are having now.

Someone wanted to make C# style foreach loops in C++, and to do that, they tried to hijack 'in' and make it a keyword.

It was foolish. Whoever did it should be ashamed of themselves.

and why i should comment it out.


Because that #define is polluting the namespace, replacing all instances of 'in' with '='.

Unless you are using 'in' for the purpose of writing those foreach loops, that #define is of no value to you, and it is just wrecking other areas of your code. So it should be removed.
Last edited on
that is a good clear explanation for the 'why'

there are three classes that use the foreach class, so if I change foreach then those classes may not work, they are: set.h, vector.h, and bst.h

I looked in bst.h but do not know what to look for to know how 'in' is used; this is the closest I found

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
29
/*
 * Method: iterator
 * Usage: iter = bst.iterator();
 * -----------------------------
 * This method creates an iterator that allows the client to
 * iterate through the elements in this binary search tree.  The
 * order of elements produced by the iterator is that of an InOrder
 * walk of the tree.
 *
 * The idiomatic code for accessing elements using an iterator is
 * to create the iterator from the collection and then enter a loop
 * that calls next() while hasNext() is true, like this:
 *
 *     BST<string>::Iterator iter = bst.iterator();
 *     while (iter.hasNext()) {
 *         string key = iter.next();
 *         . . .
 *     }
 *
 * This pattern can be abbreviated to the following more readable form:
 *
 *     foreach (string key in bst) {
 *         . . .
 *     }
 *
 * To avoid exposing the details of the class, the definition of the
 * Iterator class itself appears in the private/bst.h file.
 */
	Iterator iterator();
Last edited on
I noticed that in some of the library classes they used 'cin' rather than 'in' - so I changed to that; I will leave the foreach class alone.

thank you for your help, the compiler errors for fstream are now resolved
Last edited on
closed account (o3hC5Di1)
Hi there,

std::cind is a keyword in c++, I wouldn't change the name to that if I were you, it would only make things more ambiguous.

All the best,
NwN
I used 'ontology'
Topic archived. No new replies allowed.
Pages: 12