头文件中的类模板给了我很多错误

时间:2017-01-19 16:58:44

标签: c++ templates header-files

以下代码:

main.cpp中:

#include <iostream>
#include <list>
#include <map>
#include <string>

#include "core_table.h"

using std::cout;
using std::endl;

int main() {
    core_table<int, int> t;

    cout << sizeof(t) << endl;
    return 0;
}

core_table.h:

#ifndef CORE_TABLE_H
#define CORE_TABLE_H

template <typename key, typename value>
class core_table {
    std::list<key, value> l;
    std::map<key, value> m;
public:
};

#endif // !CORE_TABLE_H

在Visual Studio 2017中给出了这个错误列表。我不知道如何解决它,因为这是我第一次尝试在多个文件中构建代码。

Severity    Code    Description Project File    Line    Suppression State
Error   C2516   '_Alloc': is not a legal base class ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    984 
Error   C2825   '_Alloc': must be a class or namespace when followed by '::'    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    659 
Error   C2510   '_Alloc': left of '::' must be a class/struct/union ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    659 
Error   C3646   'value_type': unknown override specifier    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    659 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    659 
Error   C2825   '_Ty': must be a class or namespace when followed by '::'   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    323 
Error   C2510   '_Ty': left of '::' must be a class/struct/union    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    323 
Error   C2143   syntax error: missing ';' before '*'    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    323 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    323 
Error   C2238   unexpected token(s) preceding ';'   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    323 
Error   C2039   'type': is not a member of 'std::_Get_pointer_type<_Alloc,void>'    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    661 
Error   C3646   'pointer': unknown override specifier   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    662 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    662 
Error   C2039   'type': is not a member of 'std::_Get_pointer_type<_Alloc,void>'    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    337 
Error   C3646   '_Ptrty': unknown override specifier    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    337 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    337 
Error   C2825   '_Ty': must be a class or namespace when followed by '::'   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    338 
Error   C2510   '_Ty': left of '::' must be a class/struct/union    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    338 
Error   C3646   '_Valty': unknown override specifier    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    338 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    338 
Error   C2065   '_Ptrty': undeclared identifier ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    339 
Error   C2923   'std::pointer_traits': '_Ptrty' is not a valid template type argument for parameter '_Ty'   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    339 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    339 
Error   C2146   syntax error: missing '>' before identifier '_Valty'    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    339 
Error   C2903   'rebind': symbol is neither a class template nor a function template    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    339 
Error   C2371   'std::_Get_const_pointer_type<_Alloc,void>::type': redefinition; different basic types  ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    339 
Error   C2039   'type': is not a member of 'std::_Get_pointer_type<_Alloc,void>'    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    353 
Error   C3646   '_Ptrty': unknown override specifier    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    353 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    353 
Error   C2065   '_Ptrty': undeclared identifier ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    354 
Error   C2923   'std::pointer_traits': '_Ptrty' is not a valid template type argument for parameter '_Ty'   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    354 
Error   C2903   'rebind': symbol is neither a class template nor a function template    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    354 
Error   C2371   'std::_Get_void_pointer_type<_Alloc,void>::type': redefinition; different basic types   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    354 
Error   C2039   'type': is not a member of 'std::_Get_pointer_type<_Alloc,void>'    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    368 
Error   C3646   '_Ptrty': unknown override specifier    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    368 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    368 
Error   C2065   '_Ptrty': undeclared identifier ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    369 
Error   C2923   'std::pointer_traits': '_Ptrty' is not a valid template type argument for parameter '_Ty'   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    369 
Error   C2903   'rebind': symbol is neither a class template nor a function template    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    369 
Error   C2371   'std::_Get_const_void_pointer_type<_Alloc,void>::type': redefinition; different basic types ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    369 
Error   C2039   'type': is not a member of 'std::_Get_pointer_type<_Alloc,void>'    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    383 
Error   C3646   '_Ptrty': unknown override specifier    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    383 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    383 
Error   C2065   '_Ptrty': undeclared identifier ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    384 
Error   C2923   'std::pointer_traits': '_Ptrty' is not a valid template type argument for parameter '_Ty'   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    384 
Error   C2955   'std::pointer_traits': use of class template requires template argument list    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    384 
Error   C2371   'std::_Get_difference_type<_Ty,void>::type': redefinition; different basic types    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    384 
Error   C3646   'allocate': unknown override specifier  ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    688 
Error   C2065   '_Al': undeclared identifier    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    688 
Error   C2275   '_Alloc': illegal use of this type as an expression ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    688 
Error   C2226   syntax error: unexpected type 'std::allocator_traits<_Alloc>::size_type'    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    688 
Error   C2334   unexpected token(s) preceding '{'; skipping apparent function body  ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    689 
Error   C3646   'allocate': unknown override specifier  ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    693 
Error   C2065   '_Al': undeclared identifier    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    693 
Error   C2275   '_Alloc': illegal use of this type as an expression ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    693 
Error   C2226   syntax error: unexpected type 'std::allocator_traits<_Alloc>::size_type'    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    693 
Error   C2334   unexpected token(s) preceding '{'; skipping apparent function body  ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    695 
Error   C2061   syntax error: identifier 'pointer'  ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    700 
Error   C2039   'pointer': is not a member of 'std::allocator_traits<_Alloc>'   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    990 
Error   C3646   'pointer': unknown override specifier   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    990 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    990 
Error   C2027   use of undefined type 'std::_Replace_first_parameter<_Other,_Ty>'   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    469 
Error   C3646   'type': unknown override specifier  ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    469 
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    469 
Error   C2027   use of undefined type 'std::_Replace_first_parameter<_Other,_Ty>'   ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0    469 
Error   C2535   'std::list<key,value>::list(unsigned int,const int &)': member function already defined or declared ali c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.10.24728\include\list    923 

我做错了什么?

1 个答案:

答案 0 :(得分:2)

std::list的第二个模板参数应该是一个分配器,但你还给了别的东西 - 你的valueint

因此,编译器抱怨,因为int没有allocator应该公开的任何成员。

另外,你应该让你的包含文件自给自足,即它们应该包括他们自己需要的所有标题。