gpt4 book ai didi

c++ - 如何导入 std::less 作为类型名?

转载 作者:行者123 更新时间:2023-11-30 04:28:10 25 4
gpt4 key购买 nike

由于学校规定,我在 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;
}

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

当我用测试代码编译时,我从 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的地方已定义。

关于c++ - 如何导入 std::less<T> 作为类型名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10377964/

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