C++: Trouble Using String as Argument of a Function - c++

Okay, I am having trouble with the following piece of code (in a header file):
#ifndef XML_H_INCLUDED
#define XML_H_INCLUDED
#include "libxml/parser.h"
#include "libxml/xmlwriter.h"
#include <string>
class XmlFile{
public:
XmlFile(string filename){
file = xmlParseFile(filename);
}
xmlDocPtr file; //Pointer to xml file
};
#endif // XML_H_INCLUDED
The file is including in the main source file (but is not accessed, so its contents are not important).
I keep getting the following error (In Codeblocks):
error: cannot convert 'std::string' to 'const char*'
for argument '1' to 'xmlDoc* xmlParseFile(const char*)'|
I have run into this many times, and it is driving me crazy.
I would prefer not to use vectors if possible (adds another step in initializing the function.
What am I doing wrong? I've tried looking this up, but have not found any satisfactory answers.
Thanks in advance.

file = xmlParseFile(filename.c_str());

Related

Why g++ reported an error in C++ Standard Library in MacOS GDK?

I encountered a weird error when building my code:
Before I show you what the error was, I'd like to show you what the code is doing. This is a node class for my little cmd project, each node is a file/folder. I wrote a function to show the info of the node.
// Node.h
#include <string>
#include <memory>
using namespace std;
typedef bool NODE_TYPE;
#define FILE true
#define DIR false
class Node
{
private:
string _node_name;
NODE_TYPE _node_type;
string _create_date;
string _create_user;
weak_ptr<Node> _parent;
shared_ptr<Node> _sibling;
shared_ptr<Node> _child;
public:
/*Constructors, Destructors and other functions*/
const string toString();
};
// Node.cpp
#include "Node.h"
#include <string>
#include <sstream>
using namespace std;
const string Node::toString()
{
ostringstream buffer;
buffer << ... // the data member of Node
return buffer.str();
}
I wrote a test in main.cpp, and when I say g++ Node.cpp main.cpp -Wall -std=c++11, here's the error:
In file included from Node.cpp:6:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/sstream:184:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:163:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ostream:138:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:214:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:40:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/xlocale.h:93:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/xlocale/_stdio.h:32:33: error: expected expression
int fprintf_l(FILE * __restrict, locale_t __restrict, const char * __restrict, ...)
Errors like this are 21 more, I guess there was something wrong with SDK, so I reinstalled it, however still no work. Could anyone help me?
I'd like to thank all comments below my question, they are really helpful.
In C standard library, FILE is defined as a structure to which stores the info of a file opened by the program. So my own macro definition leads to wrong macro expansion.
Here I'd like to reference the suggestion by Scott Meyers: For simple constants, prefer const objects or enums to #defines.

C++: cin.getline - no instance of overload function

I'am trying to write a line of text to a .txt file, but visual studio 2015 is giving me the following error.
Severity Code Description Project File Line Suppression State
Error (active) no instance of overloaded function "std::basic_istream<_Elem, _Traits>::getline [with _Elem=char, _Traits=std::char_traits]" matches the argument list Studying c:\Users\Klaas\Documents\Visual Studio 2015\Projects\Studying\Studying\Studying.cpp 16
and
Severity Code Description Project File Line Suppression State
Error C2664 'std::basic_istream> &std::basic_istream>::getline(_Elem *,std::streamsize,_Elem)': cannot convert argument 1 from 'std::string' to 'char *' Studying1 c:\users\klaas\documents\visual studio 2015\projects\studying1\studying1\studying1.cpp 16
My source code:
#include <iostream>
#include "stdafx.h"
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream myFile;
string myText;
myFile.open("sometext.txt");
cout << "Write text to save to file: ";
cin.ignore();
cin.getline(myText, sizeof(myText));
myFile << myText;
myFile.close();
return 0;
}
I have done multiple searches and found answers such as including
cin.ignore();
above the
cin.getline
, but i just can't get it to work and i don't really understand what the error means, and i don't see why it thinks i'm trying to convert a string to char.
Any tips would be very welcome :)
You are not using correct function. Instead of cin.getline() you should use
std::getline(cin, myText)
cin.getline() expects a pre-allocated char*, and your myText is not. It is also very hard to somehow manage to preallocate a buffer long enough for std::basic_istream::getline(), so this function is almost never useful at all.
use getline(cin,myText) I honestly don't know why this problem exists or if it even is a problem but i know for a fact that this works.
See : http://www.cplusplus.com/reference/istream/istream/getline/
See : http://en.cppreference.com/w/cpp/io/basic_istream/getline
The first parameter of this function getline must be a char *, not a string.
char myText[1024];
cin.getline( myText, sizeof(myText) );
As mentioned everywhere else, it's better to use the std::getlinefunction:
http://en.cppreference.com/w/cpp/string/basic_string/getline

