tom66
September 5th, 2009, 04:30 PM
Following code throws errors:
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int __PyPrintFormat(FILE *stream, std::string fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char *buffer;
if(vasprintf(&buffer, fmt, ap) != -1)
{
fputs(buffer, stream);
free(buffer);
return 0;
}
return 1;
}
int main()
{
__PyPrintFormat(stdout, "int output: %d\n", 5928);
return 0;
}
(yes I know I'm mixing C & C++, but it usually comes out for the better.)
I get errors compiling this:
simpletest.cpp:14: error: ‘std::string’ has not been declared
simpletest.cpp: In function ‘int __PyPrintFormat(FILE*, int, ...)’:
simpletest.cpp:20: error: invalid conversion from ‘int’ to ‘const char*’
simpletest.cpp:20: error: initializing argument 2 of ‘int vasprintf(char**, const char*, char*)’
simpletest.cpp: In function ‘int main()’:
simpletest.cpp:177: error: invalid conversion from ‘const char*’ to ‘int’
simpletest.cpp:177: error: initializing argument 2 of ‘int __PyPrintFormat(FILE*, int, ...)’
I have declared std::string by #including string.h... right?
I'm compiling with g++ simpletest.cpp.
All help appreciated!
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int __PyPrintFormat(FILE *stream, std::string fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char *buffer;
if(vasprintf(&buffer, fmt, ap) != -1)
{
fputs(buffer, stream);
free(buffer);
return 0;
}
return 1;
}
int main()
{
__PyPrintFormat(stdout, "int output: %d\n", 5928);
return 0;
}
(yes I know I'm mixing C & C++, but it usually comes out for the better.)
I get errors compiling this:
simpletest.cpp:14: error: ‘std::string’ has not been declared
simpletest.cpp: In function ‘int __PyPrintFormat(FILE*, int, ...)’:
simpletest.cpp:20: error: invalid conversion from ‘int’ to ‘const char*’
simpletest.cpp:20: error: initializing argument 2 of ‘int vasprintf(char**, const char*, char*)’
simpletest.cpp: In function ‘int main()’:
simpletest.cpp:177: error: invalid conversion from ‘const char*’ to ‘int’
simpletest.cpp:177: error: initializing argument 2 of ‘int __PyPrintFormat(FILE*, int, ...)’
I have declared std::string by #including string.h... right?
I'm compiling with g++ simpletest.cpp.
All help appreciated!