2013-09-29 15 views
13

stockListType.cppの 『この』引数として:58:ここエラー:パッシング「のconst ...」「」...」修飾子を破棄し

以上
/usr/include/c++/4.2.1/bits/stl_algo.h:91: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers 
/usr/include/c++/4.2.1/bits/stl_algo.h:92: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers 
/usr/include/c++/4.2.1/bits/stl_algo.h:94: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers 
/usr/include/c++/4.2.1/bits/stl_algo.h:98: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers 
/usr/include/c++/4.2.1/bits/stl_algo.h:100: error: passing ‘const stockType’ as ‘this’ argument of ‘bool stockType::operator<(const stockType&)’ discards qualifiers 

私が得たエラーであるとだろうからインスタンス誰かのように私にそれが何を意味するのか説明してください。オーバーロード演算子の前に定数を置くことでエラーを解決しました。私のプログラムは、文字列、5倍とintを含むファイルを読み込む株式市場のアプリケーションでした。文字列記号とインデックスゲインでプログラムを分類します。この本は私にベクトルを使って各データを保存するように指示しました。以下に示すように、オーバーロード演算子は各シンボルを比較し、コンテナのソートメンバー関数を使用してソートします。私の質問はなぜ>と<のオーバーロード演算子の前に定数を入れなければならなかったかです。 > =、< =、==、!=オーバーロード演算子ではありません。

//function was declared in stockType.h and implemented in stockType.cpp 
bool operator<(const stockType& stock)//symbol is a string 
{ 
    return (symbols < stock.symbols) 
} 


//The function below was defined in stockListType.h and implemented in 
// stockListType.cpp where I instantiated the object of stockType as a vector. 
    //vector<stockType> list; was defined in stockListType.h file 

    void insert(const& stockType item) 
    { 
     list.push_back(item); 
     } 
    void stockListType::sortStockSymbols() 
    { 
    sort(list.begin(), list.end()); 
    } 
+2

'のconst&stockType item'は次のようになります。'のconst stockType&item' – billz

+0

OKおかげで私はそれを感謝し、多くの人たち – Emy

答えて

20

エラーメッセージがoperator<機能で、あなたのオブジェクトからconstを鋳造していることを、あなたことを示しています。メンバーを変更しないすべてのメンバー関数にconstを追加する必要があります。 std::sortは、要素を比較するoperator<を使用しているため

bool operator<(const stockType& stock) const 
//          ^^^^^ 
{ 
    return (symbols < stock.symbols) 
} 

コンパイラを約operator<を文句を言う理由があります。

また、insert機能に別の構文エラーがあります。

更新:

void insert(const& stockType item); 

へ:

void insert(const stockType& item); 
//       ^^ 
関連する問題