Class declaration and definitions - c++

Here is my Class code:
class LibItem
{
public:
virtual void PrintDetails() = 0;
void setDetails(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus)
{
Title = setItemTitle;
Author = setItemAuthor;
ReleaseDate = setItemReleaseDate;
Copyright = setItemCopyright;
Genre = setItemGenre;
Status = setItemStatus;
}
void setTitle(string TitleName)
{
Title = TitleName;
}
string getTitle()
{
return Title;
}
void setReleaseDate(string date)
{
ReleaseDate = date;
}
string getReleaseDate()
{
return ReleaseDate;
}
void setAuthor(string AuthorName)
{
Author = AuthorName;
}
string getAuthor()
{
return Author;
}
void setCopyright(string CopyrightDetails)
{
Copyright = CopyrightDetails;
}
string getCopyright()
{
return Copyright;
}
void setGenre(string GenreDetails)
{
Genre = GenreDetails;
}
string getGenre()
{
return Genre;
}
void setStatus(string StatusDetails)
{
Status = StatusDetails;
}
string getStatus()
{
return Status;
}
private:
string Title;
string ReleaseDate;
string Author;
string Copyright;
string Genre;
string Status;
};
I am wanting to place this into a .h file and a .cpp file.
Is the below code correct?
LibItem.cpp:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "LibItem.h"
virtual void LibItem::PrintDetails() = 0;
void LibItem::setDetails(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus)
{
Title = setItemTitle;
Author = setItemAuthor;
ReleaseDate = setItemReleaseDate;
Copyright = setItemCopyright;
Genre = setItemGenre;
Status = setItemStatus;
}
void LibItem::setTitle(string TitleName)
{
Title = TitleName;
}
string LibItem::getTitle()
{
return Title;
}
void LibItem::setReleaseDate(string date)
{
ReleaseDate = date;
}
string LibItem::getReleaseDate()
{
return ReleaseDate;
}
void LibItem::setAuthor(string AuthorName)
{
Author = AuthorName;
}
string LibItem::getAuthor()
{
return Author;
}
void LibItem::setCopyright(string CopyrightDetails)
{
Copyright = CopyrightDetails;
}
string LibItem::getCopyright()
{
return Copyright;
}
void LibItem::setGenre(string GenreDetails)
{
Genre = GenreDetails;
}
string LibItem::getGenre()
{
return Genre;
}
void LibItem::setStatus(string StatusDetails)
{
Status = StatusDetails;
}
string LibItem::getStatus()
{
return Status;
}
};
//---------------------------------------------------------------------------
#pragma package(smart_init)
LibItem.h
//---------------------------------------------------------------------------
#ifndef LibItemH
#define LibItemH
class LibItem
{
public:
virtual void PrintDetails();
void setDetails(string, string, string, string, string, string);
void setTitle(string);
void setReleaseDate(string);
string getReleaseDate();
void setAuthor(string);
string getAuthor();
void setCopyright(string);
string getCopyright();
void setGenre(string);
string getGenre();
void setStatus(string);
string getStatus();
private:
string Title;
string ReleaseDate;
string Author;
string Copyright;
string Genre;
string Status;
};
//---------------------------------------------------------------------------
#endif
Next, if I am wanting to use this .h and .cpp file in a main function, what is the code needed to be able to do this? What are the include statements needed?

Not correct:
virtual void LibItem::PrintDetails() = 0;
The =0 should be inside the class definition (in the header).
To use the class, you need to #include "LibItem.h".
Also, in the header:
#include <string>
and replace occurences of string with std::string.
string parameters should be passed by reference:
void setReleaseDate(const string& date)
instead of
void setReleaseDate(string date)

Related

Class does not persist vector of objects after addition

