【问题标题】:Why is there a conversion in this code even though I'm not doing one? [duplicate]为什么即使我没有在此代码中进行转换? [复制]
【发布时间】:2020-02-06 14:15:29
【问题描述】:

我刚刚在大学完成了一门 C++ 课程,我们得到了期末作业。 (我只是想澄清一下:这绝不是我要求你为我做功课,我只是需要一些代码方面的帮助。)

我们的任务的第一部分是编写一个通用函数,它获取一个函子和两个索引:函子是一个条件,索引指向容器中的第一个元素和容器中最后一个元素之后的位置.该函数需要检查容器中有多少对元素满足函子的条件。

这是我的代码:

#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

bool test(vector<int> v);
template<typename InputIterator, typename Condition>
int checkCondition(InputIterator first, InputIterator last, Condition condition);

class bigger{
public:
    bigger() = default;
    ~bigger = default();
    bigger(const bigger&) = default;
    bigger& operator=(const bigger&) = default;
    bool operator()(int a, int b) const {
        return (a<b);
    }
};

template<typename InputIterator, typename Condition>
int checkCondition(InputIterator first, InputIterator last, Condition condition){
    int counter = 0;
    while (first!=last){
        vector<int>::iterator temp = first;
        ++temp;
        if (condition(*first, *temp)){
            counter++;
        }
        ++first;
    }
    return counter;
}

bool test(vector<int> v){
    vector<int>::iterator first = v.begin();
    vector<int>::iterator last = v.end();
    bigger condition();
    return checkCondition(first, last, condition);
}

这是我使用 Linux 终端(ubuntu,因为我使用的是 windows)用 g++ 编译得到的错误:

part1OfDry.cpp: In instantiation of ‘int checkCondition(InputIterator, InputIterator, Condition) [with InputIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; Condition = bigger (*)()]’:
part1OfDry.cpp:41:49:   required from here
part1OfDry.cpp:29:22: error: too many arguments to function
         if (condition(*first, *temp)){
             ~~~~~~~~~^~~~~~~~~~~~~~~
part1OfDry.cpp:29:22: error: could not convert ‘condition()’ from ‘bigger’ to ‘bool’

这是我的编译行: g++ -o test1.exe -std=c++11 -Wall -pedantic-errors -Werror -DNDEBUG thing.cpp

如果有人可以解释为什么会发生错误,我将非常感激。

【问题讨论】:

  • 它是“函数”中的“函子”
  • 查找“最麻烦的解析”
  • bigger condition();中删除()
  • 粉丝?不是 C++ 术语
  • 非常感谢@ChrisMM。

标签: c++ c++11


【解决方案1】:

试着写

bigger condition{};

或者,正如 0x5453 所建议的,简单地

bigger condition;

而不是

bigger condition();

问题:从错误消息中可以看出,bigger condition(); 被解释

 Condition = bigger (*)()

作为返回bigger的函数声明。

搜索“最令人烦恼的解析”以获得澄清。

【讨论】:

  • 或者干脆bigger condition;
  • @0x5453 - 是的,它更简单;添加;谢谢。
  • 他的默认析构函数也有额外的括号
  • @AdrianCornish - 我没看到...不是额外的括号,而是括号在错误的位置。恕我直言,您应该在回答中解释括号错误的确切位置。
  • @AdrianCornish - 关键是从你的回答中我不明白的是不需要括号;从你的第一条评论我明白了。恕我直言,您的第一条评论比您的回答更好。
【解决方案2】:

停止在不需要的地方添加括号。

#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

bool test(vector<int> v);
template<typename InputIterator, typename Condition>
int checkCondition(InputIterator first, InputIterator last, Condition condition);

class bigger{
public:
    bigger() = default;
    ~bigger() = default;
    bigger(const bigger&) = default;
    bigger& operator=(const bigger&) = default;
    bool operator()(int a, int b) const {
        return (a<b);
    }
};

template<typename InputIterator, typename Condition>
int checkCondition(InputIterator first, InputIterator last, Condition condition){
    int counter = 0;
    while (first!=last){
        vector<int>::iterator temp = first;
        ++temp;
        if (condition(*first, *temp)){
            counter++;
        }
        ++first;
    }
    return counter;
}

bool test(vector<int> v){
    vector<int>::iterator first = v.begin();
    vector<int>::iterator last = v.end();
    bigger condition;
    return checkCondition(first, last, condition);
}

【讨论】:

  • 既过于简洁又过于冗长。这就是天赋。
猜你喜欢
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-12
  • 2012-05-23
  • 2019-01-19
  • 2022-06-10
相关资源
最近更新 更多