【发布时间】:2015-02-09 15:48:57
【问题描述】:
我对命名空间 c++ 中的外部变量有疑问。这是CBVR类的.h文件
namespace parameters
{
class CBVR
{
private:
std::string database;
public:
CBVR(void);
void initialize(const std::string &fileName);
static void printParameter(const std::string &name,
const std::string &value);
};
extern CBVR cbvr;
}
.cpp 文件如下所示:
parameters::CBVR parameters::cbvr;
using namespace xercesc;
parameters::CBVR::CBVR(void)
{
}
void parameters::CBVR::initialize(const std::string &_fileName)
{
}
void parameters::CBVR::printParameter(const std::string &_name, const std::string &_value)
{
if (_value.empty())
{
cbvr << " -> " << _name << " = < EMPTY!!! >" << std::endl;
}
else
{
cbvr << " -> " << _name << " = \"" << _value << "\"" << std::endl;
}
}
在方法printParameter 中,我们使用cbvr 没有对命名空间的任何引用。 parameters::CBVR parameters::cbvr; 处理它,但我不明白它的含义以及为什么它允许在类中像这样使用 cbvr 变量?
已编辑:
我这样做了,但它说:error: undefined reference to parameters::cbvr
//parameters::CBVR parameters::cbvr;
using namespace parameters;
using namespace xercesc;
CBVR::CBVR(void)
{
}
void CBVR::initialize(const std::string &_fileName)
{
}
void CBVR::printParameter(const std::string &_name, const std::string &_value)
{
if (_value.empty())
{
cbvr << " -> " << _name << " = < EMPTY!!! >" << std::endl;
}
else
{
cbvr << " -> " << _name << " = \"" << _value << "\"" << std::endl;
}
}
那有什么区别呢?
【问题讨论】:
标签: c++ namespaces global-variables extern