My code is as shown below. The problem is inside the int main() function, r.printItems() is not printing anything. What am I missing here?
main.cpp
#include <bits/stdc++.h>
#include "Customer.h"
#include "MenuCreator.h"
#include "FoodItem.h"
MenuCreator m1;
void createCustomer() {
Customer c1("mrg", "m#gmail.com", "9654357", "+91");
}
void createRestaurantItem(Restaurant &rest) {
rest.addItems(FoodItem("t1"));
rest.addItems(FoodItem("D1"));
}
void createMenu() {
m1.createMenu("sg");
Category c1;
c1.setName("Non-veg");
m1.addCategory(c1);
Restaurant r1;
r1.setName("ABC");
r1.setDescription("Test Restaurant");
createRestaurantItem(r1);
c1.addRestaurants(r1);
}
vector<Restaurant> getRestaurantsForCategory(string category) {
return m1.getRestaurantsForCategories(category);
}
int main() {
createCustomer();
createMenu();
for (auto r: getRestaurantsForCategory("Non-veg")) {
r.printItems();
}
return 0;
}
MenuCreator.h
#include <bits/stdc++.h>
#include "Menu.h"
using namespace std;
class MenuCreator {
public:
void createMenu(string name) {
Menu m1;
m1.setName(name);
menu = m1;
}
void addCategory(const Category &categ) {
categories.push_back(categ);
}
const Menu &getMenu() const {
return menu;
}
const vector<Category> &getCategories() const {
return categories;
}
void addRestaurantForCategory(string name, const Restaurant restaurant) {
for(auto categ: categories) {
if (categ.getName() == name) {
categ.addRestaurants(restaurant);
}
}
}
const vector<Restaurant> &getRestaurantsForCategories(string category) {
for(auto categ: categories) {
if(categ.getName() == category) return categ.getRestaurants();
}
}
private:
Menu menu;
vector<Category> categories;
};
Menu.h
#include<bits/stdc++.h>
#include "Category.h"
using namespace std;
class Menu {
public:
const string &getName() const {
return name;
}
void setName(const string &name) {
Menu::name = name;
}
private:
string name;
string description;
vector<Category> categories;
};
Category.h
using namespace std;
class Category {
public:
const string &getName() const {
return name;
}
void setName(const string &name) {
Category::name = name;
}
const string &getDescription() const {
return description;
}
void setDescription(const string &description) {
Category::description = description;
}
const vector<Restaurant> &getRestaurants() const {
return restaurants;
}
void setRestaurants(const vector<Restaurant> &restaurants) {
Category::restaurants = restaurants;
}
void addRestaurants(const Restaurant &rt) {
Category::restaurants.push_back(rt);
}
private:
string name;
string description;
vector<Restaurant> restaurants;
};
FoodItem.h
#include <bits/stdc++.h>
#include <vector>
#include "FoodItem.h"
using namespace std;
class Restaurant {
public:
Restaurant() {
this->id = gen_random(12);
}
virtual ~Restaurant() {
}
const string &getName() const {
return name;
}
void setName(const string &name) {
Restaurant::name = name;
}
const string &getDescription() const {
return description;
}
void setDescription(const string &description) {
Restaurant::description = description;
}
double getLat() const {
return lat;
}
void setLat(double lat) {
Restaurant::lat = lat;
}
double getLang() const {
return lang;
}
void setLang(double lang) {
Restaurant::lang = lang;
}
const string &getImageUrl() const {
return imageUrl;
}
void setImageUrl(const string &imageUrl) {
Restaurant::imageUrl = imageUrl;
}
const string &getVideoUrl() const {
return videoUrl;
}
void setVideoUrl(const string &videoUrl) {
Restaurant::videoUrl = videoUrl;
}
const vector<FoodItem> &getItems() const {
return items;
}
void setItems(const vector<FoodItem> &items) {
Restaurant::items = items;
}
void addItems(const FoodItem &item) {
this->items.push_back(item);
}
string gen_random(const int len) {
string tmp_s;
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
srand( (unsigned) time(NULL) * getpid());
tmp_s.reserve(len);
for (int i = 0; i < len; ++i)
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
return tmp_s;
}
const string &getId() const {
return id;
}
void printItems() {
for(auto it: items) {
cout<<"item: "<<it.getName()<<endl;
}
}
private:
string id;
string name;
string description;
double lat;
double lang;
string imageUrl;
string videoUrl;
string createdAt;
string updatedAt;
vector<FoodItem> items;
};
Restaurant.h
#include <bits/stdc++.h>
#include <vector>
#include "FoodItem.h"
using namespace std;
class Restaurant {
public:
Restaurant() {
this->id = gen_random(12);
}
virtual ~Restaurant() {
}
const string &getName() const {
return name;
}
void setName(const string &name) {
Restaurant::name = name;
}
const string &getDescription() const {
return description;
}
void setDescription(const string &description) {
Restaurant::description = description;
}
double getLat() const {
return lat;
}
void setLat(double lat) {
Restaurant::lat = lat;
}
double getLang() const {
return lang;
}
void setLang(double lang) {
Restaurant::lang = lang;
}
const string &getImageUrl() const {
return imageUrl;
}
void setImageUrl(const string &imageUrl) {
Restaurant::imageUrl = imageUrl;
}
const string &getVideoUrl() const {
return videoUrl;
}
void setVideoUrl(const string &videoUrl) {
Restaurant::videoUrl = videoUrl;
}
const vector<FoodItem> &getItems() const {
return items;
}
void setItems(const vector<FoodItem> &items) {
Restaurant::items = items;
}
void addItems(const FoodItem &item) {
this->items.push_back(item);
}
string gen_random(const int len) {
string tmp_s;
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
srand( (unsigned) time(NULL) * getpid());
tmp_s.reserve(len);
for (int i = 0; i < len; ++i)
tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
return tmp_s;
}
const string &getId() const {
return id;
}
void printItems() {
for(auto it: items) {
cout<<"item: "<<it.getName()<<endl;
}
}
private:
string id;
string name;
string description;
double lat;
double lang;
string imageUrl;
string videoUrl;
string createdAt;
string updatedAt;
vector<FoodItem> items;
};
When createMenu() in main.cpp is setting up the menu, it creates a local Category object named c1 and adds it to the menu, which makes a copy of c1 when pushingnit into the MenuCreator::categories vector. No Restaurant objects had been added to c1 yet when that copy is created. So when a Restaurant is then added to c1 afterwards, the copy is not updated. That is why there are no Restaurant objects in the menu when MenuCreator::getRestaurantsForCategory() tries to return them.
Change createMenu() to fully initialize c1 before adding it to the menu, eg:
void createMenu() {
m1.createMenu("sg");
Category c1;
c1.setName("Non-veg");
Restaurant r1;
r1.setName("ABC");
r1.setDescription("Test Restaurant");
createRestaurantItem(r1);
c1.addRestaurants(r1);
m1.addCategory(c1); // <-- move down here
}
Also, on a side note, the return value of MenuCreator::getRestaurantsForCategories() is undefined if the specified category is not found. Since the return value is a reference to a vector, it needs to either return a static vector that is empty, or else throw an exception.

