2012-12-04 86 views
0
//**** Build of configuration Debug for project Calculator **** 

    **** Internal Builder is used for build    **** 
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\Calculator.o ..\src\Calculator.cpp 
..\src\/Calculator.h: In function 'std::ostream& operator<<(std::ostream&, CComplex)': 
    ..\src\/Calculator.h:38:9: error: 'float CComplex::m_imaginary' is private 
    ..\src\Calculator.cpp:79:8: error: within this context 
    ..\src\/Calculator.h:37:9: error: 'float CComplex::m_real' is private 
    ..\src\Calculator.cpp:81:12: error: within this context 
     ..\src\/Calculator.h:38:9: error: 'float CComplex::m_imaginary' is private 
     ..\src\Calculator.cpp:81:31: error: within this context 
    ..\src\/Calculator.h:37:9: error: 'float CComplex::m_real' is private 
     ..\src\Calculator.cpp:85:12: error: within this context 
     ..\src\/Calculator.h:38:9: error: 'float CComplex::m_imaginary' is private 
     ..\src\Calculator.cpp:85:31: error: within this context 
Build error occurred, build is stopped 
Time consumed: 687 ms. 

任何人都可以幫助我 - 我試圖訪問不接受訪問的私人功能。無法訪問私人功能

+0

那麼,爲什麼你想要訪問一個私人功能?以上看起來完全是預期的 –

回答

2

那麼,如果不是這樣,那麼這將是一個很好的問題。



當前類成員的列表,私營關鍵字指定 那些成員只能從成員函數和 朋友之類的訪問。這適用於聲明至 下一個訪問說明符或類末尾的所有成員。

成員函數不可訪問,因爲您試圖從類之外訪問它。如上所述,private關鍵字用於防止這一點。

如果您確實需要從課程外部訪問,那麼您需要使用public關鍵字將其設置爲公共方法。

在此處查找有關private關鍵字的some examples and explanation


看着你的錯誤,我認爲問題出在你的運營商< <超載。 操作員只能作爲朋友功能重載,這本身應該可以解決您的問題。

friend std::ostream& operator<<(std::ostream&, CComplex);

+0

你得到了起來,因爲你讓我發笑,這是真的:)但它還不是一個完整的答案。你能否稍微擴展你的答案,或許大致向@harsha解釋私人/保護/公共職能之間的區別?然後,你可以保持它;) – giorgio

+1

@giorgio我甚至試圖upvote這個問題,但我不得不停止自己:) – asheeshr

1

你可能想使operator<<朋友CComplex類的。這樣的事情:

class CComplex { 
    ... 
    // It doesn't matter whether this declaration is on a private, 
    // public or protected section of the class. The friend function 
    // will always have access to the private data of the class. 
    friend std::ostream& operator<<(std::ostream&, CComplex); 
};