我目前有以下结构
struct LR0Item{
string lhs;
vector<string> rhs;
int dpos;
};
struct Node{
LR0Item* item;
map<string, Node*> tr;
};
struct my_struct{
bool operator()(
const LR0Item &a,
const LR0Item &b) {
if(a.lhs != b.lhs)
return a.lhs<b.lhs;
if(a.dpos != b.dpos)
return a.dpos<b.dpos;
return a.rhs<b.rhs;
}
};以及以下代码:
vector<string> test_v; //assume this is filled with various strings
vector<Node*> N;
map<LR0Item,Node*,my_struct> nmap;
LR0Item* Q = new LR0Item("test", test_v, 3); //1 - error occurs here
Node* N1 = new Node(Q); //2 - error occurs here
N.push_back(N1);
nmap[*Q] = N1;我在评论1上说错了:
error: no matching function for call to 'LR0Item::LR0Item(std::string&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, int&)'|
note: candidates are: LR0Item::LR0Item()|
note: LR0Item::LR0Item(const LR0Item&)我在评论2上的错误是:
error: no matching function for call to 'Node::Node(LR0Item*&)'
note: candidates are: Node::Node()|
note: Node::Node(const Node&)我不太清楚这是怎么回事,也不知道怎么解决。
编辑:澄清,而不是使用C++11。
发布于 2013-12-08 02:55:31
您没有为您的类创建构造器。
您似乎想要一个用于LR0Item(const &,向量,int)和一个用于节点(ConstLR0Item&)
将其添加到LR0Item中:
LR0Item(const String& lhs_p, vector<string> rhs_p, int dpos_p)
: lhs(lhs_p), rhs(rhs_p), dpos(dpos_p) {}然后添加到Node中:
Node(LR0Item * lr) : item(lr) {}在{}中添加初始化所需的任何其他操作
您可能也希望使用复制构造函数,因为一旦定义了构造函数,您就没有默认的构造函数可用。
如果您想更深入地理解构造函数和重载operator =,请看更多的内容。
发布于 2013-12-08 02:55:50
您需要为LR0Item创建一个与错误消息中指定的签名相匹配的构造函数:
LR0Item::LR0Item(std::string&, std::vector<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::allocator<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > >&, int&)简化模板并修复const-ness,当然是:
LR0Item::LR0Item(const std::string&, const std::vector<std::string>&, int)发布于 2013-12-08 02:57:49
在C++11中,您可以尝试以下操作:
// Brackets instead of parentheses
LR0Item* Q = new LR0Item{"test", test_v, 3};
Node* N1 = new Node{Q};
int main() {
N.push_back(N1);
nmap[*Q] = N1;
}https://stackoverflow.com/questions/20449312
复制相似问题