2012-04-30 6 views
1

は、私はほとんどのため、学校の規則に省略してSol.hに次のコードを持っている:標準入力:: less <T>をタイプ名としてインポートするには?

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

Basiclyそれはベクトルに似コンテナですが、それは我々がテンプレートに必要とされているいくつかのことを行います。私は "typenameを比較する= std :: less < T>"を取得するように見えることはできません - スペースを無視します。

私がテストコードでコンパイルすると、私はSol.hから2つのエラーが出て、残りはすべてそれらに関係しています。

>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

ヒント:compare引数の戻り値の型は何ですか? –

答えて

2

変更

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

typename compare = std::less<T> 

するには、型ではなく、オブジェクトを希望しています。

また、#include <functional>は、std::lessが定義されているためです。

+0

私はそれを試みました。同じ結果。そして私がクラスメートにそれについて尋ねたところ、同じコンパイラを備えた大学のマシンを使用していても、同じコードをコンパイルすることができたということです。 –

+0

@TylerScottは同じエラーですか?そして私を信じてください、それは本当にコードの誤りです。他のエラーがあるかどうか。また、実際にファイルを変更していることを確認してください(例:ファイルを保存しているかどうかなど) –

+0

保存していたかどうかを確認しました。コマンドとエラースタック全体を含めるようにエラーを更新します。 –

関連する問題