gpt4 book ai didi

c++ - not1 未在此范围内声明

转载 作者:太空宇宙 更新时间:2023-11-04 15:08:20 24 4
gpt4 key购买 nike

我正在关注以下信息:How to find the first character in a C++ string

当我尝试将它实现到我的代码中时,我收到错误 not1 was not declared in this scope。

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++ 比较陌生,在类里面从事这些项目。

最佳答案

你有没有包括this header :

#include <functional>

此外,使用 std::not1,而不仅仅是 not1,因为它是在 std 命名空间中定义的。

我希望你没有在你的代码中编写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

我猜 std 命名空间中存在另一个名称为 isspace 的函数,这在解析名称时导致了问题。

所以这里有两个解决方案。一个一个地尝试:

  • 只使用::isspace。不使用 std。只是 ::isspace。看看它是否有效!
  • 或者,显式转换以帮助编译器选择所需的重载,如

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

由于转换看起来很笨拙,您也可以使用 typedef,如:

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

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

希望对您有所帮助。


之前看到过类似的问题,看这个:

关于c++ - not1 未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8330729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com