Unable to use std::string in creating members in rapidjason - rapidjson

I have tried the following code - but getting a compile error
rapidjson::Document details;
details.SetObject();
std::string s = "kjfhgdkgjhfkjfhj";
details.AddMember("farm", s, details.GetAllocator());
What is the correct way of creating a member with a std::string as a value

Related

initializing string in class using constructor

I am creating a class that has two members string and int
I want to use the constructor to initialize both of these two members to use them.
#pragma once
#include <string>
#include <iostream>
using namespace std;
class donation_1
{
public:
//string name;
const char* name;
int donation_amount;
const static size_t string_size = sizeof(string);
const static size_t int_size = sizeof(int);
donation_1(char* name_1 = "Noname", int amount = 0) : name(name_1), donation_amount(amount) {};
};
int main()
{
fstream file;
file.open("donation_total1.txt", ios_base::app);
if (file.is_open())
{
donation_1("xxxx", 20).writedata(file);
donation_1("yyyy", 30).writedata(file);
donation_1("zzzz", 40).writedata(file);
donation_1("MMMM", 50).writedata(file);
donation_1("BBBB", 60).writedata(file);
file.close();
}
else
{
cout << "file couldn't be opened" << endl;
}
return 0;
}
I want to use the constructor to initialize the class variables which I will be using to update a file, however, what I am getting is this error. this error is regarding initializing the string class member.
Severity Code Description Project File Line Suppression State
Error (active) E0310 default argument of type "const char *" is incompatible with parameter of type "char *" Stream_File_Lab D:\INVSPRIVATE\C++\Projects\Stream_File_Lab\donation_1.h 17
The error message is makes it pretty clear. The variable 'name' is declared as const char* but the value being assigned to it is only char* i.e. the const-ness is missing, hence the type incompatibility error throws up.
Please, google for pointer to a const value and how to use them.
Maybe check this tutorial

Erase a specific character from a given string C++11

I would try to remove a specific character from a given string in the following code.
int main(void){
string query="a*de*da";
string org;
uint8_t rmc='*';
std::vector<string::const_iterator> wpos;
for(string::const_iterator itr = org.begin();
itr!=org.end();
++itr){
if(*itr==rmc){
wpos.push_back(itr);
}
}
uint64_t wcnt=0;
for(auto witr: wpos){
org.erase( witr-(wcnt++) );
}
query=org;
return 0;
}
In this code, I would expect that query="adeda" however, I got an error
error: no matching function for call to ‘std::basic_string<char>::erase(__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >)’
org.erase(witr-wcnt);
My experimental setting is g++ 4.9.2 of devtoolset-3 on CentOS6.7
From C++98 to C++11, the signature of std::string::erase changed from
iterator erase(iterator p)
to
iterator erase(const_iterator p)
It seems like g++4.9.2 still uses the old version. Your example should compile if you change string::const_iterator to string::iterator.

Boost Serialization MSVC 2015 do not compile in DEBUG mode

By some reason MSVC DO NOT compile boost serialization example with the following code:
class MyName
{
public:
MyName(std::string _name, std::string _family_name)
:name{ _name }, family_name{ _family_name }
{ }
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{ ar & name; ar & family_name; } std::string name; std::string family_name;
};
int main()
{
// create and open a character archive for output
std::stringstream ofs;
// save data to archive
{
MyName my_name("MyName", "FamilyName");
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << my_name;
// archive and stream closed when destructors are called
}
// save data to archive
{
MyName my_name("afsf", "dgsass");
boost::archive::text_iarchive oa(ofs);
// write class instance to archive
oa >> my_name;
// archive and stream closed when destructors are called
}
return 0;
}
I get the follwing error:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall boost::archive::archive_exception::archive_exception(enum boost::archive::archive_exception::exception_code,char const *,char const *)" (??0archive_exception#archive#boost##QAE#W4exception_code#012#PBD1#Z) referenced in function "protected: void __thiscall boost::archive::basic_text_iprimitive<class std::basic_istream<char,struct std::char_traits<char> > >::load<unsigned int>(unsigned int &)" (??$load#I#?$basic_text_iprimitive#V?$basic_istream#DU?$char_traits#D#std###std###archive#boost##IAEXAAI#Z) cpp11_cpp14_cpp17 D:\Projects_Programing\__Testing\cpp11_cpp14_cpp17\cpp11_cpp14_cpp17\cpp11_cpp14_cpp17.obj 1
But when I compiled it in release mode.
I have read that it could happen due to MSVC STRICT mode, but I have tried and it does not work neither.
Have anybody got such error ?
I have figured out the reason of this error.
It happens when I tried to compile with flag /Za (means zero extension from MSVC for C++).
When I removed this flag my code compiles succesfully.
#lakeweb Thank you for your help and support !!
Unfortunately I do not understand why some extensions from MSVC does allow to compile Boost, but without extentions it does not compile. It is very strange !!
Maybe it is either the bug on Boost side or on MSVC side.
Any assumption ?

