具有模板的类的构造函数存在问题 我试图用C++和模板编写容器类。然而,我有一个编译错误,我不明白

具有模板的类的构造函数存在问题 我试图用C++和模板编写容器类。然而,我有一个编译错误,我不明白,c++,class,templates,constructor,containers,C++,Class,Templates,Constructor,Containers,变量elems是一个私有向量,声明为: private: vector<DataType> elems; } 构造器如下所示: template <class DataType> bag<DataType>::bag(int init_capacity) : elems(init_capacity) { } 模板 行李::行李(内部初始容量):元素(初始容量){ } 此代码返回以下错误: ../src/vector.h: In instantiati

变量
elems
是一个私有向量,声明为:

private:
  vector<DataType> elems;
}

构造器如下所示:

template <class DataType>
bag<DataType>::bag(int init_capacity) : elems(init_capacity) {
}
模板
行李::行李(内部初始容量):元素(初始容量){
}
此代码返回以下错误:

../src/vector.h: In instantiation of ‘vector<DataType>::vector(int) [with DataType = int]’:
../src/bag.h:33:60:   required from ‘bag<DataType>::bag(int) [with DataType = int]’
../src/bag_test.cpp:6:17:   required from here
。/src/vector.h:在“vector::vector(int)[with DataType=int]的实例化中:
../src/bag.h:33:60:来自“bag::bag(int)[with DataType=int]的必填项”
../src/bag_test.cpp:6:17:此处需要

我真的不知道会发生什么。我将非常感谢任何能为我指出正确方向的人…

很抱歉这个愚蠢的问题。编译器确实抱怨过这一点,但代码实际上是编译的。感谢@WhozCraig和@n.m坚持认为这不是一个错误,我注意到它实际上正在构建。谢谢为了将来的参考,我确实发布了整个消息:

**** Build of configuration Debug for project ADS ****
make all 
Building file: ../src/bag_test.cpp

Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/bag_test.d" -MT"src/bag_test.d" -o "src/bag_test.o" "../src/bag_test.cpp"
In file included from ../src/bag_test.cpp:2:0:
../src/bag.h:23:66: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const bag<DataType>&)’ declares a non-template function [-Wnon-template-friend]
../src/bag.h:23:66: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
In file included from ../src/bag.h:2:0,
                 from ../src/bag_test.cpp:2:
../src/vector.h: In instantiation of ‘vector<DataType>::vector(int) [with DataType = int]’:
../src/bag.h:34:53:   required from ‘bag<DataType>::bag(int) [with DataType = int]’
../src/bag_test.cpp:6:17:   required from here
../src/vector.h:100:6: warning: ‘vector<int>::vect_capacity’ will be initialized after [-Wreorder]
../src/vector.h:99:6: warning:   ‘int vector<int>::vect_size’ [-Wreorder]
../src/vector.h:108:1: warning:   when initialized here [-Wreorder]
Finished building: ../src/bag_test.cpp

Building target: ADS
Invoking: GCC C++ Linker
g++  -o "ADS"  ./src/bag_test.o   
Finished building target: ADS

**** Build Finished ****
****构建项目ADS的配置调试****
全部
构建文件:../src/bag_test.cpp
调用:GCC C++编译器
g++-O0-g3-Wall-c-fmessage length=0-MMD-MP-MF“src/bag_test.d”-MT“src/bag_test.d”-o“src/bag_test.o”“../src/bag_test.cpp”
在../src/bag_test.cpp:2:0中包含的文件中:

../src/bag.h:23:66:警告:朋友声明'std::ostream&operator您不应该在周围留下警告。。。如果只是因为它们混淆了编译器的输出,它似乎在使用您自己的
向量
。这是故意的,还是您打算使用
std::vector
?显示在
vector.h
中定义的
vector
类的构造函数。粘贴整个错误消息。我不在乎多长时间,也不在乎你认为什么不重要。您所显示的只是位置信息,但实际错误仍然不在发布的消息中。很抱歉问一些愚蠢的问题,但您只显示了诱人的代码片段,因此我必须问:
向量
构造函数是公共的吗?您不应该允许为您的代码发出警告。直到你达到800技能水平,并且至少培养了三名学徒。注意:我从未坚持没有错误。我坚持说你没有发过。我仍然坚持说你没有把它粘贴进去。老实说,我没有看到这样的结果。好吧,至少你坚持让我发布错误,所以我注意到,幸亏没有错误。谢谢
**** Build of configuration Debug for project ADS ****
make all 
Building file: ../src/bag_test.cpp

Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/bag_test.d" -MT"src/bag_test.d" -o "src/bag_test.o" "../src/bag_test.cpp"
In file included from ../src/bag_test.cpp:2:0:
../src/bag.h:23:66: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const bag<DataType>&)’ declares a non-template function [-Wnon-template-friend]
../src/bag.h:23:66: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
In file included from ../src/bag.h:2:0,
                 from ../src/bag_test.cpp:2:
../src/vector.h: In instantiation of ‘vector<DataType>::vector(int) [with DataType = int]’:
../src/bag.h:34:53:   required from ‘bag<DataType>::bag(int) [with DataType = int]’
../src/bag_test.cpp:6:17:   required from here
../src/vector.h:100:6: warning: ‘vector<int>::vect_capacity’ will be initialized after [-Wreorder]
../src/vector.h:99:6: warning:   ‘int vector<int>::vect_size’ [-Wreorder]
../src/vector.h:108:1: warning:   when initialized here [-Wreorder]
Finished building: ../src/bag_test.cpp

Building target: ADS
Invoking: GCC C++ Linker
g++  -o "ADS"  ./src/bag_test.o   
Finished building target: ADS

**** Build Finished ****