2017-10-07 7 views
-3

私はC++を学び、Vector2クラスを作成しようとしています。私はこのToString()関数を私のVector2クラスに持っていて、Vector2をスクリーンに印刷することができます。C++で静的constクラスを印刷する

私はこの静的const Vector2変数も呼び出されていますが、このToString()関数を使用しても出力したいと思いますが、エラーが発生しています。 これは、.hと.cppのVector2 :: up実装です。

Vector2 :: veをVector2 vecに保存してvec.ToString()のように印刷すると、それが動作します。 しかし、私はベクトル:: up.ToString()を印刷しようとすると動作しません。

これは、私のVector2クラスのVector2 :: upとToString()関数です。

"Vector2.h" 

static const Vector2 up; 

std::string ToString (int = 2); 


"Vector2.cpp" 

const Vector2 Vector2::up = Vector2 (0.f, 1.f); 

std::string Vector2::ToString (int places) 
{ 
    // Format: (X, Y) 
    if (places < 0) 
     return "Error - ToString - places can't be < 0"; 
    if (places > 6) 
     places = 6; 

    std::stringstream strX; 
    strX << std::fixed << std::setprecision (places) << this->x; 
    std::stringstream strY; 
    strY << std::fixed << std::setprecision (places) << this->y; 

    std::string vecString = std::string ("(") + 
          strX.str() + 
          std::string (", ") + 
          strY.str() + 
          std::string (")"); 

    return vecString; 
} 

私は私の主な機能

"Main.cpp" 

int main() 
{ 
    Vector2 vec = Vector2::up; 
    cout << vec.ToString() << endl; 
    cout << Vector2::up.ToString() << endl; 

    cout << endl; 
    system ("pause"); 
    return 0; 
} 

でやりたいと私は両方の印刷(0.00、1.00)が、ベクトル2 :: up.ToString(にそれらをしたいと思います)与えている何エラーVector::upとして

1>c:\users\jhehey\desktop\c++\c++\main.cpp(12): error C2662: 'std::string JaspeUtilities::Vector2::ToString(int)': cannot convert 'this' pointer from 'const JaspeUtilities::Vector2' to 'JaspeUtilities::Vector2 &' 
+4

投稿する[MCVE]。コードの画像を投稿しないでください。 –

+0

コードとエラーメッセージをコピー&ペーストする! –

+0

私はそれを編集し、ToString()のコードを投稿しました –

答えて

1

はあなただけconst宣言されたメンバ関数にアクセスすることができ、const宣言されています。 Vector2::ToStringは実際にベクトルを変更しませんが、constとは宣言していません。これを行うには、次のように宣言します。std::string ToString (int places) const;

+0

ありがとうございました。 –

関連する問題