为什么这个函数重载没有起作用?

4
class CConfFile
{
    public:
        CConfFile(const std::string &FileName);
        ~CConfFile();
        ...
        std::string GetString(const std::string &Section, const std::string &Key);
        void GetString(const std::string &Section, const std::string &Key, char *Buffer, unsigned int BufferSize);
        ...
}

string CConfFile::GetString(const string &Section, const string &Key)
{
    return GetKeyValue(Section, Key);
}

void GetString(const string &Section, const string &Key, char *Buffer, unsigned int BufferSize)
{
    string Str = GetString(Section, Key);     // *** ERROR ***
    strncpy(Buffer, Str.c_str(), Str.size());
}

为什么我在第二个函数处会得到一个错误提示:too few arguments to function ‘void GetString(const std::string&, const std::string&, char*, unsigned int)' ?

谢谢。

3个回答

11

你没有为第二个函数添加 CConfFile:: 作用域。因此,它被编译为自由函数,这导致对 GetString 的调用解析为它本身(递归调用),需要四个参数。


3
因为CConFile::GetString()是一个类成员函数,正如其名称所示,因此在第二个函数中调用它的方式是不可访问的。您声明的另一个函数GetString()是一个全局函数。
您只是忘记在第二个函数中添加CConFile::了...

0
我认为这是因为没有CConfFile实例来调用该函数,所以它假设你正在调用另一个函数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接