【发布时间】:2016-01-30 02:27:00
【问题描述】:
我正在尝试为基本编译器创建符号表。我的 Symbol 类中有 2 个构造函数——一个接受 4 个参数,一个接受 5 个参数。 我有一个简单的 main 函数,它尝试创建一个 Symbol 对象,接受 4 个参数,然后是另一个带有 5 个参数的对象。编译器抱怨符号 b 有 5 个参数:
error: no matching function for call to ‘Symbol::Symbol(std::string&, std::string&, int, kind, int)’
symbol.h:14: note: candidates are: Symbol::Symbol(const Symbol::string&, const Symbol::string&, int, kind)
symbol.h:6: note: Symbol::Symbol(const Symbol&)
我不知道为什么它说没有匹配的函数可以调用,因为它在那里。虽然我不确定“const Symbol::string&”是否与“std::string&”不同会导致一些问题,或者如果是这样的话如何解决。
这里是主要的:
int main(){
string x="x";
string y="y";
Symbol a(x,"int",0,SCALAR);
Symbol b(y,"char",0,ARRAY,5);
//operator << is overloaded
cout << a << endl;
cout << b << endl;
return 0;
}
symbol.h:
#ifndef SYMBOL_H
#define SYMBOL_H
#include "type.h"
#include <string>
class Symbol{
typedef std::string string;
string _name;
Type _type;
public:
const string& getName() const;
const Type& getType() const;
Symbol(const string& name, const string& specifier, int indirection, kind kind);
Symbol(const string& name, const string& specifier, int indirection, kind kind, int length);
};
std::ostream& operator<<(std::ostream& ostr, const Symbol& symbol);
#endif /*SYMBOL_H*/
symbol.cpp
#include "symbol.h"
#include <iostream>
using namespace std;
/*other member function definitions*/
Symbol::Symbol(const string& name, const string& specifier, int indirection, kind kind)
{
_name = name;
Type a(specifier,indirection,kind);
_type = a;
}
Symbol::Symbol(const string& name, const string& specifier, int indirection, kind kind, int length)
{
_name = name;
/*Type is another class that holds the description of the type of the
*particular variable, function,etc. declaration
*/
Type a(specifier,indirection,kind,length);
_type = a;
}
【问题讨论】:
-
您的编译器认为在您的调用中有一个尾随
, int,但在您的构造函数中没有...表明这不是您的真实代码,或者您没有进行全新构建,或者类似那个。 -
啊。是的。它创建了一个.gch 文件,我没有看到它一定已经过时了。删除并重新编译,一切都很好。谢谢@LightnessRacesinOrbit
标签: c++ constructor constructor-overloading