我在ContextFreeGrammar类的标题中定义了这个函数:
friend ContextFreeGrammar & operator<<(ContextFreeGrammar & cfg, string toRead);
并在cpp文件中实现为:
ContextFreeGrammar & operator<<(ContextFreeGrammar & cfg, string toRead)
{
if (toRead[0] == 'I')
{
string parsedNameOfVariable = Variable::parseLabel(toRead.substr(1, toRead.length() - 1));
cfg.setInitialVariable(Variable(parsedNameOfVariable));
return cfg;
}
Rule newRule = Rule::parseLabel(toRead);
cfg.addRule(newRule);
return cfg;
}
然而在main()中,当我创建这个对象并将其初始化时(导入了ContextFreeGrammar.h):
ContextFreeGrammar cfg;
cfg << "{A}->{B}"
<< "{B}->{C}"
<< "{C}->{D}"
<< "{D}->a{N}"
<< "{B}->c";
我收到此错误:|未定义引用`operator&lt;&lt;(ContextFreeGrammar&amp;,std :: __ cxx11 :: basic_string,std :: allocator&gt;)&#39; |