Header File Error

I'm getting this error:

error: `root' undeclared (first use this function)

in multiple places in my header file. I have 'root' defined in another header file as:

protected:
nodeType<elemType> *root;

so I am unsure why I'm getting the error.
Can anyone help?
Are you sure you're in the right scope?

It looks like root is part of some class. Maybe you forgot to put those member functions in the class.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// myclass.h
class MyClass
{
public
  int AFunction();

protected:
  int root;
};

//==========
//  myclass.cpp

#include "myclass.h"

int AFunction()  // notice, forgot the scope operator, therefore this function is global
{
  return root;  // ERROR, 'root' doesn't exist in the global namespace
}

int MyClass::AFunction() // here we have the scope operator -- so we're in MyClass
{
  return root;  // OK, 'root' exists in MyClass so no error here.
}
It is under a template class

template <class elemType>
class binaryTreeType
{
.
.
.
protected:
nodeType<elemType> *root;
}

and it's being called in another header file which includes the class

template<class elemType>
void guessingTreeType<elemType>::CreateTree(nodeType<elemType> &rootNode)
{
root = &rootNode; <- here is where i get the error
}
root looks like it's a part of the binaryTreeType class... but your function is part of the guessingTreeType class.

Does guessingTreeType have a 'root' variable?
I think I've read about this before (assuming that guessingTreeType is a binaryTreeType). I think you have to explicitly tell it where to find root but I forget the details of why. Try this->root.
Topic archived. No new replies allowed.