C++ Access error, pointer trouble

I'm working on saving objects to a file and reading them back out. Right now I'm trying to do them one at a time to see if I can do and avoid duplicate objects. I'm getting this error: Exception thrown at 0x00915B18 in who.exe: 0xC0000005: Access violation writing location 0xDDDDDDDD
I know that this is because I'm not doing something with a pointer correctly but I'm lost and can't quite figure it out, current code is:
#include<iostream>
#include "Book.h"
#include "InventoryBook.h"
#include "SoldBook.h"
#include<string>
#include<fstream>
using namespace std;
void saveBook(Book);
Book returnBook();
void main() {
Book novel;
InventoryBook shelf;
SoldBook gone;
novel.setAuthor("Joe");
novel.setISBN("1234567788");
novel.setPublisher("Me");
novel.setTitle("Joe vs. the Volcano");
saveBook(novel);
Book rBook = returnBook();
cout << rBook.getAuthor();
system("Pause");
}
void saveBook(Book saved) {
ofstream myfile;
myfile.open("bookInventory.txt", ios::app);
myfile.write((char*)&saved, sizeof(saved));
}
Book returnBook() {
ifstream myfile;
myfile.open("bookInventory.txt", ios::in);
Book novel;
Book newBook;
myfile.read((char*)&novel, sizeof(novel));
newBook.setAuthor(novel.getAuthor());
return newBook;
}
I know that the save book function works and that I run into my issues with the returned book object. I'm just not able to see where this goes wrong. I'm a little fuzzy on pointers.
#pragma once
#ifndef BOOK_H
#define BOOK_H
#include<iostream>
using namespace std;
class Book{
private:
string ISBN;
string bookTitle;
string authorName;
string publisher;
public:
Book(string, string, string, string);
Book();
//destructor
~Book();
//Mutators
void setTitle(string);
void setISBN(string);
void setAuthor(string);
void setPublisher(string);
//accessors
string getTitle();
string getISBN();
string getAuthor();
string getPublisher();
};
#endif
#include "Book.h"
Book::Book() {
ISBN = "";
bookTitle = "";
authorName = "";
publisher = "";
}
Book::Book(string newISBN, string newTitle, string newAuthor, string newPub) {
ISBN = newISBN;
bookTitle = newTitle;
authorName = newAuthor;
publisher = newPub;
}
Book::~Book()
{
}
void Book::setISBN(string newISBN) {
ISBN = newISBN;
}
string Book::getISBN() {
return ISBN;
}
void Book::setTitle(string newTitle) {
bookTitle = newTitle;
}
string Book::getTitle() {
return bookTitle;
}
void Book::setAuthor(string newAuthor) {
authorName = newAuthor;
}
string Book::getAuthor() {
return authorName;
}
void Book::setPublisher(string newPub) {
publisher = newPub;
}
string Book::getPublisher() {
return publisher;
}

