开发者

Why this function overloading is not working?

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 Buffe开发者_StackOverflow社区rSize)
{
    string Str = GetString(Section, Key);     // *** ERROR ***
    strncpy(Buffer, Str.c_str(), Str.size());
}

Why do I get an error too few arguments to function ‘void GetString(const std::string&, const std::string&, char*, unsigned int)' at the second function ?

Thanks


You haven't scoped the second function with CConfFile::. It is being compiled as a free function, so the call to GetString resolves to itself (recursively), which requires four parameters.


Because the CConFile::GetString() is, as the name suggest, a class member function, that is not accessible the way you call it in the second function. The other function you're declaring, GetString(), is a global one.

You just forgot to add CConFile:: to the second function...


I would say it is because of not having a CConfFile instance to call that function, so it is assuming you are calling the other one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