TinyXML #include problem... Using libraries - c++

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.

It's called TiXmlDocument, not TiXMLDocument
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.

dump_to_stdout( &doc ); // defined later in the tutorial
Here's your problem.
dump_to_stdout takes a const char* which TiXmlDocument is definitely not.
You're already in that function, so assuming the file loads you'll have infinite recursion.
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*);

Related

Fix 'no matching function for call to ...' error?

I'm trying to run this .cpp file in Codeblocks (only change is adding #include for windows.h to beginning).
It uses OpenGL, GLUT, and GLUI. I think I've gotten OpenGL and GLUT to work in Codeblocks but GLUI is still giving me some issues. At first, I downloaded GLUI from here and copied glui.h into C:\Program Files (x86)\CodeBlocks\MinGW\include\GL.
I was getting an error around line 455:
void control_cb( int control ){
if (control == 5){
GLUI_Master.close_all();
(...)
};
which stated there was an undefined reference to GLUI_Master.
I then realized that the GitHub repository also had a GLUI header file so I deleted the other header file and copied this new one into the same place as above.
I was able to get a little further now, getting an error around line 508:
void createSettingsMenu(){
(...)
new GLUI_Button( settings, "Update", 5,control_cb);
//settings->set_main_gfx_window(mainWindow);
(...)
};
which stated there was no matching function for call to:
GLUI_Button::GLUI_Button(GLUI*&, const char [7], int, void(&) (int)).
I'm not sure what this means, but it also says
**note: candidate: GLUI_Button::GLUI_Button()
note: candidate expects 0 arguments, 4 provided**
around line 847:
GLUI_Button( void ) {
sprintf( name, "Button: %p", this );
type = GLUI_CONTROL_BUTTON;
h = GLUI_BUTTON_SIZE;
w = 100;
alignment = GLUI_ALIGN_CENTER;
can_activate = true;
};
I'm assuming this is related to the error but I'm not sure how to fix this -- I thought this error was surely caused by an issue in setting up GLUI with Codeblocks.

Calling zlib (C code) from a C++ file

I have a .cpp file that contains the following function to decompress a file via zlib:
#include <zlib.h>
#include <fstream>
bool gzip_uncompress(std::string &compressed_file_path,std::string &uncompressed_file_path)
{
char outbuffer[1024*16];
gzFile infile = (gzFile)gzopen(compressed_file_path, "rb");
FILE *outfile = fopen(uncompressed_file_path, "wb");
gzrewind(infile);
while(!gzeof(infile))
{
int len = gzread(infile, outbuffer, sizeof(outbuffer));
fwrite(outbuffer, 1, len, outfile);
}
fclose(outfile);
gzclose(infile);
return true;
}
This looks like it should run to me, but I'm getting compile time errors stating:
No matching function call to 'gzopen'
and
No matching function call to 'fopen'
The only thing I can thing of is that I am calling these in a C++ file, and the zlib is a C library. I'm not sure about the fopen error though.
Does anyone see how I can call these functions and get around the compile error?
I hava also tried:
extern "C" {
#include <zlib.h>
}
but still no go. Am I barking up the wrong tree? Should I move this function into a C file? But then I'd presumable have the same issue.
Use .c_str() when passing a std::string to functions that expect a char *.
there are two things here...
Compile time inclusion of C header in C++ file
Linking C compiled lib (zlib) with C++ program
for the first issue, your compiler is not able to see declaration of gzopen, there might be many reasons for that..
please check the declaration of functions gzopen in zlib.h
Make sure you need to include path to zlib.h in the arguments to g++
ex: -I/usr/local/include/zlib.h
Please follow the link below for developer documentation
http://www.zlib.net/manual.html
Please follow the help
For 2. Linking your C++ program with zlib, please use
extern "C" {
#include <zlib.h>
}

C function calls in C++ class namespace

I'm attempting to familiarize myself with C++ by way of a project, but I have hit an error that I am not quite sure how to deal with. I have the following code:
void myclass::write(std::string str) {
write(filedes, (void *)str.c_str(), str.length());
}
Where filedes is an instance variable of myclass. Attempting to compile this yields the error:
myclass.cpp: In member function ‘void myclass::write(std::string)’:
myclass.cpp:38: error: no matching function for call to ‘myclass::write(int&, void*, size_t)’
myclass.cpp:37: note: candidates are: void myclass::write(std::string)
myclass.hpp:15: note: void myclass::write(std::string, int)
So what am I doing wrong? Can I not legally make this function call from the method?
Presumably you want to call write(2), which is in the global namespace:
::write(filedes, str.c_str(), str.length());
(you might need to #include <unistd.h>).
It looks like the compiler isn't aware of a global write function. Did you include the right headers (<unistd.h> in unix)? (Also, that (void *) cast is useless.)
filedes is not an instance variable of your class (not an instance of your class), but a member of your class with type int, as I see from the compiler error. If you want to call a function from a global namespace with the same name as a method there, use ::write(...). By the way: Use std::size_t for length, not int.
Compiler thinks that write is your class method and it isnt. If you are talking about system call write you should #include unistd.h and use ::write(filedes, (void *)str.c_str(), str.length());
it depends what is the write function if it is a system function then you have to use
::write , if it is in some other code make sure you put extern, use the header or the code that implements the function

Trouble converting an Arduino library from Arduino 0023 to 1.0

I'm using a library that unfortunately, it appears the developer no longer works on or replies to issues in his Git repository. The library is used to drive HT1632C LED matrix drivers, and while it works in 0022/0023, it does not work in Arduino 1.0. When compiled, my Sketch gives the following error:
In file included from Final_code__1_0compatible.cpp:7:
C:\arduino-1.0\libraries\ht1632c/ht1632c.h:182: error: conflicting return type specified for 'virtual void ht1632c::write(uint8_t)'
C:\arduino-1.0\hardware\arduino\cores\arduino/Print.h:48: error: overriding 'virtual size_t Print::write(uint8_t)'
Referencing this site: http://arduino.cc/forum/index.php?topic=82450.0, I found a comment from someone who had the same compilation errors I did (but with another library). It looks like their fix was to replace something with "size_t"
Line 200: size_t write(uint8_t); //changed to resolve conflict with print.h
The conflicting lines in the library's .h and .cpp files look to be:
.h:
void write(uint8_t chr);
.cpp:
void ht1632c::write(uint8_t chr)
{
byte x, y;
if (chr == '\n') {
//y_cur += font_height;
} else {
//x_cur += putchar(x_cur, y_cur, chr, GREEN, PROPORTIONAL);
//x_cur = 0;
//y_cur = 0;
}
//sendframe();
}
I'm not a C/C++ expert, but am I correct, in that if I change the .h to be "size_t write(uint8_t chr)" and the .cpp to be "size_t ht1632c::write(uint8_t chr)" that this will work?
I tried doing it, and it compiles, but I don't know if I replace the word "void" with "size_t", or if I need to replace the "uint8_t" with "size_t".
so,
size_t ht1632c::write(uint8_t chr)
is the right function change. You should also add a
return 1;
right after the
sendframe();
line. write is expected to return the number of characters successfully written, as you don't have any way in the code as pasted to determine if there is an error in the writing, you should just say it worked.

Using boost::iostreams::mapped_file_source with wide character strings

If I instantiate a mapped_file_source (boost 1.46.1 ) with a narrow character string as in the following I don't have a problem:
boost::iostreams::mapped_file_source m_file_( "testfile.txt" );
However if I try to use a wide string:
boost::iostreams::mapped_file_source m_file_( L"testfile.txt" );
I get the following compiler error in VC2010 SP1:
P:\libs\boost_1_46_1\boost/iostreams/device/mapped_file.hpp(128): error C2248: 'boost::iostreams::detail::path::path' : cannot access private member declared in class 'boost::iostreams::detail::path'
P:\libs\boost_1_46_1\boost/iostreams/detail/path.hpp(111) : see declaration of 'boost::iostreams::detail::path::path'>
P:\libs\boost_1_46_1\boost/iostreams/detail/path.hpp(37) : see declaration of 'boost::iostreams::detail::path'
If I instead try to pass the constructor a boost::filesystem::path I get the following error:
P:\libs\boost_1_46_1\boost/iostreams/device/mapped_file.hpp(128): error C2664: 'boost::iostreams::detail::path::path(const std::string &)' : cannot convert parameter 1 from 'const boost::filesystem3::path' to 'const std::string &'
Reason: cannot convert from 'const boost::filesystem3::path' to 'const std::string'
I feel like I'm missing something obvious, but I'm just running around in circles trying to figure out what the compiler is trying to tell me, but I'm just getting lost. That palm to forehead moment is just not happening.. What is it that I am doing incorrectly?
The constructor defined in mapped_file.hpp looks like the following:
// Constructor taking a parameters object
template<typename Path>
explicit mapped_file_source(const basic_mapped_file_params<Path>& p);
The basic_mapped_file_params class constructors look like this:
// Construction from a Path
explicit basic_mapped_file_params(const Path& p) : path(p) { }
// Construction from a path of a different type
template<typename PathT>
explicit basic_mapped_file_params(const PathT& p) : path(p) { }
Where the template class is defined as:
// This template allows Boost.Filesystem paths to be specified when creating or
// reopening a memory mapped file, without creating a dependence on
// Boost.Filesystem. Possible values of Path include std::string,
// boost::filesystem::path, boost::filesystem::wpath,
// and boost::iostreams::detail::path (used to store either a std::string or a
// std::wstring).
template<typename Path>
struct basic_mapped_file_params
: detail::mapped_file_params_base
{
There is some additional help in the header that says:
// For wide paths, instantiate basic_mapped_file_params
// with boost::filesystem::wpath
If I take this approach with:
boost::iostreams::basic_mapped_file_params<boost::filesystem::wpath> _tmp(L"test.txt");
boost::iostreams::mapped_file_source m_file_( _tmp );
I get the same C2664 error mentioned above..
I know the compiler is telling me what the problem is, but looking at the header source and the comments leads me to believe that what I'm trying to accomplish is supported, it's just my approach that is incorrect. Am I misinterpreting what the header file is telling me? I know there is probably a good lesson about template instantiation and explicit/implicit conversion in here somewhere.
Interestingly enough, upgrading my boost install to 1.47.0 seems to cleared up C2664 error but I'm still getting the C2248 error about access to the private member.
With boost 1.48 I can do something like this.
#include <boost/filesystem.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <iostream>
int main()
{
boost::filesystem::path p(L"b.cpp");
boost::iostreams::mapped_file file(p); // or mapped_file_source
std::cout << file.data() << std::endl;
}
or you can do this with mapped_file_params(used create new file)
boost::filesystem::path p(L"aa");
basic_mapped_file_params<boost::filesystem::path> param; // template param
param.path = p;
param.new_file_size = 1024;
It's telling you that boost::iostreams::mapped_file_source's constructor does not take a wchar_t*, nor does it take a boost::filesystem::path. It only takes std::string, or types convertible to std::string. Or, to put it another way, you can't use UTF-16 paths with this object.
It looks like the documentation for mapped_file is pretty old and does not reflect what is in the header or in the header comments. In order to instantiate a boost::iostreams:mapped_file_source object with a wide character string you need to explicity pass in the boost::iostreams::detail::path like this:
boost::iostreams::mapped_file_source m_file_( boost::iostreams::detail::path(boost::filesystem::path(L"testfile.txt")) );
I was able to get this to compile by stepping thought the error messages and determining how the template classes were being instantiated and finally saw that boost::iostreams::detail::path had a private constructor that took a &std::wstring as a parameter which is where the code was failing to compile.