【问题标题】:How to import std::less<T> as a typename?如何将 std::less<T> 作为类型名导入?
【发布时间】:2012-04-30 02:50:15
【问题描述】:

我在 Sol.h 中有以下代码,由于学校规定,大部分代码被省略:

template <typename T,int promote = 1, typename compare = std::less<T>() >
class Sol{
private:
  struct node{
    T data;
    struct node *next;
    struct node *previous;
  };
node *head, *tail;
public:
  typedef unsigned int size_type;
  typedef T key_type;
  typedef T value_type;
  size_type count;
  Sol() : head(0), tail(0) {
    count=0;
  }

基本上它是一个类似于矢量的容器,但它做了一些我们需要模板化的事情。我似乎无法得到“typename compare = std::less” - 忽略空格。

当我使用测试代码编译时,我从 Sol.h 中得到两个错误,其余的都与它们有关。

>temple> g++ -Wall -Wextra -ansi -pedantic Sol.h main.cc
Sol.h:6:61: error: expected type-specifier
Sol.h:6:61: error: expected '>'
main.cc: In function 'int main()':
main.cc:19:19: error: template argument 3 is invalid
main.cc:19:24: error: invalid type in declaration before '(' token
main.cc:19:48: error: expression list treated as compound expression in initializer [-fpermissive]
main.cc:19:48: warning: left operand of comma operator has no effect [-Wunused-value]
main.cc:19:48: error: invalid conversion from 'char*' to 'int' [-fpermissive]
main.cc:20:19: error: template argument 3 is invalid
main.cc:20:31: error: expected initializer before 'it'
main.cc:22:13: error: request for member 'erase' in 'foo', which is of non-class type 'int'
main.cc:24:9: error: 'it' was not declared in this scope
main.cc:24:18: error: request for member 'find' in 'foo', which is of non-class type 'int'
main.cc:25:9: error: request for member 'end' in 'foo', which is of non-class type 'int'
main.cc:28:13: error: request for member 'find' in 'foo', which is of non-class type 'int'
main.cc:30:13: error: request for member 'find' in 'foo', which is of non-class type 'int'
main.cc: In function 'std::string cat(const T&) [with T = int, std::string = std::basic_string<char>]':
main.cc:23:24:   instantiated from here
main.cc:12:35: error: 'int' is not a class, struct, or union type
main.cc:12:50: error: request for member 'end' in 'con', which is of non-class type 'const int'
main.cc:12:35: error: 'int' is not a class, struct, or union type
main.cc:12:35: error: 'int' is not a class, struct, or union type
main.cc:12:35: error: 'int' is not a class, struct, or union type

明确第 6 行是模板行。

【问题讨论】:

  • 提示:比较参数的返回类型是什么?

标签: c++ templates functor


【解决方案1】:

改变

typename compare = std::less<T>()

typename compare = std::less<T>

你想要的是一个类型,而不是一个对象。

您还必须执行#include &lt;functional&gt;,因为这是定义std::less 的地方。

【讨论】:

  • 我试过了。结果相同。奇怪的是,我问过同学们,即使我们使用的是大学机器和相同的编译器,他们也设法获得了相同的代码进行编译。
  • @TylerScott 同样的错误还是其他?不过请相信我,这确实是代码中的错误。是否有其他错误。还要确保你真的在改变它(例如,确保你正在保存文件等)
  • 我检查以确保它正在保存并且确实如此。我将更新错误以包含整个命令和错误堆栈。
  • 此外,只要您不是 CSU 的学生,我也可以通过电子邮件向您展示所有代码。
  • @TylerScott 实际上不,我错了,你必须 #include &lt;functional&gt; 这是std::less 所在的位置。因此,两者都删除那些() 并在定义上方执行#include &lt;functional&gt;
猜你喜欢
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 2021-04-22
  • 2017-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多