我正在尝试为基本的编译器创建符号表。我的Symbol类中有2个构造函数,一个有4个参数,一个有5个。我有一个简单的main函数,它试图创建一个Symbol对象,接受4个参数,然后再创建5个参数。编译器抱怨Symbol 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;
}
有一个预编译的头文件(.gch)必须已经过时。删除并重新编译就可以了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句