-2

I have seen similar answers, but I don't seem to be able to solve mine just by looking at these for example ( this or that one for example).

So, I am having that .

A.h

#ifndef INCLUDE_CLASS_NAME
#define INCLUDE_CLASS_NAME

#include <B.h>

using namespace C;

D::DPtr myvariable;  <-- Error in here

#endif

In the include B.h I have this :

namespace C{
namespace E{

class D
{
  public:
      typedef shared_ptr<D> DPtr;
}

} //end of namespace E
} // end of namespace C

Why am I getting this error in the mentioned line :

'D'  does not name a type

I am including the .h file, which defines the class. What am I missing?

Community
  • 1
  • 1
ghostrider
  • 5,131
  • 14
  • 72
  • 120

3 Answers3

5

The symbol D is inside namespace E, which is inside namespace C, so the fully qualified name is C::E::D.

So either:

  • Add E:: to refer to D correctly:

    mutable E::D::DPtr myvariable;
    
  • Declare E in the using directive as well:

    using namespace C::E;
    
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
4

You missed namespace E...

mutable E::D::DPtr myvariable; // should work
marom
  • 5,064
  • 10
  • 14
0

You're trying to call a function that does not exist yet according to the preprocessor. You need to define functions before you call them:

int mainFunction(){
    int foo(int bar){ //define function FIRST...
        return bar;
    }
foo(42) //call it later.
}

This should get rid of the error. This does not:

int mainFunction(){
    foo(42) //trying to call a function before it's declared does not work.
        int foo(int bar){
        return bar;
    }
}
Mason Watmough
  • 495
  • 6
  • 19