- 论坛徽章:
- 0
|
类定义的问题
没错,DgAddressBase就是为了继承的,可是我就不清楚,为什么g++编译通过的代码在VC上出错?是类定义的问题还是编译器的问题?可能我上面没说清楚,全部代码如下:
1.DgAddressBase.h文件
#ifndef DGADRESSBASE_H
#define DGADRESSBASE_H
#include <iostream>;
using namespace std;
class DgAddressBase
{
public:
virtual ~DgAddressBase (void);
protected:
DgAddressBase (void) {}
//纯虚函数
virtual ostream& writeTo(ostream& stream) const = 0;
//"<<"操作符重载
friend ostream& operator<< (ostream& stream, DgAddressBase& add);
//测试函数
friend ostream& f(ostream& stream, DgAddressBase& add);
};
// 为何在VC中编译失败? 信息如下:
// error C2248: 'writeTo' : cannot access protected member declared in class 'DgAddressBase'
// ... : see declaration of 'writeTo'
inline ostream& operator<< (ostream& stream, DgAddressBase& add)
{
return add.writeTo(stream);
}
//编译成功
inline ostream& f(ostream& stream, DgAddressBase& add)
{
return add.writeTo(stream);
}
#endif
2.DgAddressBase.cpp文件
#include "DgAddressBase.h"
DgAddressBase::~DgAddressBase (void)
{
//析构函数
}
高手解释一下吧? |
|