2011-11-30 23 views
0

宣佈我從以下一些這方面的信息:How to find the first character in a C++ stringNOT1並沒有在此範圍

當我試圖落實到我的代碼,我得到了錯誤NOT1沒有在這個範圍內聲明。

void ASTree::indent(int ind, int inc) { 
    std::string theText; 

    for (std::vector<ASTree*>::const_iterator i = child.begin(); i != child.end(); ++i) { 
     switch ((*i)->nodeType) { 
      case category: 
       (*i)->indent(ind + inc, inc); 
       break; 
      case token: 
       { 
       //out << std::setw(indent) << " "; 
       theText = (*i)->text; // << std::endl; 
       std::string::iterator firstChar = std::find_if(theText.begin(), theText.end(), std::not1(std::isspace)); 
       theText.erase(theText.begin(), firstChar); 
       (*i)->text = theText; 
       } 
       break; 
      case whitespace: 
       //out << (*i)->text; 
       break; 
     } 
    } 
} 

我對C++有點新不熟悉,並且在課上學習這些項目。

+1

那麼,not1在此代碼中沒有任何聲明。你確定你沒有錯過什麼嗎? – juanchopanza

回答

7

您有加入this頭:因爲它是在std命名空間中定義

#include <functional> 

此外,使用std::not1,不只是not1

我希望你沒有在你的代碼中寫入using namespace std,反正這是一個壞主意。


好吧由你讀這篇評論後:

get yet another error. :) no matching function for call to ânot1(<unresolved overloaded function type>)â I also updated the code above to show you my current 

我想有一個與名字isspace存在於std命名空間,這是在解析名稱造成問題的另一種功能。

所以這裏有兩個解決方案。一個一個嘗試:

  • 只使用::isspace。沒有使用std。只需::isspace。看看它是否有效!
  • 或者明確地投以幫助選擇所需的過載的編譯器,如

    std::not1(((int)(*)(int))(std::isspace)); 
    

由於鑄造看起來很笨拙,你也可以使用類型定義,如:

//define this inside the function, or outside the function! 
typedef int (*fun)(int); 

//then do this: 
std::not1((fun)(std::isspace)) 

我希望這可以幫助你。


類似的問題以前已經見過,看到這一點:

+0

是的,包括在內。 – teynon

+0

@Tom:在這種情況下,只需在''not1'前加'std ::'就可以! – Nawaz

+0

我曾嘗試使用std :: not1幾次,但我得到了一堆在底部說一個巨大的錯誤:錯誤:沒有匹配的調用?(std :: unary_negate )(char&) – teynon

1

要使用std::not1需要#include <functional>,還需要與命名空間正確前綴它(如果您沒有using指令):

std::string::iterator firstChar = std::find_if(
    theText.begin(), 
    theText.end(), 
    std::not1(isspace));