error C2248: cannot access protected member declared in class '****'

from: http://hi.baidu.com/%BC%BC%CA%F5%B2%BB%CA%C7%B7%B9%CD%EB/blog/item/496b5b507143c5858d54305f.html

 

vs2008下,从“类”创建的对象无法访问该“类”的 protected 或 private 成员,不可以执行拷贝构造函数。

我遇到这个问题时,情况是这样的:

=> 本来有一个类QGraphicsItem,我添加新类GeologicalItem并继承自该类:

class GeologicalItem : public QGraphicsItem
{
public:
GeologicalItem();

protected:
private:
// 数据成员。
int i;
int j;
}

情况大概就是这样。编译就出现error C2248错误。
vs2008下,从“类”创建的对象无法访问该“类”的 protected 或 private 成员,不可以执行拷贝构造函数

解决方法:自己编写新类的复制构造函数和赋值构造函数:
GeologicalItem(const GeologicalItem &item);
GeologicalItem& operator=(const GeologicalItem &item);
编译,成功通过!

 

#include <iostream> #include <iomanip> #include <string> using namespace std; class Point { private: string type; double x, y; public: Point(string t = "", double xx = 0, double yy = 0) : type(t), x(xx), y(yy) {} Point(const Point& p) : type(p.type), x(p.x), y(p.y) {} virtual ~Point() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Point& p) { os << "Type: " << p.type << "\nX: " << fixed << setprecision(2) << p.x << "\nY: " << fixed << setprecision(2) << p.y; return os; } virtual void PrintName() { cout << "This is a Point object." << endl; } }; class Circle : public Point { private: double r; public: Circle(string t = "", double xx = 0, double yy = 0, double rr = 0) : Point(t, xx, yy), r(rr) {} Circle(const Circle& c) : Point(c), r(c.r) {} ~Circle() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Circle& c) { os << static_cast<const Point&>(c) << "\nRadius: " << fixed << setprecision(2) << c.r; return os; } virtual void PrintName() { cout << "This is a Circle object." << endl; } }; class Cylinder : public Circle { private: double h; public: Cylinder(string t = "", double xx = 0, double yy = 0, double rr = 0, double hh = 0) : Circle(t, xx, yy, rr), h(hh) {} Cylinder(const Cylinder& cy) : Circle(cy), h(cy.h) {} ~Cylinder() { cout << type << " object is destroyed." << endl; } friend ostream& operator<<(ostream& os, const Cylinder& cy) { os << static_cast<const Circle&>(cy) << "\nHeight: " << fixed << setprecision(2) << cy.h; return os; } virtual void PrintName() { cout << "This is a Cylinder object." << endl; } }; int main() { Point p1("Point1", 1, 2); Point p2("Point2", 3, 4); Circle c1("Circle1", 5, 6, 7); Circle c2("Circle2", 8, 9, 10); Cylinder cy1("Cylinder1", 11, 12, 13, 14); Cylinder cy2("Cylinder2", 15, 16, 17, 18); cout << p1 << endl; cout << p2 << endl; cout << c1 << endl; cout << c2 << endl; cout << cy1 << endl; cout << cy2 << endl; Point* ptr; ptr = &p1; ptr->PrintName(); ptr = &c1; ptr->PrintName(); ptr = &cy1; ptr->PrintName(); return 0; }c:\program files (x86)\microsoft visual c++6.0\myprojects\shiayn6\6.cpp(27) : error C2248: 'type' : cannot access private member declared in class 'Point' c:\program files (x86)\microsoft visual c++6.0\myprojects\shiayn6\6.cpp(8) : see declaration of 'type' 执行 cl.exe 时出错.
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值