error C2275 : typedef 識別子に、クラス メンバ アクセス演算子 (->) を使用しました。
こんな警告が出た。
コンパイルしています... main.cpp c:\foo\main.cpp(25) : error C2275: 'MyLt' : typedef 識別子に、クラス メンバ アクセス演算子 (->) を使用しました。 c:\foo\main.cpp(7) : 'MyLt' の宣言を確認してください。 ビルドログは "file://c:\foo\Debug\BuildLog.htm" に保存されました。 foo - エラー 1、警告 0
コンパイルしたのはこれ。
#include <cstdlib> #include <iostream> #include <algorithm> #include <vector> struct MyLt { bool operator () (const int lhs, const int rhs) const { return lhs < rhs; } }; void main (int argc, char **argv) { std::vector <int> IntVector; for (int i = 0; 10 > i; i++) IntVector.push_back (std::rand ()); std::cout << "before sort" << std::endl; for (int i = 0; IntVector.size () > i; i++) std::cout << IntVector [i] << ", "; std::cout << std::endl; std::sort (IntVector.begin (), IntVector.end (), MyLt); // error C2275 std::cout << "after sort" << std::endl; for (int i = 0; IntVector.size () > i; i++) std::cout << IntVector [i] << ", "; std::cout << std::endl; return; }
ハァ? typedef ? メンバアクセス演算子? どちらも使ってないよ。はっきり言ってこのエラーメッセージは意味不明。
このソースの誤りは、std::sort () に渡す、ファンクタ(関数オブジェクト)の指定方法。折角 operator () を定義してても、構造体名だけ渡してる。正解はこう。
#include <cstdlib> #include <iostream> #include <algorithm> #include <vector> struct MyLt { bool operator () (const int lhs, const int rhs) const { return lhs < rhs; } }; void main (int argc, char **argv) { std::vector <int> IntVector; for (int i = 0; 10 > i; i++) IntVector.push_back (std::rand ()); std::cout << "before sort" << std::endl; for (int i = 0; IntVector.size () > i; i++) std::cout << IntVector [i] << ", "; std::cout << std::endl; std::sort (IntVector.begin (), IntVector.end (), MyLt ()); // MyLt の後ろの "()" がポイント std::cout << "after sort" << std::endl; for (int i = 0; IntVector.size () > i; i++) std::cout << IntVector [i] << ", "; std::cout << std::endl; return; }
結構悩んだよ。ふう。
おまけ。
C++ Builder 6 の場合はこんな感じのコンパイルエラー。ちょっとはわかりやすい?
[C++ エラー] main.cpp(25): E2108 typedef 'MyLt' の使い方が間違っている [C++ エラー] main.cpp(25): E2285 '_STL::sort<_RandomAccessIter,_Compare>(int *,int *,undefined)' に一致するものが見つからない
トラックバック
- このエントリーにトラックバック:
- http://frog.raindrop.jp/cgi-bin/mt/mt-tb.cgi/1265
コメント
こんにちは、またブログ覗かせていただきました。また、遊びに来ま~す。よろしくお願いします
こんにちは、またブログ覗かせていただきました。また、遊びに来ま~す。よろしくお願いします