2011-12-03 9 views
5

ここではコードです。 G ++でコンパイルされたことは言う:グラム++文字列remove_ifエラー

hw4pr3.cpp: In function `int main()': 
hw4pr3.cpp:20: error: no matching function for call to `remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)' 
hw4pr3.cpp:21: error: no matching function for call to `remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)' 
hw4pr3.cpp:22: error: no matching function for call to `remove_if(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unknown type>)' 

答えて

13

は、isspaceispunctisdigitの先頭に::を追加します。

word.erase(remove_if(word.begin(), word.end(), ::isspace), word.end()); 
word.erase(remove_if(word.begin(), word.end(), ::ispunct), word.end()); 
word.erase(remove_if(word.begin(), word.end(), ::isdigit), word.end()); 
+0

グローバルネームスペース内のCライブラリ関数を持つことは、廃止され、従来のもの( ''を含める必要があります)、最悪の場合、依存してはいけない奇妙なコンパイラ特有です。 –

+0

@KerrekSB:私はそれが非難されたことを知らなかった/ハック、チップのおかげで。 – AusCBloke

3

#include <cctype>を追加(そしてあなたがabusing namespace std;されていない場合std::isspaceなどを言います)。

必要なすべてのヘッダーを常に含め、隠れたネストされたインクルードには依存しないでください。

<locale>にもう1つのオーバーロードを曖昧させなければならない場合もあります。明示的なキャストを追加することでこれを行います。彼らは、コンパイラが使用する上で決めることができないというオーバーロードを持っているので

word.erase(std::remove_if(word.begin(), word.end(), 
          static_cast<int(&)(int)>(std::isspace)), 
      word.end()); 
2

私は、次のいずれかを行う場合は私にとって、それはグラムを使用してコンパイル++:

  • using namespace std;を削除し、を変更します0〜std::string;または
  • 変更isspace::isspace(など)。

これらのいずれかがisspace(など)の代わりに、おそらくstd::isspace(等)を意味するものとして解釈されるの主な名前空間から取得されます。

0

問題は、std :: isspace(int)はintをパラメータとして取りますが、文字列はcharで構成されていることです。したがって、あなた自身の関数を次のように書く必要があります:

bool isspace(char c){return c == ''; }

他の2つの機能についても同様です。