为什么编译器认为传递的是 std::string&而不是 std:string

Why does the compiler think a std::string& is passed not a std:string

本文关键字:std string 编译器 为什么      更新时间:2023-10-16

我的问题是为什么编译器认为我在processSymbol(const string, enumDataType)传递一个std::string&。我之前在我的代码中使用了processSymbol(const string, enumDataType)并按预期工作,但是在这个函数中,我得到了这个错误

error: call of overloaded 'processSymbol(std::string&, symsErrorCode&)' is ambiguous|
note: candidates are:
note: void processSymbol(std::string, symsErrorCode&)|
note: void processSymbol(std::string, symsErrorCode)|

这是我想知道的功能。

void processSearchForSymbols(ifstream & inSyms, BST & symTable){
   symsErrorCode symsError;
   string symbol;
   while(inSyms >> symbol){
       symsError = NO_ERROR;
       processSymbol(symbol, symsError);
   }
}

processSymbol()编译我认为应该如何的函数是这样的:

void processInsertSymbolData(ifstream & inFile, BST & symTable){
string symbol, value, rFlag, fourCharSymbol;
symsErrorCode symsError;
while(inFile >> symbol >> value >> rFlag){
    symsError = NO_ERROR;
    processSymbol(symbol, symsError);
    if(symsError == NO_ERROR)
    {
        fourCharSymbol = createFourCharSymbol(symbol);
        processValue(value, symsError);
        if(symsError == NO_ERROR)
        {
            processRFlag(rFlag, symsError);
            if(symsError == NO_ERROR)
            {
                insertIntoSymTable(symsError, fourCharSymbol, value, rFlag, symTable);
            }
        }
    }
    errorOutput( symsError, symbol, fourCharSymbol, value);
}
return;
}

这是processSymbol(const string symbol, symsErrorCode symsError)函数

void processSymbol(const string symbol, symsErrorCode symsError){
if(symbol.length() > 10)
{
    symsError = LENGTH;
}
else if(!isalpha(symbol[0]))
{
    symsError = START_CHAR;
}
else
{
    for(unsigned int i = 1; i < symbol.length(); i++)
    {
        if( !isalnum(symbol[i]) && symbol[i] != '_' )
        {
            symsError = INVALID_CHAR;
        }
    }
}
return;
}

编译器无法区分您尝试调用的 processSymbol 的两个声明。 我建议使用指针(*)而不是枚举的&。 这将使编译器清楚地知道您打算调用哪个版本的函数。

processSymbol(const string symbol, symsErrorCode* symsError) ...