3

Hey, i'm really trying to get TinyXML to at least read a file but it says "main.cpp:8: error: ‘TiXMLDocument’ was not declared in this scope"

This is the code im using:

TiXMLDocument("demo.xml");

Ideally i want to read able to read files and output the XML so i also tried this code i found online in a tutorial

#include <iostream>

#include "tinyxml.h"
#include "tinystr.h"

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 ); // defined later in the tutorial
    }
    else
    {
        printf("Failed to load file \"%s\"\n", pFilename);
    }
}

int main(void)
{
    dump_to_stdout("demo.xml");
    return 0;
}

And the errors I'm getting now are:

main.cpp: In function ‘void dump_to_stdout(const char*)’:
main.cpp:13: error: cannot convert ‘TiXmlDocument*’ to ‘const char*’ for argument ‘1’ to ‘void dump_to_stdout(const char*)’

If it helps im on a mac, ive tried compiling in terminal as well as textmate. I also tried to compile the cpp files for TinyXML separately before compiling main.cpp and i have no idea why i cant print out demo.xml let alone read it.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
CurtisJC
  • 680
  • 3
  • 9
  • 23

2 Answers2

1
  1. It's called TiXmlDocument, not TiXMLDocument
  2. You can't call a function that you haven't declared yet. Since you're trying to call an undeclared overload of dump_to_stdout, the compiler assumes you want to call the version that takes const char * and fails.
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • sorry, was working on the second lot of code... but i tried changing to TiXmlDocument and still didn't work. Undefined symbols: "TiXmlNode::~TiXmlNode()", referenced from: TiXmlDocument::~TiXmlDocument()in ccfppiJ4.o "vtable for TiXmlDocument", referenced from: TiXmlDocument::~TiXmlDocument()in ccfppiJ4.o "TiXmlString::nullrep_", referenced from: TiXmlString::quit() in ccfppiJ4.o "TiXmlDocument::TiXmlDocument(char const*)", referenced from: _main in ccfppiJ4.o ld: symbol(s) not found collect2: ld returned 1 exit status – CurtisJC Jan 25 '11 at 16:24
  • @CurtisJC: that's a linker error. Are you linking with `-ltinyxml`? – Fred Foo Jan 25 '11 at 17:57
  • @AFAIK, I've never used tinyxml has a lib. I've always just had to include the source files in my project. It's my understanding that's how they've always been distributed. Could that be the issue here? – Moo-Juice Jan 25 '11 at 18:04
  • @Moo-Juice, @CurtisJC: TinyXML is distributed as a shared library on some Linux platforms, including Debian and Ubuntu. Otherwise, Curtis forgot to link in the object files. – Fred Foo Jan 25 '11 at 18:09
  • i just have all the files in the same folder.. so i think this must be the problem... how do i link them? and im guessing i have to link with main.cpp aswell? – CurtisJC Jan 25 '11 at 19:47
  • 1
    When you've compiled the all files (with `gcc -c`), run `gcc` with all the resulting `.o` files. See the GCC manual for details. – Fred Foo Jan 25 '11 at 20:15
  • ok... as im working with c++ ive been using g++ as opposed to gcc, but tried that anyways... in terminal g++ -c tinystr.cpp tinyxml.cpp which gave me .o files, after that in terminal g++ tinystr.o tinyxml.o main.cpp and im getting "undefined symbols" when it trys to compile – CurtisJC Jan 25 '11 at 20:35
  • @CurtisJC, yes, `g++`, sorry. Which symbols are undefined? – Fred Foo Jan 25 '11 at 20:40
  • attempted a copy paste but too bulky.. Undefined symbols: "vtable for TiXmlDocument", referenced from: TiXmlDocument::TiXmlDocument(char const*)in tinyxml.o TiXmlDocument::TiXmlDocument()in tinyxml.o TiXmlDocument::TiXmlDocument()in tinyxml.o TiXmlDocument::TiXmlDocument(char const*)in tinyxml.o TiXmlDocument::TiXmlDocument(TiXmlDocument const&)in tinyxml.o TiXmlDocument::TiXmlDocument(TiXmlDocument const&)in tinyxml.o TiXmlDocument::~TiXmlDocument()in cclsRFr7.o "TiXmlDocument::SetError(int, char const*, TiXmlParsingData*, TiXmlEncoding)", etc... – CurtisJC Jan 25 '11 at 20:49
  • @larsmans Ok was definitely a link problem... finally figured out how im supposed to run it, textmate is not good for linking cpp files apparently... thank you – CurtisJC Jan 26 '11 at 10:41
0
dump_to_stdout( &doc ); // defined later in the tutorial

Here's your problem.

  1. dump_to_stdout takes a const char* which TiXmlDocument is definitely not.
  2. You're already in that function, so assuming the file loads you'll have infinite recursion.
  3. It doesn't matter that you've got one defined later that takes a TiXmlDocument. At this point, the only dump_to_stdout that exists is the one you're in, hence the error. Forward declare the one you want before this function, e.g: void dump_to_stdout(TiXmlDocument*);
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • The compiler error clearly shows that no infinite recursion is taking place. It just won't compile. – Fred Foo Jan 25 '11 at 16:06
  • @larsmans, I am aware of that. I am just pointing out that if that did resolve (if the document had a const char* operator, for example), that it would recurse. It certainly doesn't form the basis of my suggestions. – Moo-Juice Jan 25 '11 at 16:14
  • all your other suggestions are entirely correct. However, C++'s overloading resolution will prevent infinite recursion unless `dump_to_stdout(TiXmlDocument*)` calls back into `dump_to_stdout(const char *)`. – Fred Foo Jan 25 '11 at 17:54