提问人:bad_chemist 提问时间:12/16/2021 更新时间:12/16/2021 访问量:192
无法在 C++ 向量和对象之间创建映射
Unable to create a map between a C++ vector and an object
问:
我正在尝试在粒子和它所在的位置之间创建映射。从本质上讲,我想要我的地图,其中 a 和 is a Particle。M[loc] = p
loc
std::vector <int>
p
这是我拥有的代码:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
class Particle{
public:
std::vector <int> coords; // the coordinates of the particles
std::string ptype;
int orientation;
bool operator<(const Particle& rhs)const{
return coords < rhs.coords;
}
// constructor
Particle (std::vector <int> crds, std::string type_, int orientation_): coords (crds), ptype (type_), orientation (orientation_){
}
// destructor
~Particle(){
}
Particle( const Particle &other); // copy constructor
// print location of the particle
void printCoords();
};
int main(int argc, char* argv[]){
std::map <std::vector <int>, Particle> OccupancyMap;
Particle p ({0,0,0}, "monomer", 0);
std::cout << p.ptype;
Particle pdash = p;
OccupancyMap[p.coords] = pdash ;
}
当我尝试编译此代码时,我收到一条大量错误消息:
In file included from main.cpp:1:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:215:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:14:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/string:506:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/string_view:175:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__string:57:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/algorithm:643:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:677:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/tuple:1401:7: error: no matching constructor for initialization of 'Particle'
second(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/utility:507:11: note: in instantiation of function template specialization 'std::__1::pair<const std::__1::vector<int>, Particle>::pair<const std::__1::vector<int> &, 0>' requested here
: pair(__pc, __first_args, __second_args,
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:1684:31: note: in instantiation of function template specialization 'std::__1::pair<const std::__1::vector<int>, Particle>::pair<const std::__1::vector<int> &>' requested here
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:1562:21: note: in instantiation of function template specialization 'std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::vector<int>, Particle>, void *>>::construct<std::__1::pair<const std::__1::vector<int>, Particle>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
__a.construct(__p, _VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:1413:14: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::vector<int>, Particle>, void *>>>::__construct<std::__1::pair<const std::__1::vector<int>, Particle>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
{__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__tree:2194:20: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<std::__1::vector<int>, Particle>, void *>>>::construct<std::__1::pair<const std::__1::vector<int>, Particle>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
__node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__tree:2139:29: note: in instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::__map_value_compare<std::__1::vector<int>, std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::less<std::__1::vector<int>>, true>, std::__1::allocator<std::__1::__value_type<std::__1::vector<int>, Particle>>>::__construct_node<const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
__node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/map:1521:20: note: in instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::__map_value_compare<std::__1::vector<int>, std::__1::__value_type<std::__1::vector<int>, Particle>, std::__1::less<std::__1::vector<int>>, true>, std::__1::allocator<std::__1::__value_type<std::__1::vector<int>, Particle>>>::__emplace_unique_key_args<std::__1::vector<int>, const std::__1::piecewise_construct_t &, std::__1::tuple<const std::__1::vector<int> &>, std::__1::tuple<>>' requested here
return __tree_.__emplace_unique_key_args(__k,
^
main.cpp:51:15: note: in instantiation of member function 'std::__1::map<std::__1::vector<int>, Particle, std::__1::less<std::__1::vector<int>>, std::__1::allocator<std::__1::pair<const std::__1::vector<int>, Particle>>>::operator[]' requested here
OccupancyMap[p.coords] = pdash ;
^
main.cpp:35:5: note: candidate constructor not viable: requires single argument 'other', but no arguments were provided
Particle( const Particle &other); // copy constructor for some reason i dont know
^
main.cpp:26:5: note: candidate constructor not viable: requires 3 arguments, but 0 were provided
Particle (std::vector <int> crds, std::string type_, int orientation_): coords (crds), ptype (type_), orientation (orientation_){
^
1 error generated.
我不太明白这是怎么回事。这里错误的本质是什么?它指向什么概念? 我将不胜感激你对我的任何建议。如果这是一个明显的问题,我深表歉意。
答: 暂无答案
评论
OccupancyMap[p.coords]
OccupancyMap.emplace(p.coords, pdash);