在网上找到了一个小的例子,是用来实现函数的递归调用的,代码详情如下:

 C/C++ code
void dump_to_stdout(const char* pFilename)
{
    TiXmlDocument doc(pFilename);
    bool loadOkay = doc.LoadFile();
    if (loadOkay)
    {
        printf("\n%s:\n", pFilename);
        dump_to_stdout( &doc ); // 此行报错
    }
    else
    {
        printf("Failed to load file \"%s\"\n", pFilename);
    }
}

如上代码,在VS2008里调试这个例子时报错:

 error C2664: “dump_to_stdout”: 不能将参数 1 从“TiXmlDocument *”转换为“const char *”

应该如何解决此问题,实现函数的递归调用呢?

是因为漏掉了dump_to_stdout( &doc );这个函数,因为这个函数需要在后面进行声明,如下是修改后的完整代码:

 C/C++ code:
void dump_to_stdout( TiXmlNode* pParent, unsigned int indent = 0 )
{
    if ( !pParent ) return;

    TiXmlNode* pChild;

    TiXmlText* pText;
    int t = pParent->Type();
    printf( "%s", getIndent(indent));
    int num;

    switch ( t )

    {
    case TiXmlNode::DOCUMENT:
        printf( "Document" );
        break;

    case TiXmlNode::ELEMENT:

        printf( "Element [%s]", pParent->Value() );
        num=dump_attribs_to_stdout(pParent->ToElement(), indent+1);
        switch(num)
        {
            case 0:  printf( " (No attributes)"); break;
            case 1:  printf( "%s1 attribute", getIndentAlt(indent)); break;
            default: printf( "%s%d attributes", getIndentAlt(indent), num); break;
        }
        break;

    case TiXmlNode::COMMENT:

        printf( "Comment: [%s]", pParent->Value());
        break;

    case TiXmlNode::UNKNOWN:

        printf( "Unknown" );
        break;

    case TiXmlNode::TEXT:

        pText = pParent->ToText();
        printf( "Text: [%s]", pText->Value() );
        break;

    case TiXmlNode::DECLARATION:

        printf( "Declaration" );
        break;
    default:
        break;
    }
    printf( "\n" );
    for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
    {
        dump_to_stdout( pChild, indent+1 );
    }
}

此文由Web开发之答疑解惑源整理,如需转载,请注明原文出处:,谢谢!

更多热门文章:

1.如何统计一个页面打开所需要的时间?()

2.ASP哈希表类()

3.用户管理问题()