首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++结构错误“调用没有匹配函数.”

C++结构错误“调用没有匹配函数.”
EN

Stack Overflow用户
提问于 2013-12-08 02:51:32
回答 4查看 4.7K关注 0票数 1

我目前有以下结构

代码语言:javascript
复制
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;
                    }
};

以及以下代码:

代码语言:javascript
复制
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上说错了:

代码语言:javascript
复制
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上的错误是:

代码语言:javascript
复制
error: no matching function for call to 'Node::Node(LR0Item*&)'
note: candidates are: Node::Node()|
note:                 Node::Node(const Node&)

我不太清楚这是怎么回事,也不知道怎么解决。

编辑:澄清,而不是使用C++11。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-12-08 02:55:31

您没有为您的类创建构造器。

您似乎想要一个用于LR0Item(const &,向量,int)和一个用于节点(ConstLR0Item&)

将其添加到LR0Item中:

代码语言:javascript
复制
LR0Item(const String& lhs_p, vector<string> rhs_p, int dpos_p)
     : lhs(lhs_p), rhs(rhs_p), dpos(dpos_p) {}

然后添加到Node中:

代码语言:javascript
复制
Node(LR0Item * lr) : item(lr) {}

{}中添加初始化所需的任何其他操作

您可能也希望使用复制构造函数,因为一旦定义了构造函数,您就没有默认的构造函数可用。

如果您想更深入地理解构造函数和重载operator =,请看更多的内容。

票数 0
EN

Stack Overflow用户

发布于 2013-12-08 02:55:50

您需要为LR0Item创建一个与错误消息中指定的签名相匹配的构造函数:

代码语言:javascript
复制
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,当然是:

代码语言:javascript
复制
LR0Item::LR0Item(const std::string&, const std::vector<std::string>&, int)
票数 1
EN

Stack Overflow用户

发布于 2013-12-08 02:57:49

在C++11中,您可以尝试以下操作:

代码语言:javascript
复制
// 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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20449312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档