[英]C++ Struct Error “No matching function for call…”
I currently have the following structure(s) 我目前有以下结构
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;
}
};
and the following code: 和以下代码:
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;
I am getting an error on comment 1 saying: 我在评论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&)
And my error at comment 2 saying: 我在评论2的错误说:
error: no matching function for call to 'Node::Node(LR0Item*&)'
note: candidates are: Node::Node()|
note: Node::Node(const Node&)
I'm not entirely sure what is going on here or how to fix it. 我不完全确定这里发生了什么或如何解决。
EDIT: To clarify, not using C++11. 编辑:为了澄清,不使用C ++ 11。
You need to make a constructor for LR0Item which matches the signature specified in the error message: 您需要为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&)
Simplifying the templates and fixing up the const-ness, of course that is: 简化模板并修复一致性,当然是:
LR0Item::LR0Item(const std::string&, const std::vector<std::string>&, int)
You have not created constuctors for your classes 您尚未为课程创建构造函数
You seem to want one for LR0Item(const String&, vector, int) and one for Node(const LR0Item&) 您似乎想要一个用于LR0Item(const String&,vector,int)和一个用于Node(const LR0Item&)
Add this to LR0Item: 将此添加到LR0Item:
LR0Item(const String& lhs_p, vector<string> rhs_p, int dpos_p)
: lhs(lhs_p), rhs(rhs_p), dpos(dpos_p) {}
And add this to Node: 并将其添加到Node:
Node(LR0Item * lr) : item(lr) {}
In the {}
add anything else you need to do for initialization 在
{}
添加初始化所需执行的其他操作
You will likely want to heve the copy constructor as well since once you define a constructor, you will not have the default ones available to you 您也可能希望同时使用副本构造函数,因为一旦定义了构造函数,就不会有默认的可用构造函数
Look more into constructors and overloading operator =
if you want to understand this in more depth 更多地了解构造函数和重载
operator =
如果您想更深入地了解这一点
In C++11 you can try the following: 在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;
}
If you have a C++11 compiler then simply change the round parentheses in those calls, to curly braces . 如果您使用的是C ++ 11编译器,则只需将这些调用中的圆括号更改为花括号即可 。
Otherwise, for a C++03 compiler, define constructors . 否则,对于C ++ 03编译器,请定义构造函数 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.