2017-06-14 59 views
-2

我有以下的头文件:重载提取操作无法访问成员变量

#include <iostream> 
#include "product.h" 

using namespace std; 

class ProductInfo : Product 
{ 
    int UPC; 

public: 
    ProductInfo() : Product(NULL, 0.0), UPC(0) 
    friend istream& operator>>(istream& is, ProductInfo& pinfo); 
}; 

Product包含一个受保护的变量float price

当我尝试我的提取操作中更改float price,我的IDE(克利翁)告诉我protected 'Product::price is inaccessible'

这里是一个cpp文件中的相关代码:

#include "productinfo.h" 

istream& operator>>(istream& is, ProductInfo& pinfo) 
{ 
    char info[256]; 

    if (is.getline(info, 256)) 
    { 
    strtok(info, ","); 
    pinfo.UPC = atoi(info); 
    pinfo.setName(strtok(NULL, ",")); 
    pinfo.price = atof(strtok(NULL, ",")); 
    } 

    return is; 
} 

我做错了什么,或者这是我的IDE的问题?

+0

它说,'产品::价格inaccessible',你要小心。 –

+1

你只是用'ProductInfo'输出运算符,而不是''继承'private'。 –

+0

请寄出'Product'的定义。 –

回答

0

class ProductInfo : Product意味着私人继承;因此ProductInfo(及其朋友)无法按名称访问Product的私人或受保护成员。

也许你想使用公有继承:

class ProductInfo : public Product