如何导入 std::less<T> 作为字体名?

How to import std::less<T> as a typename?

本文关键字:gt 字体 何导入 lt 导入 std less      更新时间:2023-10-16

我在 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;
  }

基本上它是一个类似于矢量的容器,但它做了一些我们需要模板的事情。我似乎无法获得"类型名称比较 = 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 行是模板行。

更改

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

typename compare = std::less<T>

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

你也必须做#include <functional>因为这是定义std::less的地方。