Say I have a struct like the following:
struct employee
{
std::string name;
int age;
};
With boost fusion I can decorate the struct:
BOOST_FUSION_ADAPT_ASSOC_STRUCT(
demo::employee,
(std::string, name)
(int, age)
)
And access its members like this:
employee e;
boost::fusion::at_c<0, employee>(e)
Is there any way, at runtime, to get from a string "name" to the struct field
name?
The scenario of the code will be like this:
employee e; // or some other struct determined at runtime!
string attribute;
cin >> attribute;
// ApplyVisitor would inspect the correct field from the struct and act on it.
cout << ApplyVisitor(e, attribute);
Related
I have the following constructor in a class Student that is a subclass of a base class Person:
namespace Uni
{
Uni::Student::Student(string majorCourse, int enrollNumber , string name, int age, bool isStudying)
: Uni::Person::Person(std::__1::string name, int age, bool isStudying), majorCourse_(majorCourse), enrollNumber_(enrollNumber)
{
cout << "[Temp] Student Default Constructor" << endl;
}
...
}
I would like to set the attributes name_, age_ and isStudying_ (which are attributes of the Person class) to the values of name, age, and isStudying, but I get an error on this statement:
Uni::Person::Person(std::__1::string name, int age, bool isStudying)
type name is not allowed
How can I fix this?
You should just be forwarding the arguments along to the base class, so lose the typenames
Uni::Student::Student(string majorCourse, int enrollNumber , string name, int age, bool isStudying)
: Uni::Person::Person(name, age, isStudying),
majorCourse_(majorCourse),
enrollNumber_(enrollNumber)
{ }
vector<string> hj{ "jack" };
vector<double> x{ 8 };
NamePairs pair1(hj,x);
This is the only way the code runs. Is there a way to pass the values directly to pair1 object instance
You are passing the wrong types to your constructor:
"Jack" is of type const char[5] and the second argument 28.2 is of type double
Your constructor though is expecting a std::vector<string> and a std::vector<double>. So the problem is your constructor is expecting a "list" of strings and doubles, what you are not giving him.
Considering the name of your class the correct solution should be:
class NamePairs
{
private:
double age;
std::string name;
public:
NamePairs(const std::string& name, double Age)
{
this->name = name;
this->age = age;
}
};
Now you can instantiate it like that:
NamePairs pair("Alfred", 43.4);
Also consider using a std::pair
Considering you want a list of Persons that each have a Name and Age there a some different Solutions:
You could create a class Person that has two attributes:
class Person
{
private:
double Age;
std::string Name;
public:
Person(double age, const std::string& name)
{
Age = age;
Name = name;
}
};
And use it like that:
std::vector<Person> persons;
persons.push_back(Person(23.4, "Alfons");
Or you also could (more like your try) create a class PersonList like this:
class PersonList
{
private:
std::vector<double> Ages;
std::vector<std::string> names;
public:
void addPerson(double Age, const std::string& Name)
{
Ages.push_back(Age);
Names.push_back(Names);
}
};
And use it like that:
PersonList list;
list.addPerson(23.5, "Andrea");
I would greatly prefer the first Approach because it is way easier to handle the Persons if they're are not in the list(e.g. returned by a accessor or passed around to other functions/objects or if you Need to operate upon them). And it Looks way cleaner to me
I have a list of objects derived from a class named "Campus" which contains two strings, one int and two lists : one for "Students", the other is for "Teachers", before closing the program, I want to save the campus objects and of course the "Student" and "Teachers" objects contained on their lists, I want to serialize those data in an XML or JSON format or even anything else then store the result in a file.
Can somebody give me the fastest way to do the serialization with a library (that is not heavy as boost) in XML or JSON or another solution. When it comes to deal with JSON or XML serialization, I don't know what to do !
EDIT: is this feasible with RapidJSON ?
class Campus
{
private:
std::string city;
std::string region;
int capacity;
std::list<Student> students;
std::list<Teacher> teachers;
}
class Student
{
private:
int ID;
std::string name;
std::string surname;
}
class Teacher
{
protected:
int ID;
std::string name;
std::string surname;
};
You can use this C++ serialization library : Pakal persist
#include "XmlWriter.h"
class Campus
{
private:
std::string city;
std::string region;
int capacity;
std::list<Student> students;
std::list<Teacher> teachers;
public:
void persist(Archive* archive)
{
archive->value("city",city);
archive->value("region",region);
archive->value("capacity",capacity);
archive->value("Students","Student",students);
archive->value("Teachers","Teacher",teachers);
}
}
class Student
{
private:
int ID;
std::string name;
std::string surname;
public:
void persist(Archive* archive)
{
archive->value("ID",ID);
archive->value("surname",surname);
archive->value("name",name);
}
}
class Teacher
{
protected:
int ID;
std::string name;
std::string surname;
public:
void persist(Archive* archive)
{
archive->value("ID",ID);
archive->value("surname",surname);
archive->value("name",name);
}
};
Campus c;
XmlWriter writer;
writer.write("campus.xml","Campus",c);
Unfortunately C++ doesn't support reflection, so it can't automagically figure out the parameter names.. but check out this answer which looks like it'll be close to what you want: https://stackoverflow.com/a/19974486/1715829
Is this kind of implementation of nested linked list valid in c++?
if yes,then how to declare a head to those nested linked lists?
what is the syntax for accessing the data in those lists inside?
here is a part of my general code.
I'm trying to perform a library management system in c++.
struct course
{
string course_name;
struct course_books
{
struct wait_list
{
long stu_num;
//date inserted
wait_list * next_wait_stu;
};
struct voters_list
{
long stu_num;
int vote;
voters_list * next_voter;
};
struct deposit_list
{
long stu_num;
//date given
//bring back date
deposit_list * next_depositor;
};
};
course * next_course;
};
struct demand
{
int ISBN;
string book_name,
course,
author,
edition;
int demands_num;
struct demanding_students
{
string demander_name;
int demander_stu_number;
//demand date
demanding_students * next_demanding_stu;
};
demand * next_demand;
};
struct STUDENT_INFO
{
struct all_deposited_books
{
int ISBN;
//date given
//bring back date
all_deposited_books * next_dep_book;
};
struct today_deposited
{
int ISBN;
today_deposited * next_today_dep_book;
};
};
You would probably want to use classes for this instead. Something like:
class STUDENT_INFO{
protected:
struct all_deposited_books
{
int ISBN;
//date given
//bring back date
};
struct today_deposited
{
int ISBN;
};
public:
all_deposited_books * next_dep_book;
today_deposited * next_today_dep_book;
};
Then you create a class instance like: STUDENT_INFO theBestStudentInfo; and if you want to accses a struct variable inside that you just call: theBestStudentInfo.all_deposited_books[0].ISBN = 5; or something like that.
Aswell as I dont think you can create a struct instance inside the struct itself, like:
struct all_deposited_books
{
int ISBN;
//date given
//bring back date
all_deposited_books * next_dep_book; //<- this is probably gonna cause problems
};
first create the struct and how its going to look like, and then you can start creating how many instances of it as you want :)
I have struct
struct Course{
int cId;
string courseName;
};
and I want to add students for each Course. I thought to define another struct in to struct Course like
struct Course{
int cId;
string courseName;
struct Student{
int sId;
string studentName;
};
};
if I define struct like this how could I use it ? I have Course * cptr = new Course[1];
which for using the struct course.
How could I add Students to specified cId's ?
Your Course does not contain a Student. It just defines a struct of that name, i.e. a type Course::Student. For a type to contain an instance of another type, you just have to declare a member variable:
struct Student { .... };
struct Course
{
....
Student student;
};
If you want each course to hold more than one Student, then you can use a container. In the absence of more information, the best candidate for that is std::vector:
struct Course
{
....
std::vector<Student> students;
};
struct Course
{
// ...
struct Student
{
int sId;
string studentName;
};
};
This defines a struct Course and another struct Course::Student, but Course does not instantiate any student (there is no member of Course which has a type Student). If you want students to be a member of course, you need something like this:
struct Course
{
// ...
struct Student
{
int sId;
string studentName;
} student;
};
or
struct Course
{
// ...
struct Student
{
int sId;
string studentName;
} students[10];
};
Which would define a single student or an array of 10 students as members of Course, respectively. (NOTE: std::vector would be a better choice than a statically sized array)
Alternatively, you can declare Student as a struct outside of Course:
struct Student { ... };
struct Course
{
// ...
Student student;
// or std::vector<Student> students;
// or std::array<Student, 10> students;
// or Student* students
};
A very simple way:
struct Student
{
int sId;
string studentName;
};
struct Course
{
int cId;
string courseName;
std::vector <Student> students;
};
This way you may write
Student s;
Course c;
c.students.push_back(s);
std::vector is your friend, but if you are not allowed to use that, you can do:
struct Student {...}; // Implementation omitted for brevity...
struct Course
{
Student *students_; // Use students_=new Student[num_students]; to allocate
// memory dynamically
};
I prefer you to divide this struct into two individual struct. Then you can initialize a student object in the Course struct. Or u can implement a function in Course struct to link with Student struct.