【问题标题】:ALL_OF invalid conversion from 'char' to 'const char*' [-fpermissive]ALL_OF 从 'char' 到 'const char*' 的无效转换 [-fpermissive]
【发布时间】:2014-01-16 16:57:26
【问题描述】:

我在编译代码时遇到问题。

template<class InputIterator, class UnaryPredicate>
  bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    if (!pred(*first)) return false;
    ++first;
  }
  return true;
}

我从http://www.cplusplus.com/reference/algorithm/all_of/?kw=all_of获取代码。

我正在使用 Code::blocks 12.11,但出现以下错误:

C:\Users\PC-HP\Desktop\chiffre\romain\main.cpp||在 'bool all_of(InputIterator, InputIterator, UnaryPredicate) 的实例化中 [with InputIterator = __gnu_cxx::__normal_iterator >; UnaryPredicate = bool (*)(std::basic_string)]':|

C:\Users\PC-HP\Desktop\chiffre\romain\main.cpp|84|从这里需要|

C:\Users\PC-HP\Desktop\chiffre\romain\main.cpp|13|错误:从 'char' 到 'const char*' 的无效转换 [-fpermissive]|

c:\program files\codeblocks\mingw\bin..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.tcc|214|错误:初始化 'std::basic_string 的参数 1 <_chart _traits _alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]' [-fpermissive]|

||=== 构建完成:2 个错误,2 个警告(0 分钟,1 秒)===|

第 84 行:

while(!all_of(romain.begin(), romain.end(), IsRoman))

这是我的完整代码:http://pastebin.com/k0KYNB6H 我不使用c++11

【问题讨论】:

  • romain的类型是什么?

标签: c++


【解决方案1】:

pred 接受一个字符串,但在表达式 pred(*first) 中,您只输入了一个 char

【讨论】:

  • 感谢您的精确!
【解决方案2】:

根据您的错误消息,您的谓词采用std::basic_string(可能真的是std::string),但您正在迭代chars 的序列。这些不会转换为std::string。你想传递一个谓词像

bool IsRoman(char c) {
    return ...;
}

【讨论】:

    猜你喜欢
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多