I'm trying to learn how to properly separate class header and code

I have a class project that I did from college that "works", but isn't constructed properly.
It has a Date class and a Person class.
In my "working" code, all of the class data (constructors, members, and functions) are contained within a single header file for each class (a separate file for each class).
The program loads text file data (Persons, presidents and actors) into a vector, sorts, filters and prints the data on the console, and saves it to a file.
In the Person class constructor (and functions), the Date class is instantiated for a "birthdate" object. Then the birthday is used in the program.
The problem I'm having is this:
I can easily instantiate "Date" in the Person constructor and members, if all the class code is in a header file for each class, AND I make the Date the "Parent" class for Person. But if I don't do both of these things, then "Person" doesn't know anything about "Date" and cannot instantiate anything from Date.
If I use an #include "Date.h" in Person.h, this creates even more problems and doesn't work either.
Of course, "Date" is not a proper parent class for "Person", but it was the only way I could hack the code to make it work lol. When I first coded this years ago in college, I never did figure out how to divide the class code properly in so that it would compile and run. The "working code" with all the class code in a header file is my hack. I want to learn how to do it the right way.
I have pasted in the bare minimum example below of what "works" and what doesn't. Any tips to make this code work with the classes divided into header and .cpp files would be much appreciated.
What works:
class Date {
private:
int month;
int day;
int year;
public:
Date::Date() {}
virtual ~Date() {}
Date( int aMonth, int aDay, int aYear ) {
this->month = aMonth;
this->year = aYear;
this->day = aDay;
}
// "Getter" functions for the Date class
int getMonth(){return this->month;}
int getYear() {return this->year;}
int getDay() {return this->day;}
// "Setter" functions for the Date class
void setDay( int aDay ){ this->day = aDay; }
void setMonth( int aMonth ) { this->month = aMonth; }
void setYear( int aYear ) { this->year = aYear; }
};
class Person : public Date{
private:
string title;
string firstName;
string lastName;
Date birthDate;
public:
Person::Person() {}
Person(string title, string firstName, string lastName, Date birthDay);
virtual ~Person() {}
//"Getter" functions for the Person class
string getTitle() { return title; }
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
Date getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void setTitle(string Title) { this->title = Title; }
void setFirstName(string fName) { this->firstName = fName; }
void setLastName (string lName) { this->lastName = lName; }
void setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
};
What doesn't work (Doesn't compile):
Date.h
class Date {
private:
int month;
int day;
int year;
public:
Date();
virtual ~Date() {}
Date( int aMonth, int aDay, int aYear );
//Getters and setters
int getMonth();
int getYear() ;
int getDay();
void setDay( int aDay );
void setMonth( int aMonth ) ;
void setYear( int aYear ) ;
};
Date.cpp
#include "Date.h"
Date::Date() {}
Date::Date( int aMonth, int aDay, int aYear ) {
this->month = aMonth;
this->year = aYear;
this->day = aDay;
}
int Date::getMonth(){return this->month;}
int Date::getYear() {return this->year;}
int Date::getDay() {return this->day;}
//"Setter" functions for the Date class
void Date::setDay( int aDay ){ this->day = aDay; }
void Date::setMonth( int aMonth ) { this->month = aMonth; }
void Date::setYear( int aYear ) { this->year = aYear; }
Person.h
#include <string>
class Person{
private:
string title;
string firstName;
string lastName;
Date birthDate;
public:
//Person::Person() {}
Person(string title, string firstName, string lastName, Date birthDay);
//"Getter" functions for the Person class
string getTitle() { return title; }
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
Date getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void setTitle(string Title) { this->title = Title; }
void setFirstName(string fName) { this->firstName = fName; }
void setLastName (string lName) { this->lastName = lName; }
void setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
};
Person.cpp
#include "Person.h"
#include <iostream>
using namespace std;
//Person class constructor with 0 arguments
Person::Person(string atitle, string afirstName, string alastName, Date abirthDay) {
title = atitle,
firstName = afirstName,
lastName = alastName,
birthday = abirthDay)
}
//"Getter" functions for the Person class
string Person::getTitle() { return title; }
string Person::getFirstName() { return firstName; }
string Person::getLastName() { return lastName; }
Date Person::getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void Person::setTitle(string Title) { this->title = Title; }
void Person::setFirstName(string fName) { this->firstName = fName; }
void Person::setLastName (string lName) { this->lastName = lName; }
void Person::setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
Operating system notes
I'm using MS Visual Studio 2012 Update 4 on a Windows 7 PC.
To prevent redefinition errors, add include guards to header files. To be portable, use:
#if !defined(THE_HEADER_NAME_H)
#define THE_HEADER_NAME_H
// Usual code and declarations here.
#endif
In Visual Studio, you can use:
#pragma once
// Usual code and declarations here.
In headers, don't write (at global scope level)
using namespace XXXXX
because that will cause all files including yours to also use namespace XXXXX which they may not want imposed upon them. If you use types that are in a namespace, type out the entire type name with namespace, so use:
std::string getTitle()
(In a cpp file, you can have 'using namespace XXXXX' after any #includes)
You have 2 problems IMHO.
First is the guards problem that Scott Langham's answer will solve.
The second is a problem of namespaces.
I assume that in your working source, you have (directly on indirectly via stdafx.h or another include) :
#include <string>
using namespace std;
It is not recommended to have using namespace std; in a .h header file ... but that means that you should use std::string instead of string:
date.h
class Date {
private:
int month;
int day;
int year;
public:
Date::Date() { } //Date class constructor with 0 arguments
virtual ~Date() {} //Date class destructor
Date( int aMonth, int aDay, int aYear ) { //Date class constructor with 3 arguments
this->month = aMonth; // Set the private member data with the argument data
this->year = aYear;
this->day = aDay;
}
//"Getter" functions for the Date class
int getMonth(){return this->month;}
int getYear() {return this->year;}
int getDay() {return this->day;}
//"Setter" functions for the Date class
void setDay( int aDay ){ this->day = aDay; }
void setMonth( int aMonth ) { this->month = aMonth; }
void setYear( int aYear ) { this->year = aYear; }
};
person.h (string -> std::string and remove method implementations)
#include <string>
#include "Date.h"
class Person : public Date{
private: //Private data members
std::string title;
std::string firstName;
std::string lastName;
Date birthDate;
public:
Person::Person() {} //Person class constructor with 0 arguments
Person(std::string title,
std::string firstName,
std::string lastName,
Date birthDay); //Person class constructor with 4 arguments
virtual ~Person(){} //Person class destructor
//"Getter" functions for the Person class
std::string getTitle();
std::string getFirstName();
std::string getLastName();
Date getBirthDay();
//"Setter" functions for the Person class
void setTitle(std::string Title);
void setFirstName(std::string fName);
void setLastName (std::string lName);
void setBirthday (Date aBirthday);
};
person.cpp (fixed includes and constructor)
#include <string>
#include "Person.h"
#include <iostream>
using namespace std;
//Person class constructor with 0 arguments
Person::Person(string atitle, string afirstName, string alastName, Date abirthDay) {
title = atitle,
firstName = afirstName,
lastName = alastName,
birthDate = abirthDay;
}
//"Getter" functions for the Person class
string Person::getTitle() { return title; }
string Person::getFirstName() { return firstName; }
string Person::getLastName() { return lastName; }
Date Person::getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void Person::setTitle(string Title) { this->title = Title; }
void Person::setFirstName(string fName) { this->firstName = fName; }
void Person::setLastName (string lName) { this->lastName = lName; }
void Person::setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
It can even be used without guards, but guard are anyway a good practice.
In fact as Date.h is included in Person.h it should really have a guard - not shown here for simplicity

C++ undefined symbol

I am creating a C++ console application where I am saving and loading a vector to a file. The file I am saving and loading has a header that has the size of the vector.
Here is my code:
void loadFromFile()
{
ifstream iStream("file.ext", ios::binary);
fileHeader_t fHeader;
iStream.read((char*)&fHeader, sizeof(fileHeader_t));
if (fHeader.magicNumber = 0xDEADBEAF)
{
appointments.resize(fHeader.appointmentCount); iStream.read((char*)&appointments[0], fHeader.appointmentCount * sizeof(appointment));
}
}
void saveToFile()
{
ofstream oStream("file.ext", ios::binary);
fileHeader_t fHeader;
fHeader.magicNumber = 0xDEADBEAF;
fHeader.appointmentCount = appointments.size();
oStream.write((char*)&fHeader, sizeof(fileHeader_t));
oStream.write((char*)&appointments[0], sizeof(appointment) * appointments.size());
}
And here is the header struct:
struct fileHeader_s
{
DWORD magicNumber;
size_t appointmentsCount;
}fileHeader_t;
I am getting the following errors:
E2379 Statement missing ;
E2451 Undefined symbol 'fHeader'
At the following lines:
fileHeader_t fHeader;
Why is this happening, and more importantly, how can I fix it?
Thanks
Update
Here is my full code:
class appointment
{
public:
appointment(string aDate, string aTime, string aType,
string aLocation, string aComments, bool aIsImportant,
string aReminderDate, string aReminderTime)
{
appDate = aDate;
appTime = aTime;
appType = aType;
appLocation = aLocation;
appComments = aComments;
appIsImportant = aIsImportant;
appReminderDate = aReminderDate;
appReminderTime = aReminderTime;
}
void setDate(string aDate)
{
appDate = aDate;
}
void setTime(string aTime)
{
appTime = aTime;
}
void setType(string aType)
{
appType = aType;
}
void setLocation(string aLocation)
{
appLocation = aLocation;
}
void setComments(string aComments)
{
appComments = aComments;
}
void setIsImportant(bool aIsImportant)
{
appIsImportant = aIsImportant;
}
void setReminderDate(string aReminderDate)
{
appReminderDate = aReminderDate;
}
void setReminderTime(string aReminderTime)
{
appReminderTime = aReminderTime;
}
string getDate()
{
return appDate;
}
string getTime()
{
return appTime;
}
string getType()
{
return appType;
}
string getLocation()
{
return appLocation;
}
string getComments()
{
return appComments;
}
bool getIsImportant()
{
return appIsImportant;
}
string getReminderDate()
{
return appReminderDate;
}
string getReminderTime()
{
return appReminderTime;
}
private:
appointment();
string appDate;
string appTime;
string appType;
string appLocation;
string appComments;
bool appIsImportant;
string appReminderDate;
string appReminderTime;
//person owner;
};
class calendar
{
public:
calendar()
{
loadFromFile();
}
~calendar()
{
saveToFile();
}
void createAppointment(string aDate, string aTime, string aType, string aLocation, string aComments, bool aIsImportant, string aReminderDate, string aReminderTime)
{
appointment newAppointment(aDate, aTime, aType, aLocation, aComments, aIsImportant, aReminderDate, aReminderTime);
appointments.push_back(newAppointment);
}
private:
vector<appointment> appointments;
string calCurrentDate;
string calCurrentTime;
void loadFromFile()
{
ifstream iStream("file.ext", ios::binary);
fileHeader_t fHeader;
iStream.read((char*)&fHeader, sizeof(fileHeader_t));
if (fHeader.magicNumber = 0xDEADBEAF)
{
appointments.resize(fHeader.appointmentCount); iStream.read((char*)&appointments[0], fHeader.appointmentCount * sizeof(appointment));
}
}
void saveToFile()
{
ofstream oStream("file.ext", ios::binary);
fileHeader_t fHeader;
fHeader.magicNumber = 0xDEADBEAF;
fHeader.appointmentCount = appointments.size();
oStream.write((char*)&fHeader, sizeof(fileHeader_t));
oStream.write((char*)&appointments[0], sizeof(appointment) * appointments.size());
}
typedef struct fileHeader_s
{
DWORD magicNumber;
size_t appointmentCount;
}fileHeader_t;
};
I am getting the following 2 errors:
[BCC32 Warning] Person.cpp(271): W8060 Possibly incorrect assignment
Full parser context
Person.cpp(245): class calendar
Person.cpp(290): decision to instantiate: void calendar::loadFromFile()
--- Resetting parser context for instantiation...
Person.cpp(267): parsing: void calendar::loadFromFile()
[BCC32 Error] vector(608): E2247 'appointment::appointment()' is not accessible
Full parser context
vector(607): decision to instantiate: void vector >::resize(unsigned int)
--- Resetting parser context for instantiation...
Person.cpp(18): #include c:\program files (x86)\embarcadero\rad studio\9.0\include\dinkumware\vector
vector(8): namespace std
vector(330): class vector<_Ty,_Ax>
vector(607): parsing: void vector >::resize(unsigned int)
Can I please have some help to fix this?
You are missing the typedef keyword in struct definition.
typedef struct fileHeader_s
{
}fileHeader_t;
However, in C++ it is not required for the typedef keyword. In that case, struct name is still fileHeader_s.
I think what you want is a typedef
typedef struct fileHeader_s
{
DWORD magicNumber;
size_t appointmentsCount;
}fileHeader_t;

C++ saving/loading a vector to a file

I am creating a Calendar application in C++.
Here is my code:
class appointment
{
public:
appointment(string aDate, string aTime, string aType,
string aLocation, string aComments, bool aIsImportant,
string aReminderDate, string aReminderTime)
{
appDate = aDate;
appTime = aTime;
appType = aType;
appLocation = aLocation;
appComments = aComments;
appIsImportant = aIsImportant;
appReminderDate = aReminderDate;
appReminderTime = aReminderTime;
}
void setDate(string aDate)
{
appDate = aDate;
}
void setTime(string aTime)
{
appTime = aTime;
}
void setType(string aType)
{
appType = aType;
}
void setLocation(string aLocation)
{
appLocation = aLocation;
}
void setComments(string aComments)
{
appComments = aComments;
}
void setIsImportant(bool aIsImportant)
{
appIsImportant = aIsImportant;
}
void setReminderDate(string aReminderDate)
{
appReminderDate = aReminderDate;
}
void setReminderTime(string aReminderTime)
{
appReminderTime = aReminderTime;
}
string getDate()
{
return appDate;
}
string getTime()
{
return appTime;
}
string getType()
{
return appType;
}
string getLocation()
{
return appLocation;
}
string getComments()
{
return appComments;
}
bool getIsImportant()
{
return appIsImportant;
}
string getReminderDate()
{
return appReminderDate;
}
string getReminderTime()
{
return appReminderTime;
}
private:
appointment();
string appDate;
string appTime;
string appType;
string appLocation;
string appComments;
bool appIsImportant;
string appReminderDate;
string appReminderTime;
//person owner;
};
class calendar
{
public:
calendar()
{
loadFromFile();
}
~calendar()
{
saveToFile();
}
void createAppointment(string aDate, string aTime, string aType,
string aLocation, string aComments, bool aIsImportant,
string aReminderDate, string aReminderTime)
{
appointment newAppointment(string aDate, string aTime, string aType,
string aLocation, string aComments, bool aIsImportant,
string aReminderDate, string aReminderTime);
//appointments.resize(appointments.size() + 1,newAppointment);
}
private:
vector<appointment> appointments;
string calCurrentDate;
string calCurrentTime;
void loadFromFile()
{
//Code to load appointments from file
}
void saveToFile()
{
//Code to save appointments to file
}
};
Can I please have some help with the following:
When the constructor is called, I want to load a file (loadFromFile() method) of appointment objects and set the 'appointments' variable to have the contents of this file. After the saveToFile() method,I want to save the contents of the appointment vector to file.
Also, when the createAppointment() method is called, I want to increase the size of the vector by 1, and add the contents to the vector. I am not sure of the correct code.
Update Saving/Loading from files
void loadFromFile()
{
ifstream iStream("file.ext", ios::binary);
fileHeader_t fHeader;
iStream.read((char*)&fHeader, sizeof(fileHeader_t));
if (fHeader.magicNumber = 0xDEADBEAF)
{
appointments.resize(fHeader.appointmentCount); iStream.read((char*)&appointments[0], fHeader.appointmentCount * sizeof(appointment));
}
}
void saveToFile()
{
ofstream oStream("file.ext", ios::binary);
fileHeader_t fHeader;
fHeader.magicNumber = 0xDEADBEAF;
fHeader.appointmentCount = appointments.size();
oStream.write((char*)&fHeader, sizeof(fileHeader_t));
oStream.write((char*)&appointments[0], sizeof(appointment) * appointments.size());
}
Vectors are supposedly contiguous, so you shouldn't have any problem about loading them using ifstream, if I were you I'd create a basic header for your binary file with something like :
struct fileHeader_s
{
DWORD magicNumber;
size_t appointmentsCount;
}fileHeader_t;
Then, in a loop, just read each item in an appointment value, and use appointments.push_back( item );
You should do the same in createAppointment, don't resize the vector, just do push_back(newAppointment);
You're going to have to come up with your own file format (i.e. save each record on a line manually), or use something like Boost::serialization.