This project i am working on uses pointers, inheritance and internal aggregation (rule of 3). I understand the concept of rule of 3 and have read a few posts here about it. The basic thing that needs to be implemented is to have a room class where each room has several meetings (Internal Aggregation). Room has the following attributes:
string d_name
int d_nMeetings
Meeting** d_schedule.
I know that the rule of three needs to be implemented in Meeting** d_schedule and d_schedule would be an array of meeting pointers which calls an array of pointers which points to meetings.
ButI'm not too sure however on how it would work in this case (in d_schedule or in the function below (in meeting.h) which reads void add(Meeting*). The following is my code:
Meeting.h:
#include <vector>
#include <string>
#include "item.h"
class Meeting: public Item{
protected:
bool d_coffee;
std::vector<std::string> d_participants;
public:
Meeting(std::string, Time, int, bool, std::vector<std::string>, int);
void print();
};
Meeting.cpp:
#include "meeting.h"
#include <iostream>
#include <string>
#include <vector>
#include "item.h"
Meeting::Meeting(std::string _what, Time _deadline, int _duration, bool _coffee, std::vector<std::string> _participants, int _priority = 0) : Item(_what, _deadline, _duration, priority = 0)
{
d_coffee = _coffee;
d_participants = _participants;
}
void Meeting::print()
{
Item::print();
}
Room.h:
#include "meeting.h"
class Room{
private:
std::string d_name;
Meeting** d_schedule;
int d_nMeetings;
public:
Room(std::string _name);
void setName(std::string);
std::string getName();
bool add(Meeting*);
void print();
};
Thanks
Why not just use std::vector<Meeting*> d_schedule; instead of reimplementing the same functionality.
If you still want to use Meeting** d_schedule;, checkout how vector data structure works:
http://codefreakr.com/how-is-c-stl-implemented-internally/
Basically you'll have to allocate some fixed array and reallocate and copy over it every time it fills up.
Related
I am really stuck with the problem here.
So basically I have a Stock class and a BuyOrder class.The BuyOrder class takes the Stock class as a member so it knows which stock to buy. When the stock is created, it takes in a company name and initializes with random prices. Then I can create a buy order takes in bidPrice,bidQuantity,Stock arguments.
In the main I want to create an array of BuyOrder to store the orders. Then every time the user created an order, the order can be stored.
My first approach is to declare BuyOrder buyList[100]. This way every time the user created an order, I can use the set functions to set each buyList[i] and then increment i. However, when I declare BuyOrder buyList[100]in main. It says No matching constructor for initialization of 'BuyStock buyList[100]' I went back to BuyOrder.cpp try to add a default constructor BuyOrder::BuyOrder(){} then it shows the error:Constructor for 'BuyOder'must explicitly initialize the reference member 'buyStock'.
I don't know how to proceed from here. Very much appreciated.
Below is part of BuyOrder.cpp since it way too long and the rest part are just function definitions.
BuyOrder::BuyOrder{double price, int quantity, Stock &s)
:buyPrice{price},
buyQuantity{quantity},
buyStock{s}{}
void BuyOrder::setBuyStock(Stock stock){
buyStock = stock;
}
void BuyOrder::setBuyOrderPrice(double price) {
buyPrice = price;
}
void BuyOrder::setBuyOrderQuantity(int quantity) {
buyQuantity = quantity;
Below is BuyOrder.h
#ifndef BUYORDER_H
#define BUYORDER_H
#include <ctime>
#include<iostream>
#include "Stock.h"
class BuyOrder {
friend void getCurrentTime();
public:
BuyOrder(double , int , Stock & );
void setBuyStock(Stock);
void setBuyOrderPrice(double price);
void setBuyOrderQuantity(int quantity);
double getBuyOrderPrice();
int getBuyOrderQuantity();
void placeBuyOrder();
void checkExcute();
void modifyBuyOrder();
void cancelBuyOrder();
double getHighestBidPrice();
int getHighestBidPriceQuantity();
double getLowestBidPrice();
int getLowestBidPriceQuantity();
private:
double buyPrice;
int buyQuantity;
bool excute = false;
Stock &buyStock;
time_t t;
};
#endif
This is the Stock.h
#ifndef STOCK_H
#define STOCK_H
#include <vector>
#include <string>
using namespace std;
class Stock {
friend void getCurrentTime();
public:
Stock();
Stock(string nameOfCompany);
int getBidTerms();
int getAskTerms();
void printStockInfo();
void initialize();
void sortBid();
void sortAsk();
string nameOfCompany;
string currentTime;
vector<double> askPrice;
vector<int> askQuantity;
vector<double> bidPrice;
vector<int> bidQuantity;
int bidTerm;
int askTerm;
};
#endif
and this is Stock.cpp excluding the long function definitions
#include "Stock.h"
#include <iostream>
#include <vector>
#include <time.h>
#include <iomanip>
#include <ctime>
#include <random>
#pragma warning(disable : 4996)
using namespace std;
void getCurrentTime() {
time_t rawTime;
struct tm * timeinfo;
time(&rawTime);
timeinfo = localtime(&rawTime);
cout << asctime(timeinfo);
}
Stock::Stock(){}
Stock::Stock(string companyName) :nameOfCompany{ companyName } {
initialize();
sortBid();
sortAsk();
}
Because your BuyOrder class (which you don't show us, and should) contains a reference member (Stock &buyStock;), you have to set that reference to something within any constructor for the class. This means that you can't normally use a default constructor, as that does not initialize the reference.
Possible solutions include changing buyStock to not be a reference, changing it to be a pointer (which can be nulled out for those cases where it doesn't represent an order yet), or changing containers from a fixed size array to something that can be resized, like a vector.
I am currently trying to create a list that will initialize 100 item objects but for some reason my global variables and arrays are not being recognized by the compiler. Any input would be greatly appreciated. Also if you see anything that I am doing wrong, criticism is highly appreciated.
Header for my List:
#ifndef List_hpp
#define List_hpp
#include <stdio.h>
#include <string>
#include <iostream>
#include "Item.hpp"
using namespace std;
class List
{
private:
static int itemcount;
Item items[100];
public :
List(){
this->itemcount = 0;
};
void addItem(string, string, int, double);
void removeItem(string);
void display();
}; //End List Class
#endif /* List_hpp */
CPP file:
#include "List.hpp"
//This method will add a new item
void addItem(string name, string unit, int quantity, double price){
Item newItem(name, unit, quantity, price);
itemcount++;
}
void removeItem(string name){
}
void display(){
}
In the CPP File of the class List you must put the class name before every function implementation like:
void List::addItem...
void List::removeItem...
void List::display..
I think the implementation of addItem is not completed yet. You didn't put the new created item in the list.
You must declare a copy constructor for the class Item or use dynamic allocation.
I am trying to move from using structs to using classes, and I have a few questions with my - not fully complete, but enough to illustrate my queries - code (thank you in advance for clarifying these):
I am having problems with creating a constructor that takes in arguments, specifically the line in the header file that I have currently left as neighborAtt(int neighbor_id, int att_1, int att_2);.
When using neighborAtt as a struct, I could do it easily as neighborAttributes currentNode(neighborID, att1, att2);. What is the class-equivalent?
In the .cpp file, I know that I need to define the constructor as neighborAtt::neighborAtt().
Do I need to this with the functions (i.e. include neighborAtt::) or is what I've done accurate?
This is my header file:
#if !def connectivity_H
#define connectivity_H
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <sstream>
#include <fstream>
class listAtt;
class vecListAtt;
class neighborAtt //contains the neighbour and associated attributes of a node
{
public:
neighborAtt();
neighborAtt(int neighbor_id, int att_1, int att_2);
vecListAtt connFrFile(int file_ext);
vecListAtt makeList(std::vector<std::list<neighborAtt>> nodeAndInfo, int nodeID, neighborAtt neighAndAtt);
neighborAtt getAtt(std::string currentLine);
private:
int neighborID;
int attribute1;
int attribute2;
};
typedef std::list<neighborAtt> listAtt;
typedef std::vector<listAtt> vecListAtt;
#endif
and the .cpp file:
#include "stdafx.h"
#include "connectivity.h"
neighborAtt::neighborAtt(): neighborID(0), attribute1(0), attribute2(0) {}
//neighborAtt::neighborAtt constructor with arguments
vecListAtt connFrFile(int file_ext)
{
//code
}
neighborAtt getAtt(std::string line)
{
//code
}
For the second constructor (one with the arguments) you do just the same as for one without them. Or did I get the question wrong? It'd be like:
neighborAtt::neighborAtt(int neighbor_id, int att_1, int att_2)
: neighborID(neighbor_id),
attribute1(att_1),
attribute2(att_2)
{
}
And for the methods you must go the same way:
vecListAtt neighborAtt::connFrFile(int file_ext)
{
//code
}
error C2071: 'Lexicon::list' : illegal storage class
I have a class that reads a bunch of strings into memory and then provides functions that allow applying operations on those strings and their relationships. As part of this I'd like to have a shared memory between the main.cpp where some of the operations are initiated and the class where the operations are completed. For this, in a previous post, it was suggested to use an extern type. But, now there is an error. How do I resolve this error and have a memory space shared by several classes?
in lexicon.h
#ifndef _lexicon_h
#define _lexicon_h
#include <string>
#include <vector>
using namespace std;
class Lexicon {
public:
Lexicon();
~Lexicon();
extern vector<vector<string>> list;
void buildVectorFromFile(string filename, vector<vector<string>> &list, int v, int h);
private:
struct charT { char letter; nodeT *next;};
};
#endif
in main.cpp
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include "lexicon.h"
void buildVectorFromFileHelper (Lexicon & lex)
{
vector<vector<string>> list;
lex.buildVectorFromFile("ASCII.csv", list, 200, 2); //build 2x200 vector list
}
Ok, I missunderstood your previous question (this is what happens when you don't post full code). Inside a class, extern is not used:
in lexicon.h
#ifndef _lexicon_h
#define _lexicon_h
#include <string>
#include <vector>
using namespace std;
class Lexicon {
public:
Lexicon();
~Lexicon();
vector<vector<string>> list;
private:
struct charT { char letter; nodeT *next;};
};
#endif
in main.cpp
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include "lexicon.h"
void buildVectorFromFileHelper (Lexicon & lex)
{
vector<vector<string>> list;
lex.buildVectorFromFile("ASCII.csv", list, 200, 2); //build 2x200 vector list
}
The problem here is that Lexicon doesn't have the method buildVectorFromFile, so how are you calling lex.buildVectorFromFile("ASCII.csv", list, 200, 2);?
To share the same vector, if it's a member, make it static:
class Lexicon {
public:
Lexicon();
~Lexicon();
static vector<vector<string>> list;
private:
struct charT { char letter; nodeT *next;};
};
In lexicon.cpp:
vector<vector<string>> Lexicon::list;
The rules of an extern memory is explained here in this daniweb thread; the comment there is that yes, this should be simple but it is somehow not intuitive. The gist is that the memory is globally declared with the extern prefix in .cpp file A and then to reuse the memory in cpp B, globally declare it again in .cpp file B.
I think Luchian_Grigore and #jahhaj were getting there but we had either just not found the words for me to understand or they were still finding the words to explain.
I've spent quite a few hours researching and trying to figure out why I'm getting this error. Basically the three files that have to do with the inheriting are CollegeMember.h, Employee.h, and EmpAcademicRecord.h. Employee. is inheriting from CollegeMember.h and EmpAcademicRecord.h is inheriting from Employee.h. Like this CollegeMember <- Employee <- EmpAcademicRecord. The error occurs in EmpAcademicRecord.h. Heres the three files.
CollegeMember.h
#include <cstdlib>
#include <iostream>
#include<ctype.h>
#include<string.h>
#include "Employee.h"
#include "Student.h"
using namespace std;
// ****************************************************************************
// Class Definitions follow
typedef char* String;
// The CollegeMember class
class CollegeMember
{
protected:
int ID_Number;
string FirstName, LastName;
string AddressLine1, AddressLine2, StateProv, Zip;
string Telephone;
string E_Mail;
string answer, answer2, answer3, answer4;//used as sort of booleans for use with validation
// member functions
public:
CollegeMember ( ); // constructor
CollegeMember(const CollegeMember&); //overloaded constructor
void Modify (CollegeMember Member);
void InputData(int x);
string Summary ( ); //summary
string PrintMe(); //fully describes
}; // End of CollegeMember class declaration
Employee.h
#include <cstdlib>
#include <iostream>
#include<ctype.h>
#include<string.h>
#include "EmpAcademicRecord.h"
#include "EmpEmploymentHistory.h"
#include "EmpExtraCurricular.h"
#include "EmpPersonalInfo.h"
#include "EmpPublicationLog.h"
using namespace std;
// ****************************************************************************
// Class Definitions follow
typedef char* String;
// The Employee Class
class Employee: protected CollegeMember
{
float Salary;
protected:
string Department, JobTitle;
// Member Functions
public:
Employee ( ); // constructor
void Modify (Employee ThisEmp);
void InputData(int x);
void SetSalary (float Sal) // Specified as an in-line function
{ Salary = Sal;}
float GetSalary ( ) {return Salary;} // Specified as an in-line function
string Summary ( ); //summary
string PrintMe(); //fully describes
}; // End of Employee class declaration
EmpAcademicRecord.h
#include <iostream>
#include <cstdlib>
#include<ctype.h>
#include<string.h>
using namespace std;
typedef char* String;
class EmpAcademicRecord: protected Employee{ //error occurs on this line
protected:
int ReferenceNumber;
string Institution;
string Award;
string start;
string end;
public:
EmpAcademicRecord();
void InputData (int x);
void Modify(EmpAcademicRecord ThisRec);
void Summary();
};
Any help with this would be greatly appreciated.
That sort of error is usually caused by the type not being defined when you try to use it.
In this case, it appears that you may have included EmpAcademicRecord.h without having first included Employee.h (the includes at the top of the former do not show the latter).
In other words, at the point where the compiler sees:
class EmpAcademicRecord: protected Employee { //error occurs on this line
it has no idea what the Employee class is.
It may be a simple matter of adding:
#include "Employee.h"
to the top of that file, it's a little difficult to be certain since we don't have the code files. In any case, it's certainly a good first step.
Since you have EmpAcademicRecord.h being included by Employee.h, that will probably result in an infinite recursion.
You could fix that with include guards, but I can't see why you need that particulat inclusion. EmpAcademicRecord depends on Employee, not the other way around.