"invalid conversion from 'FILE* {aka _iobuf}' to 'int'" error

I want to change size of *.txt file, which is created before. For this i use chsize.
Code:
#include <iostream>
#include <stdio.h>
#include <io.h>
using namespace std;
int main()
{
FILE *wfile;
wfile = fopen("test.exe", "a");
chsize(wfile, 1024); //error is here
file.close();
return 0;
}
And here goes the error:
[Error] invalid conversion from 'FILE* {aka _iobuf*}' to 'int'
[-fpermissive]
Q: What is wrong here? I opened the file with fopen right how it explained in the internets.
Q2: I love Windows as a OS, but I don't want to learn Windows API for C++ or something like that. Is there a option to use something portable?
First parameter for chsize is file descriptor not FILE*.
You can use function "fileno".
How can I convert a file pointer ( FILE* fp ) to a file descriptor (int fd)?

No operator== match for std::string?

I got a strange problem with my program. So in the header I got something like this:
#ifndef SET1_INCLUDED
#define SET1_INCLUDED
#include <iostream>
using namespace std;
typedef std::string ItemType;
class Set1{
public:
------some public constructor and method in here-------
private:
ItemType setMember[100];
}
in 1 part of my function in the Set1.cpp file I got something like this :
if (setMember[i] == "foo") {exist = true;}
In this case, I got an error message that says "no operator found which takes a left-hand operand of type 'ItemType' ". However, if I change the std::string in the typedef into int or unsigned long, and change "foo" to some random number, the code works perfectly. Any suggestion? thx
You are missing the header file <string>, which means that you don't have all of the global operator == definitions visible in your program. This is likely the case of your problem.
To fix this, try adding in this line:
#include <string>
Hope this helps!
You need to include string header file to bring relative types and operator to scope.
#include <string>
Note:
It's bad coding practice to pull in everything from std namespace in header file.
//using namespace std;

C++ compiler error with stringbuf / ifstream

I cannot understand why my compiler (MSVC++2010) doesn't like this code:
// get_sum(filename as c-string) returns sum from file
int get_sum(const char* const s) {
stringbuf bill_buf;
ifstream bill_file;
bill_file.open(s);
bill_file.get(bill_buf, '\0'); // read whole file
bill_file.close();
return get_sum_from_string(bill_buf.str());
}
I get these errors (I translated them from German to English and give the correct line numbers for the code excerpt without leading comment):
Error 1 error C2079: 'bill_buf' uses undefined class 'std::basic_stringbuf<_Elem,_Traits,_Alloc>' (Line 2)
Error 2 error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem *,std::streamsize)': Conversion of parameter 1 from 'int' to 'char *' not possible (Line 5)
Error 3 error C2228: To the left of ".str" there must be a class/structure/union. (Line 7)
Has anyone got an idea what's going on there? Thanks a lot! (If anyone has got a better idea how to quickly get the whole file contents into a string, I'd also appreciate it)
You're missing an include. Here's your code, this time without using streambuf:
#include<fstream>
#include<string>
#include<iterator>
int get_sum(const char* const s) {
std::ifstream bill_file(s);
std::string contents((std::istreambuf_iterator<char>(bill_file)),
std::istreambuf_iterator<char>());
return get_sum_from_string(contents);
}
For #1, you probably forgot to #include <sstream> and only have a forward declaration from some other header in scope. #2 and #3 are follow-up errors, don't mind them, fix #1 first and go on.
Looks like you need to #include <sstream>.
1) In your header file (.h) you should specify "using namespace std". Otherwise, all your stream operations/variables etc have to start with 'std::'
2) Have you included the right headers? You should add
#include <sstream>