class Foo {
vector<Bar> bars;
public:
Foo(int barcount) : { bars(barcount, 0); };
};
我试图将bars
向量转换为持有barcount
条的向量。但是,这样做时我遇到了2个错误:
Foo.cpp:8: error: expected identifier before ‘{’ token
Foo(int barcount) : { bars(barcount, 0); };
^
Foo.cpp:8: error: no match for call to ‘(std::vector<Bar>) (int&, int)’
Foo(int barcount) : { bars(barcount, 0); };
非常感谢任何可能出现问题的帮助。
编辑:这是我现在的代码(替换了Foo / Bar):
class Machine {
vector<Rotor> rotors;
public:
Machine(int rotorcount) : rotors(rotorcount, 0) {}
};
我收到一条很长的错误信息:
/usr/include/c++/4.8/bits/stl_vector.h: In instantiation of ‘void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_Integer, _Integer, std::__true_type) [with _Integer = int; _Tp = Rotor; _Alloc = std::allocator<Rotor>]’:
/usr/include/c++/4.8/bits/stl_vector.h:404:55: required from ‘std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = int; _Tp = Rotor; _Alloc = std::allocator<Rotor>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Rotor>]’
Machine.cpp:8:51: required from here
/usr/include/c++/4.8/bits/stl_vector.h:1166:59: error: no matching function for call to ‘std::vector<Rotor>::_M_fill_initialize(std::vector<Rotor>::size_type, int&)’
_M_fill_initialize(static_cast<size_type>(__n), __value);
^
/usr/include/c++/4.8/bits/stl_vector.h:1166:59: note: candidate is:
/usr/include/c++/4.8/bits/stl_vector.h:1212:7: note: void std::vector<_Tp, _Alloc>::_M_fill_initialize(std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = Rotor; _Alloc = std::allocator<Rotor>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = Rotor]
_M_fill_initialize(size_type __n, const value_type& __value)
^
/usr/include/c++/4.8/bits/stl_vector.h:1212:7: note: no known conversion for argument 2 from ‘int’ to ‘const value_type& {aka const Rotor&}’
答案 0 :(得分:3)
初始化列表位于逗号之后,但在构造函数体之前,并且后面没有分号:
Foo(int barcount) : bars(barcount, 0) {};
答案 1 :(得分:0)
no known conversion for argument 2 from ‘int’ to ‘const value_type& {aka const Rotor&}’
听起来你没有将int
作为单个参数的构造函数。