我在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&lt; T&gt;” - 留出空间。
当我使用测试代码编译时,我从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行是模板行。
答案 0 :(得分:2)
更改
typename compare = std::less<T>()
要
typename compare = std::less<T>
你想要一个类型,而不是一个对象。
此外,您必须执行#include <functional>
,因为这是定义std::less
的位置。