function not within scope

Jun 30, 2012 at 4:30pm
I am having trouble figuring out why I am getting an out of scope error here. Here is the code.

parser.cpp

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
#include<iostream>
#include<string>
#include<vector>

#include "tinyxml.h"
#include "tinystr.h"
#include "tinyxml.cpp"
#include "tinystr.cpp"
#include "tinyxmlerror.cpp"
#include "tinyxmlparser.cpp"
#include "interfaceInterpreter.cpp"

using namespace std;

void dump_to_stdout( TiXmlNode* pParent, unsigned int indent = 0 )
{
	if ( !pParent ) return;

	TiXmlNode* pChild;
	TiXmlText* pText;
	int t = pParent->Type();
	int num;
	
	switch ( t )
	{

	case TiXmlNode::TINYXML_ELEMENT:
		interpreter.pushTree(pParent->Value());
		break;

	case TiXmlNode::TINYXML_TEXT:
		pText = pParent->ToText();
		break;
	
	default:
		break;
	}

	for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
	{
		dump_to_stdout( pChild, indent+1 );
	}
	
	if (pParent->Type()==TiXmlNode::TINYXML_ELEMENT) interpreter.popTree();
}

void load(const char* pFilename)
{
	TiXmlDocument doc(pFilename);
	bool loadOkay = doc.LoadFile();
	if (loadOkay)
	{
		dump_to_stdout( &doc ); // defined later in the tutorial
	}
	else
	{
		printf("Failed to load file \"%s\"\n", pFilename);
	}
}

int main()
{
	ParseInterpreter interpreter;
	load("file.xml");
	return 0;
}


interfaceInterpreter.cpp

1
2
3
4
5
6
7
8
9
10
11
#include<string>
#include<vector>
#include "interfaceInterpreter.h"

void ParseInterpreter::pushTree(std::string nextParent){
	parentTree.push_back(nextParent);
}

void ParseInterpreter::popTree(){
	parentTree.pop_back();
}


interfaceInterpreter.h

1
2
3
4
5
6
class ParseInterpreter{
	std::vector <string> parentTree;
	public:
	void pushTree(std::string);
	void popTree();
};

I am getting the following compile errors
1
2
3
parser.cpp: In function ‘void dump_to_stdout(TiXmlNode*, unsigned int)’:
parser.cpp:28: error: ‘interpreter’ was not declared in this scope
parser.cpp:44: error: ‘interpreter’ was not declared in this scope


I am not sure why interpreter would not be declared. In the main function I initialized an instance of the ParseInterpreter class and called it interpreter. The functions being called are public functions so any other function should be able to call them. Any suggestions?
Jun 30, 2012 at 4:35pm
And where is interpreter defined that you use in the function in the following statements?

1
2
	case TiXmlNode::TINYXML_ELEMENT:
		interpreter.pushTree(pParent->Value());
Jun 30, 2012 at 5:30pm
interpreter is an instance of the class ParseInterpreter which is defined in interfaceInterpreter.h
Jun 30, 2012 at 5:51pm
Your error is telling you that interpreter isn't declared as you know.

One of the problems with defining a global variable in a header is that it isn't shared properly between source objects and if you don't have protections in your headers, it will likely be defined twice.

Define interpreter in your source file to ensure that it is created properly.

If you want to share the same object in multiple source files, then define it in one source file AND in the header with the extern keyword in front of the declaration.
Jun 30, 2012 at 5:55pm
You did not show the object definition.
Jun 30, 2012 at 6:03pm
It is clear that I am not using the object correctly. I do not understand why defining the class in the headerfile, linking to it and then defining an instance of the class in the source file does not place that object within the scope of the source file.

I am not sure where you suggest that I place the extern keyword.

Vlad, I admit that I am not sure exactly what it is that you are asking of me. If I have not shown the object definition, I am not sure what I would need to provide in order to do so.
Jun 30, 2012 at 6:17pm
You should show its definition in your header where as you said it is defined.
Last edited on Jun 30, 2012 at 6:17pm
Jun 30, 2012 at 6:22pm
I thought what I had written in the header file was the definition of the class and therefore any instance of the class. I seem to be wrong about this.
Jun 30, 2012 at 8:19pm
At some point in your code (likely at the top of one of your source files), you'll have to define interpreter as a ParseInterpreter.

I just noticed that it is defined in your main() at line 63. That object is created in your main, but no other functions can see it.

To make the other functions see it you can do one of the following:
1) Move line 63, to line 12 - AKA global scope (easier)
2) Add a ParseInterpreter object as an argument to dump_to_stdout() (better coding).
Jul 2, 2012 at 6:42pm
I made the declaration global just to see if the code would work. I was able to compile and run the code but now after trying to make some improvements I am running into another problem. It is related but different enough that I will post it in a new thread.
Topic archived. No new replies allowed.