关于模版一个问题的测试
2006-08-19 11:26
316 查看
那天写模版类时遇到了一个问题,现在记录下来备忘.
环境:Windows 2k3 sp1 + Mingw(gcc 3.4.2)
/**//* ========================================================================== */
/**//* */
/**//* main.c */
/**//* (c) 2006 ShellEx */
/**//* */
/**//* Description : 关于模版的测试 */
/**//* */
/**//* ========================================================================== */
#include <iostream>
using namespace std;
/**////////////////////////////////////////////////////////////////////////////////
//基类
template <class TElem>
class Base
...{
public:
virtual long GetData(void)...{return data;};
Base(void)...{data = 1;};
protected:
long data;
};
/**////////////////////////////////////////////////////////////////////////////////
//派生类
template <class TElem>
class Child : public Base <TElem>
...{
public:
virtual void Print(void)
...{
printf("Data = %d ", data);
};
};
/**////////////////////////////////////////////////////////////////////////////////
int main()
...{
Child <int>a;
a.Print();
printf("getData=%d", a.GetData());
return 0;
}
编译:
g++ main.cpp -o main.exe
编译提示如下错误:
main.cpp: In member function `virtual void Child<TElem>::Print()':
main.cpp:34: error: `data' undeclared (first use this function)
main.cpp:34: error: (Each undeclared identifier is reported only once for each f
unction it appears in.)
data明明是基类的成员,为何派生类Child的成员函数Print不能访问他(基类成员函数GetData是可以访问他的)?data难道不是被继承下来了么?
通过Wing老师的指点,解决方法如下:
把Print函数里的len改成this->len
原因是由于是模版,所以不确定会有多少个类似对象,编译器不知道len到底是哪种实例化对象的成员,所以用this指针就可以确保使用的是实例化后的对象,指向的是当前对象,保证唯一性.
同时感谢好友禿頭孤鳥的帮助.
环境:Windows 2k3 sp1 + Mingw(gcc 3.4.2)
/**//* ========================================================================== */
/**//* */
/**//* main.c */
/**//* (c) 2006 ShellEx */
/**//* */
/**//* Description : 关于模版的测试 */
/**//* */
/**//* ========================================================================== */
#include <iostream>
using namespace std;
/**////////////////////////////////////////////////////////////////////////////////
//基类
template <class TElem>
class Base
...{
public:
virtual long GetData(void)...{return data;};
Base(void)...{data = 1;};
protected:
long data;
};
/**////////////////////////////////////////////////////////////////////////////////
//派生类
template <class TElem>
class Child : public Base <TElem>
...{
public:
virtual void Print(void)
...{
printf("Data = %d ", data);
};
};
/**////////////////////////////////////////////////////////////////////////////////
int main()
...{
Child <int>a;
a.Print();
printf("getData=%d", a.GetData());
return 0;
}
编译:
g++ main.cpp -o main.exe
编译提示如下错误:
main.cpp: In member function `virtual void Child<TElem>::Print()':
main.cpp:34: error: `data' undeclared (first use this function)
main.cpp:34: error: (Each undeclared identifier is reported only once for each f
unction it appears in.)
data明明是基类的成员,为何派生类Child的成员函数Print不能访问他(基类成员函数GetData是可以访问他的)?data难道不是被继承下来了么?
通过Wing老师的指点,解决方法如下:
把Print函数里的len改成this->len
原因是由于是模版,所以不确定会有多少个类似对象,编译器不知道len到底是哪种实例化对象的成员,所以用this指针就可以确保使用的是实例化后的对象,指向的是当前对象,保证唯一性.
同时感谢好友禿頭孤鳥的帮助.
相关文章推荐
- 关于测试一个windows动态链接库的问题;请教各位大侠!
- 【selenium】python+selenium+unittest,关于每次执行完一个测试用例都关闭浏览器等时间较长的问题之解决方案
- 关于模版的一个问题
- 在测试aes加密的时候,出现一个关于密钥的长度问题
- 测试管理_关于面试中最后的一个问题
- 关于NSOperationQueue,一个容易让初学者误解的问题
- 网站中多个页面同时引用一个head和foot页面遇到的css样式时有时无的问题——关于路径问题
- 关于邮件列表一个问题的解释
- C#写的计数器控件 - 一个关于相对路径使用的问题
- 淘宝内核组的一个关于测试机器性能的帖子,很有意思
- 一个关于UDP通信端口的问题
- 关于获取text类型value的一个小问题
- 一个关于nvarchar字段排序,中英文混合排序的问题
- 关于DLL搜索路径顺序的一个问题
- 关于FreeMarker获取一个对象中另一个对象出现的问题以及解决方法
- 一个关于C语言指针的测试
- 一个关于windows installer的问题,很郁闷!
- 关于android中gen目录的一个小问题
- 一个可能是关于namespace的问题?
- 一个关于C语言二维数组问题