Problems inheriting from a class in the same namespace in C++
I was happily working in C++, until compilation time arrived.
I've got several classes inside some namespace (let's call it N); two of these classes correspond to one base class, and other derived from it. Each class has its own pair of .hpp and .cpp files; I think it'd look like:
namespace N{
class Base{
};
class Derived: public Base{
};
}
However, g++ (maybe linker) keeps telling me:
Derived.hpp:n: error: expected class-name before ‘{’ token
It doesn't recognize Base as a class, even when I have correctly #include'ed the hpp file corresponding to its definition to Derived's .hpp!
"It's something with #includes", I thought, since these classes' .hpps are #included in开发者_StackOverflow other files, so I added this to Derived declaration in Derived.hpp:
#include "Base.hpp"
namespace N{
class Base;
class Derived: public Base{
};
}
And now g++ complains:
Derived.hpp:n: error: invalid use of incomplete type ‘struct N::Base’
So, I got lost here. Please help me, I will apreciate it a lot. :)
(By the way, I'm rather experienced in Python, not C++, so this issues are really strange to me. Also, I changed classes' names and stuff :).
Edit: A more exact representation of my files is:
File Pieza.hpp
-----------------------
#include "Celda.hpp"
namespace Reglas
{
class Pieza
{
public:
Pieza() {}
virtual ~Pieza() {}
private:
Celda *c;
};
}
File Jugador.hpp
-----------------------
#include "Jugada.hpp"
#include "Excepciones.hpp"
#include "Pieza.hpp"
namespace Reglas
{
//compiler asked for these :S
class Celda;
class Tablero;
class Jugador : public Pieza
{
public:
Jugador() {}
virtual ~Jugador() {}
};
}
Derived.hpp:n: error: invalid use of incomplete type ‘struct N::Base’
This makes me think that you didn't #include "Base.hpp
in the Derived.cpp
source file.
EDIT: In your Derived.cpp
, try changing the order of #include
s to:
#include "base.hpp"
#include "derived.hpp"
// .. rest of your code ..
Like this:
// Derived.hpp
#pragma once
namespace foo
{
class Base;
class Derived : public Base
{
public:
Derived();
~Derived();
};
}
// Derived.cpp
#include "base.hpp"
#include "derived.hpp"
namespace foo
{
Derived::Derived()
{
}
Derived::~Derived()
{
}
}
So, you're going to want to edit Jugador.hpp
to look like this:
// Jugador.hpp
#include "Pieza.hpp" // move this above Jugada.hpp
#include "Jugada.hpp"
#include "Excepciones.hpp"
namespace Reglas
{
//compiler asked for these :S
class Celda;
class Tablero;
class Jugador : public Pieza
{
public:
Jugador() {}
virtual ~Jugador() {}
};
}
Your files should look like:
File Base.hpp
-----------------------
namespace N
{
class Base
{
public:
Base() {}
virtual ~Base() {} // Make sure you have a virtual destructor
};
}
File Derived.hpp
-----------------------
#include "Base.hpp"
namespace N
{
class Derived : public Base
{
public:
Derived() {}
~Derived() {}
};
}
精彩评论