error C2513 ifstream declaration

Pages: 12
error C2513: 'std::basic_ifstream<_Elem,_Traits>' : no variable declared before '='

the first line 'ifstream in;' does not compile

it seems like I am declaring 'in', and there is no '=', so I am confused on this one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "stdafx.h"

#include <iostream>
#include <string>
#include <fstream>

#include "genlib.h" 
#include "iterator.h"
#include "lexicon.h"
#include "set.h" 
#include "simpio.h" 
#include "strutils.h"


using namespace std;


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
void Lexicon::addWordsFromFile(string filename)
{
	ifstream in;
	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(in, line); //read each line
		if (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
	}
	in.close(); //boilerplate close file line
}
Last edited on
Did you #include <fstream> ?
yes, I edited the post to include the header files
And does the error still exist?
yes

this function has several errors
line 4
- C2513: 'std::basic_ifstream<_Elem,_Traits>' : no variable declared before '='
- C2059: syntax error : ';'
- C2143: syntax error : missing ';' before '='

line 10
- C2143: syntax error : missing ')' before '='
- C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 0 provided
- C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 0 provided
- C2059: syntax error : ','
- C2059: syntax error : ')'

line 11
- C2059: syntax error : '='

line 28
- C2143: syntax error : missing ';' before '='

With that many errors from what appears is good C++, my intuition is that I am missing something simple; a compiler setting.






Last edited on
The error message points to the assignment operator =. So I think that the invalid code is above the statement you showed.
There might be some macro mangling going on.

one of your headers might be doing #define in so that it can "clarify" parameters:
void function(in int parameter1, out int& parameter2 )

Try renaming 'in' to something else, or see if in is #defined:

1
2
3
4
// after your includes
#ifdef in
#undef in
#endif 




Remember kids.. macros are evil...
yes, in CS 106 A & B they taught me just enough about fire to burn myself - I never took CS106C, the last section of the three part sequence.

Is this correct headers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"

#include <iostream>
#include <string>
#include <fstream>
//#include <set>

#include "genlib.h" 
#include "iterator.h"
#include "lexicon.h"
#include "set.h" 
#include "simpio.h"
#include "strutils.h"

#ifdef in
#undef in
#endif 


those errors are gone, now I have 16 linker errors - do I need to check if they are related to the new headers or should I just start resolving these as a separate issue?

Also, point me in the right direction to learn what you know about what these are and why they fixed the errors
1
2
3
#ifdef in
#undef in
#endif 
Last edited on
This
#ifdef in
#undef in
#endif
means that somewhere in your headers you declared name in.
meaning 'name in' is buried somewhere in one of the referenced header files, or a header referenced by a referenced header?

or are you mean that this
#ifdef in
#undef in
#endif

was in my header

It is now since I added it, but it was not there before.
Last edited on
I have said clear enough in some of your included headers there is a declaration of in. Look through your headers.
closed account (o3hC5Di1)
Hi there,

Referring to Disch's message (http://cplusplus.com/forum/beginner/73146/#msg390750 ).

#define in defines "in" to the preprocessor, it could be assigned a value, making it a constant, like so: #define in 34 .
This would replace every occurrence of 'in' within your code by number 34.

So, if one of your headers already defines "in", and later it is redefined by another header, this causes confusion and breaks stuff.

So, in order to check for its existence you use
1
2
3
#ifdef in   //if defined in
#undef in  //undefine in
#endif //stop if statement 


Hope that helps.

All the best,
NwN
I dug through the header files and the header files referenced by the header files, and the associated .cpp files, and I do not see #define in - in explorer searched the project file for #define in and did not find #define in

do I need the <fstream> .h and .cpp file or is it already included in the compiler library?
Last edited on
You can do simply

#include "stdafx.h"

#include <iostream>
#include <string>
#include <fstream>
//#include <set>

#include "genlib.h"
#ifdef in
#error in is defined
#endif

#include "iterator.h"
#ifdef in
#error in is defined
#endif

#include "lexicon.h"
#ifdef in
#error in is defined
#endif

#include "set.h"
#ifdef in
#error in is defined
#endif

#include "simpio.h"
#ifdef in
#error in is defined
#endif

#include "strutils.h"

#ifdef in
#error in is defined
#endif

And the error message will show which header does contain declaration of in if it is a manifest constant.
lexicon.h

fatal error C1189: #error : in is defined

I do not see 'in' appear anywhere in the file?
Last edited on
So if you did not find in in this header you can insert suggested check around all other headers in this header. Also usually the error message reports the program line number.
is it because lexicon.cpp has lexicon.h as a header? It is referencing itself.
If you have cyclic references of included files then it is possible.
found it

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * File: foreach.h
 * Last modified on Thu Jun 11 12:04:09 2009 by eroberts
 * -----------------------------------------------------
 * This interface defines the foreach keyword, which is used to
 * simplify iteration.  All iterable classes import this interface,
 * so clients never need to do so explicitly.
 */

#ifndef _foreach_h
#define _foreach_h

#include "genlib.h"

/* These #includes are for files that contain "in" as a token */

#include <ios>
#include <fstream>
#include <sstream>

/* Redefine the ios constants (one of which is "in") */

static const ios::openmode IOS_APP = ios::app;
static const ios::openmode IOS_ATE = ios::ate;
static const ios::openmode IOS_BINARY = ios::binary;
static const ios::openmode IOS_IN = ios::in;
static const ios::openmode IOS_OUT = ios::out;
static const ios::openmode IOS_TRUNC = ios::trunc;

/*
 * Class: FE_Iterator
 * ------------------
 * This class is a base class for all Iterators that can work with
 * the foreach construct.  The only purpose of this class is to make
 * it possible to freeing the iterators after they are no longer needed.
 *
 * Note: FE_Iterator is implemented in lexicon.cpp, which is the only
 * iterable class that is not a template class.
 */

class FE_Iterator {
public:
	FE_Iterator();
	~FE_Iterator();
};

/*
 * Class: FE_State
 * ---------------
 * This class is used to maintain the state of the foreach processing.
 * The class itself is essentially private, but the implementations in
 * the different classes use the fields directly.
 *
 * Note: FE_State is implemented in lexicon.cpp, which is the only
 * iterable class that is not a template class.
 */

class FE_State {
public:
	int state;
	FE_Iterator *iter;

	FE_State();
	~FE_State();
};

/*
 * Macro: foreach
 * Usage: foreach (type var in collection) { . . . }
 * -------------------------------------------------
 * Provides a much simpler hook to the iterator facility.
 */

#define foreach(arg) \
  for (FE_State _fe; _fe.state < 2; ) \
    for (arg.foreachHook(_fe); _fe.state++ == 1; _fe.state = 0)

#define in =

#endif


also, the foreach.h file does not have a .cpp - why?
Last edited on
It need not any definitions.
Pages: 12