关于is not a member of '`global namespace''的异常
关于is not a member of '`global namespace''的错误
小弟初学VC++,照别人的例子改写了一个程序,包含这样几个文件:
//Singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H
namespace only
{
template <class T>
class Singleton
{
private:
static T * instance;
public:
static T & Instance()
{
if(!instance)
{
instance = new T();
}
return *instance;
}
};
template <class T>
T * Singleton <T> ::instance = 0;
}
#endif
//TestObject.h
#ifndef CTESTOBJECT_H
#define CTESTOBJECT_H
#include "Singleton.h "
class CTestObject : public only::Singleton <CTestObject>
{
public:
void Test();
};
#endif
//TestObject.cpp
#include "stdafx.h "
#include "TestObject.h "
void CTestObject::Test()
{
//测试代码
}
以上代码都能编译通过,但是在其他文件里加入如下代码编译就会报错:
//AAADlg.cpp
#include "stdafx.h "
#include "TestObject.h "
void CAAADlg::OnOK()
{
CTestObject::Instance()::Test();
CDialog::OnOK();
}
错误信息如下:
...\AAADlg.cpp(98) : error C2039: 'Test ' : is not a member of '`global namespace ' '
...\AAADlg.cpp(98) : error C2146: syntax error : missing '; ' before identifier 'Test '
...\AAADlg.cpp(98) : error C2065: 'Test ' : undeclared identifier
不知道怎么解决,请高手赐教,不胜感激!
------解决方案--------------------
是 CTestObject::Instance().Test();
不是CTestObject::Instance()-> Test();
------解决方案--------------------
返回的是引用,不是指针
小弟初学VC++,照别人的例子改写了一个程序,包含这样几个文件:
//Singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H
namespace only
{
template <class T>
class Singleton
{
private:
static T * instance;
public:
static T & Instance()
{
if(!instance)
{
instance = new T();
}
return *instance;
}
};
template <class T>
T * Singleton <T> ::instance = 0;
}
#endif
//TestObject.h
#ifndef CTESTOBJECT_H
#define CTESTOBJECT_H
#include "Singleton.h "
class CTestObject : public only::Singleton <CTestObject>
{
public:
void Test();
};
#endif
//TestObject.cpp
#include "stdafx.h "
#include "TestObject.h "
void CTestObject::Test()
{
//测试代码
}
以上代码都能编译通过,但是在其他文件里加入如下代码编译就会报错:
//AAADlg.cpp
#include "stdafx.h "
#include "TestObject.h "
void CAAADlg::OnOK()
{
CTestObject::Instance()::Test();
CDialog::OnOK();
}
错误信息如下:
...\AAADlg.cpp(98) : error C2039: 'Test ' : is not a member of '`global namespace ' '
...\AAADlg.cpp(98) : error C2146: syntax error : missing '; ' before identifier 'Test '
...\AAADlg.cpp(98) : error C2065: 'Test ' : undeclared identifier
不知道怎么解决,请高手赐教,不胜感激!
------解决方案--------------------
是 CTestObject::Instance().Test();
不是CTestObject::Instance()-> Test();
------解决方案--------------------
返回的是引用,不是指针