質問

皆様にお伝えしたくて書き込みの小さなマトリクス図書館C++のためのマトリクス事業です。しかし私のコンパイラの警告を、前もっていません。このコードを離れた棚を6ヶ月間でしたマイコンピュータからdebian etchにlenny(g++(Debian4.3.2-1.1)4.3.2 ただし私は同じような問題にはUbuntuシステムと同じg++.

ここで、該当部分のマトリクスクラス:

namespace Math
{
    class Matrix
    {
    public:

        [...]

        friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix);
    }
}

の"実行":

using namespace Math;

std::ostream& Matrix::operator <<(std::ostream& stream, const Matrix& matrix) {

    [...]

}

これらのエラーのコンパイラ:

で行います。cpp:459:エラー:'std::ostream& Math::行列::オペレーター<<(std::ostream&, const数::マトリクス&)'が必要 ちょうど一引数

いてはちょっとした混乱するこのエラーの、その後は再び私のC++頂いているビットの赤った後、多くのJavaその6ヶ月です。:-)

役に立ちましたか?

解決

あなたはfriendとしてあなたの関数を宣言しました。これは、クラスのメンバではありません。あなたは、実装からMatrix::削除する必要があります。 Matrix(クラスのメンバではない)指定された機能は、プライベートメンバ変数にアクセスできることを意味します。あなたは機能実装方法が間違っている<=>クラスのインスタンスメソッドのようなものです。

他のヒント

ただ、あなたについての一つの他の可能性を伝える:私はそのための友達の定義を使用して好きます:

namespace Math
{
    class Matrix
    {
    public:

        [...]

        friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix) {
            [...]
        }
    };
}

関数が自動的に周囲の名前空間Mathに標的にされます(その定義は、そのクラスの範囲内で表示されていても)がありますが、引数依存の照合を行いますMatrixオブジェクトと<<演算子を呼び出さない限り表示されませんその演算子の定義を見つけます。それはマトリックス以外の引数の型のために目に見えないですので、それは時々、あいまいな呼び出しを支援することができます。その定義を書くとき、あなたはまた、マトリックスで定義された名前を直接参照することができ、いくつかの可能性が、長いプレフィックスで名前を修飾し、Math::Matrix<TypeA, N>などのテンプレートパラメータを提供せずに、自分自身を行列します。

Mehrdadの答えに追加するには、

namespace Math
{
    class Matrix
    {
       public:

       [...]


    }   
    std::ostream& operator<< (std::ostream& stream, const Math::Matrix& matrix);
}

あなたの実装では、

std::ostream& operator<<(std::ostream& stream, 
                     const Math::Matrix& matrix) {
    matrix.print(stream); //assuming you define print for matrix 
    return stream;
 }

と仮定してい過負荷 operator << すべての派生クラス std::ostream 取扱いの Matrix クラスではない過負荷 << のための Matrix クラス)で、より感を宣言する過負荷機能外算引き算など余計な計算は名前空間にヘッダを表します。

使友達機能の場合のみ機能が実現できない、公開インターフェース.

で行います。h

namespace Math { 
    class Matrix { 
        //...
    };  
}
std::ostream& operator<<(std::ostream&, const Math::Matrix&);

このオペレーターの過負荷が宣言された以外の名前

Matrix.cpp

using namespace Math;
using namespace std;

ostream& operator<< (ostream& os, const Matrix& obj) {
    os << obj.getXYZ() << obj.getABC() << '\n';
    return os;
}

一方でいる場合、過負荷機能 する必要がある友人のつニーズにアクセス、個人および保護されます。

Math.h

namespace Math {
    class Matrix {
        public:
            friend std::ostream& operator<<(std::ostream&, const Matrix&);
    };
}

必要なもの)を同封して機能定義の名前空間ブロックだけでなく using namespace Math;.

Matrix.cpp

using namespace Math;
using namespace std;

namespace Math {
    ostream& operator<<(ostream& os, const Matrix& obj) {
        os << obj.XYZ << obj.ABC << '\n';
        return os;
    }                 
}

C ++ 14では、あなたは、T ::プリント(STD :: ostreamに&)のconstを持つ任意のオブジェクトを印刷するには、以下のテンプレートを使用することができます。メンバーます。

template<class T>
auto operator<<(std::ostream& os, const T& t) -> decltype(t.print(os), os) 
{ 
    t.print(os); 
    return os; 
} 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top