为什么VS下用tinyxml解析xml无法进行const char转换?

在网上找到了一个小的例子,是用来实现函数的递归调用的,代码详情如下:
 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开发之答疑解惑源www.znjcx.com整理,如需转载,请注明原文出处:http://www.znjcx.com/html/y2012/3478_why-cant-with-tinyxml-parsing-xml-vs-const-char-conversion.html,谢谢!

更多热门文章:

1.如何统计一个页面打开所需要的时间?(http://www.znjcx.com/html/y2012/2404_time-needed-to-open-a-page-of-statistics.html)

2.ASP哈希表类(http://www.znjcx.com/html/y2012/2401_asp-hash-table-class.html)

3.用户管理问题(http://www.znjcx.com/html/y2012/2399_user-management.html)

本文出自 “Web开发之答疑解惑源” 博客,转载请与作者联系!

你可能感兴趣的:(转换,char,Const,tinyxml解析)