无法从另一个名称空间创建类的实例? [英] Cannot create an instance of a class from another namespace?
问题描述
好的,这真的很麻烦.我从未遇到过这样的事情.
Okay, so this is really werid. I've never encountered anything like this.
我的程序的一部分(无法编译)包含以下三个名称空间:
Part of my program (Fails to compile) contains three namespaces as following:
// namespaceA.h
namespace A {
enum Kind { jimmy, david };
}
// end of namespaceA.h
// namespaceB.h
#include "namespaceA.h"
namespace B {
class Tree {
public:
Tree *prev;
Tree *next;
Tree *down;
A::Kind kind;
Tree();
~Tree();
};
}
// end of namespaceB.h
// Implementation details of the class are placed in namespaceB.cc
// Constructor / Desctructor defined in the namespaceB.cc file!
// Something like this,
#include "namespaceB.h"
namespace B {
inline Tree::Tree() { ... }
inline Tree::~Tree() { ... }
}
// namespaceC.cc
#include "namespace.B"
namespace C {
void run() {
B::Tree *tree; // FINE
B::Tree tree; // Fail to compile!?
}
}
// end of namespaceC.cc
现在,g ++运行得很好,但是链接器ld抱怨:
Now, g++ went along just fine but the linker ld complains:
"namespaceC.cc: undefined reference to `B::Tree::Tree()'
"namespaceC.cc: undefined reference to `B::Tree::~Tree()'
我以前从未遇到过这样的事情……这看起来真的很奇怪,我什至不知道任何单词/术语来描述这个问题.
I have never ever encountered anything like this before... This just seems really weird, I don't even know any words/terms to describe this problem.
非常感谢您的帮助.
谢谢
推荐答案
您对B::Tree::Tree()
和B::Tree::~Tree()
的定义是内联声明的.这意味着它们仅在该源文件中可用,而在其他任何文件中均不可用.
Your definitions of B::Tree::Tree()
and B::Tree::~Tree()
are declared inline. This means they are only available in that source file, not any others.
从定义中删除inline
,或者将内联定义移动到需要它们的所有源文件所包含的头文件中,都应该解决链接错误.
Either removing inline
from the definitions, or moving the inline definitions into a header file included by all source files that need them, should fix the link errors.
这篇关于无法从另一个名称空间创建类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!