嵌套通过引用传递

时间:2011-08-19 06:43:05

标签: c++ pass-by-reference

我需要通过引用进行嵌套传递。

已编辑包含实际代码

确切错误是:

  

没有用于调用'CLItem :: getValue(std :: string *&)'的匹配函数

完整错误是:

App/CycCmdLine.cpp: In member function ‘std::string CycCmdLine::getEnvironment()’:
App/CycCmdLine.cpp:245: error: no matching function for call to ‘CycCmdLine::getValue(const char [16], std::string&)’
./../CmdLine/CmdLine.h: In member function ‘bool CmdLine::getValue(std::string, T&) [with T = std::string*]’:
App/CycCmdLine.cpp:384:   instantiated from here
./../CmdLine/CmdLine.h:237: error: no matching function for call to ‘CLItem::getValue(std::string*&)’
./../CmdLine/CmdLine.h:145: note: candidates are: bool CLItem::getValue(bool&)
./../CmdLine/CmdLine.h:146: note:                 bool CLItem::getValue(std::string&)
./../CmdLine/CmdLine.h:147: note:                 bool CLItem::getValue(long unsigned int&)
./../CmdLine/CmdLine.h:148: note:                 bool CLItem::getValue(std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)
./../CmdLine/CmdLine.h:149: note:                 bool CLItem::getValue(std::list<std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::list<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&)

更新的代码:

// initial caller
string CycCmdLine::getEnvironment()
{
    string sRet="";
    do
    {     
        if(hasLogFile())
            {
                string sLogFile="";
                getValue(CYCCMD_CMDSTR_LOGFILE, sLogFile);
                sRet+="\n  -Profiler logfile: "+sLogFile;
            }
    }while(false);
    return(sRet);
}

// CmdLine::getValue function definition (this is in the header file, as a template should be)
template <typename T>
bool CmdLine::getValue(string sName, T &tValue)
{
    bool bRet=false;
    do
    {
    // try to get the iterator
    bool bExists;
    map<string, CLItem*>::iterator itItr;
    bExists=getMapElement(sName, itItr);

        // go ahead and return the value
        if(!itItr->second->getValue(tValue)) { break; }
        bRet=true;
    }while(false);
    return(bRet);
}

bool CLItem::getValue(string& sValue)
{
    bool bRet=false;
    do
    {
        // is this the correct type?
        if(!isType(CLType_STRING)) { break; }

        // return the value
        sValue=m_sValue;
        bRet=true;
    }while(false);
    return(bRet);
}

2 个答案:

答案 0 :(得分:3)

如果我们查看消息的这一部分

./../CmdLine/CmdLine.h: In member function ‘bool CmdLine::getValue(std::string, T&) [with T = std::string*]’:
App/CycCmdLine.cpp:384:   instantiated from here
./../CmdLine/CmdLine.h:237: error: no matching function for call to ‘CLItem::getValue(std::string*&)’

似乎在App/CycCmdLine.cpp:384处,您调用getValue,Tstd::string*。然后,这会导致模板尝试调用不存在的函数。

开始调查此行384!

答案 1 :(得分:2)

您可以使用此代码重现该错误。

#include <string>

struct X
{
    void foo(std::string& n)
    {
    }

    void bar()
    {
       std::string* s;
       foo(s);
    }
};


prog.cpp: In member function ‘void X::bar()’:
prog.cpp:12: error: no matching function for call to ‘X::foo(std::string*&)’
prog.cpp:5: note: candidates are: void X::foo(std::string&)

建议的参考符号不是很重要。您确实尝试传递指向期望值的指针,并且编译器正在猜测该调用的合适函数可能是什么样子。 (这可能意味着你传递左值,所以函数可以通过引用来获取它。)

另请注意,它列出候选人,函数名称相同但参数类型不合适。


现在您已经发布了实际代码,也许在错误的实例化中检查模板类型是什么(错误消息也应该提到)。


现在您还发布了错误消息:

App/CycCmdLine.cpp: In member function ‘std::string CycCmdLine::getEnvironment()’:
App/CycCmdLine.cpp:245: error: no matching function for call to ‘CycCmdLine::getValue(const char [16], std::string&)’

这是第一个错误。翻译:类CycCmdLine没有成员函数getValue(也没有这个名称的全局函数)。该名称的函数是CmdLine的成员。

./../CmdLine/CmdLine.h: In member function ‘bool CmdLine::getValue(std::string, T&) [with T = std::string*]’:
App/CycCmdLine.cpp:384:   instantiated from here
./../CmdLine/CmdLine.h:237: error: no matching function for call to ‘CLItem::getValue(std::string*&)’

这是你要问的第二个错误。您正在CycCmdLine.cpp的第384行传递指向字符串的指针(位于与您想象的不同的位置)。