读书人

g++编译不过有关问题

发布时间: 2013-03-14 10:33:15 作者: rapoo

g++编译不过问题

#include <stdio.h>

class CEmptyPoolData
{

};


typedef struct tagMyData
{
tagMyData()
{
myint= 10;
}
int myint;
}TMyData;

template<typename CDATA = CEmptyPoolData>
class CTest:public CDATA
{
public:
void Print()
{
printf("%d", myint);
}
};




#include "tst.h"

CTest<TMyData> g_test;
int main()
{
g_test.Print();
return 0;
}



In file included from tst.cpp:2:
tst.h: In member function ‘void CTest<CDATA>::Print()’:
tst.h:24: 错误:‘myint’在此作用域中尚未声明

[解决办法]
CTest没有该成员变量
代码是什么意思?看起来很混乱
[解决办法]
头一次看到这种写法.基类的名字也能写成模板.?
[解决办法]

#include "tst.h"

CTest<tagMyData> g_test;
int main()
{
g_test.Print();
return 0;
}

[解决办法]
CTest有一个依赖型基类,为了避免名称冲突,C++标准规定,此时名字搜索不会检查基类,如果需要引用基类中的名字,需使用基类的qualified-id,例如:

printf("%d", CDATA::myint);
[解决办法]

template<class CDATA = CEmptyPoolData>
class CTest:public CDATA
{
public:
void Print()
{
printf("%d", tagMyData::myint);
}
};

再试

读书人网 >C++

热点推荐