2017-03-18 8 views
-1
#include <iostream> 
using namespace std; 

class S; 

class R { 
     int width, height; 
     public: 
     int area() // Area of rectangle 
    {return (width * height);} 
    void convert (S a); 
}; 
class S { 
    private: 
    int side; 
    public: 
    S (int a) : side(a) {} 
    friend void convert(S a); 

}; 

void R::convert (S a) {  
    width = a.side; 
    height = a.side; // Interpreting Square as an rectangle 
} 
int main() { 

    int x; 

    cin >> x; 
    R rect; 
    S sqr (x); 
    rect.convert(sqr); 
    cout << rect.area(); 
    return 0; 
} 

をプライベートメンバにアクセスすることができません私は、次のエラーを取得しています:フレンド関数を通じて

prog.cpp: In member function ‘void R::convert(S)’: prog.cpp:26:14: error: ‘int S::side’ is private within this context width = a.side; ^~~~ prog.cpp:16:8: note: declared private here int side; ^~~~ prog.cpp:27:15: error: ‘int S::side’ is private within this context height = a.side; // Interpreting Square as an rectangle ^~~~ prog.cpp:16:8: note: declared private here int side; ^~~~

私は友人ファンクション機能もプライベートも同じエラーを入れてみました。 あなたは、コンパイラでも変換がRに属していないことがある前に、手始めにS.

+0

友人が... RにSクラスのソリューションのための –

答えて

0

を助けてください最初の宣言で

void convert (S a); 

を使用し、第二に、あなたは機能convertがMEMBであることを指定する必要がありますクラスRの関数です。

class R { 
     int width, height; 
     public: 
     int area() // Area of rectangle 
    {return (width * height);} 
    void convert (class S a); 
        ^^^^^^ 
}; 
class S { 
    private: 
    int side; 
    public: 
    S (int a) : side(a) {} 
    friend void R::convert(S a); 
       ^^^ 
}; 
+0

おかげではありません:)お試しください –

0

Sが宣言されていないわからないためfriend R;

friend void convert(S a);は無意味である必要がありますclass S

関連する問題