Why is this C++ program giving compile-time error eventhough forward declaration is supplied?

Code:
#include<iostream>
using namespace std;
class MyClassOne;
class MyClassTwo
{
int myInteger;
float myFloat;
public:
void SetData(int myIntegerParameter, float myFloatParameter)
{
myInteger = myIntegerParameter;
myFloat = myFloatParameter;
}
void Show(MyClassOne myObjectParameter)
{
cout<<"MyClassOne..."<<"\n";
cout<<myObjectParameter.myInteger<<"\n";
cout<<myObjectParameter.myFloat<<"\n";
cout<<"MyClassTwo..."<<"\n";
cout<<myInteger<<"\n";
cout<<myFloat<<"\n";
}
};
class MyClassOne
{
int myInteger;
float myFloat;
public:
void SetData(int myIntegerParameter, float myFloatParameter)
{
myInteger = myIntegerParameter;
myFloat = myFloatParameter;
}
friend void MyClassTwo :: Show(MyClassOne);
};
int main()
{
MyClassOne myObjectOne;
myObjectOne.SetData(10, 10.5);
MyClassTwo myObjectTwo;
myObjectTwo.SetData(20, 20.5);
myObjectTwo.Show(myObjectOne);
return 0;
}
Error Message:
1>friend_function.cpp(22) : error C2027: use of undefined type 'MyClassOne'
1>friend_function.cpp(6) : see declaration of 'MyClassOne'
1>friend_function.cpp(22) : error C2228: left of '.myInteger' must have
class/struct/union
1>friend_function.cpp(23) : error C2027: use of undefined type 'MyClassOne'
1>friend_function.cpp(6) : see declaration of 'MyClassOne'
1>friend_function.cpp(23) : error C2228: left of '.myFloat' must have
class/struct/union
1>Generating Code...
1>Build log was saved at "file://Debug\BuildLog.htm"
1>Problem_05___Friend_Function - 4 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
MyClassTwo::Show expects a complete definition of MyClassOne.
Move the body of the Show method to after the definition of MyClassOne and it will work.
Because you can't use a forward declaration except for providing pointers to objects.
You can do the following instead:
//MyClassTwo.h
//declaration in header
void Show(MyClassOne* myObjectParameter);
and move the implementation to the cpp file:
//MyClassTwo.cpp
void MyClassTwo::Show(MyClassOne* myObjectParameter);
{
cout<<"MyClassOne..."<<"\n";
cout<<myObjectParameter->myInteger<<"\n";
cout<<myObjectParameter->myFloat<<"\n";
cout<<"MyClassTwo..."<<"\n";
cout<<myInteger<<"\n";
cout<<myFloat<<"\n";
}
The forward declaration allows you to use pointer as they are all the same size. Using an actual instance would mean you'd have to know the size of the object since it will be pushed on the method's argument stack. But you don't know the size since its declaration wasn't encountered yet. Changing the order of declaration will give an error when declaring the friend.
Separation of implementation and declaration is the way to go.
To access members of a class you need its definition, not just its declaration.
Specifically, you need to have the MyClassOne definition above the MyClassTwo definition, if you expect MyClassTwo to access members of MyClassOne.
Also, it would be a good idea to pass MyClassOne as a reference to const to the MyClassTwo::Show() member function. Therefore, you should redefine it as:
void Show(const MyClassOne &myObjectParameter)

Access violation exception while using getenv to retrieve an environment variable that doesn't exist

I am using MS Visual Studio 2008 for developing a C++ application. I use the 'getenv()' function to fetch an environment variable, but when the searched environment variable doesn't exist, it throws an access violation exception. What is the issue here and how to correct it?
The docs say that the getenv() function will return a NULL pointer if the searched environment variable doesn't exist, but why am I getting this access violation exception?
The std::string class calls strlen when you use std::string(str), which will produce an access violation when passed a NULL string. What you need to do is something like:
std::string env(const char *name)
{
const char *ret = getenv(name);
if (!ret) return std::string();
return std::string(ret);
}
or
bool getenv(const char *name, std::string &env)
{
const char *ret = getenv(name);
if (ret) env = std::string(ret);
return !!ret;
}
which you could use like this:
std::string myenv;
if (getenv("MYENV", myenv))
doSomethingWithMyEnv(myenv);

Resources