2

我正在尝试为基本编译器创建符号表。我的 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;
}

符号.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*/

符号.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;
}
4

1 回答 1

3

有一个预编译的头文件 (.gch) 一定已经过时了。删除它并重新编译正常。

于 2016-01-30T02:51:39.967 回答