2017-06-14 62 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