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.
Related
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.
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'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...error: '_bookingVector' was not declared in this scope|
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
Is my syntax wrong? Or are vectors initialized differently?
Here is my code:
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>
using namespace std;
class Schedule
{
public:
Schedule();
void AddBooking(int bday, int btime, int btrainer, int bid);
void RemoveBooking(int bday, int btime);
void DisplaySchedule();
void DisplayAvailableTimeSlots();
//For Testing
void DisplayDebug();
private:
vector<Booking> _bookingVector;
};
#endif // SCHEDULE_H
Schedule.cpp
#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream>
Schedule::Schedule() : _bookingVector()
{ }
void AddBooking(int bday, int btime, int btrainer, int bid){
Booking bookingObject(bday, btime, btrainer, bid);
_bookingVector.push_back(bookingObject);
}
void DisplayDebug(){
for(int i = 0; i < _bookingVector.size(); ++i){
cout << _bookingVecotr[i] << endl;
}
}
I'm very eager to learn what I'm doing wrong and fix it.
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking and DisplayDebug as non-member functions, but these should be members in order to access other members of the class.
Modify the definitions to be in the scope of the Schedule class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
Also, don't say using namespace std in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1 Your default constructor does not do anything that the compiler-generated one wouldn't do. You can safely remove it.
I'm a little confused by this error that I'm having (I'm using VS2012).
Here's my code:
RecipeBook.h:
#ifndef RECIPEBOOK_H
#define RECIPEBOOK_H
#include "SingleRecipe.h"
using namespace std;
class RecipeBook
{
private:
vector<SingleRecipe> *recipe;
SingleRecipe *one;
public:
RecipeBook(vector<SingleRecipe> *recipe);
void addRecipe(SingleRecipe *one);
bool removeRecipe(string name);
vector <SingleRecipe> *returnListOfRecipes(double time);
};
#endif
SingleRecipe.h:
#ifndef SINGLERECIPE_H
#define SINGLERECIPE_H
#include <string>
#include <vector>
using namespace std;
class SingleRecipe
{
private:
string name;
vector<string> ingredients;
vector<string> method;
int numOfServing;
double time;
public:
SingleRecipe(string name, vector<string> ingredients, vector<string> method, int numOfServing, double time);
string getName();
void setName();
int getNumOfServing();
void setNumOfServing();
double getTime();
void setTime();
string toString();
};
#endif
BookAndRecipe.cpp:
#include "RecipeBook.h"
#include "SingleRecipe.h"
#include <sstream>
#include <math.h>
using namespace std;
vector <SingleRecipe> *RecipeBook::returnListOfRecipes(double time)
{
vector<SingleRecipe> *two;
for (int i = 0; i = recipe->size(); i++)
{
if (recipe[i].data()->getTime < time)
{
*two->push_back(recipe[i].pop_back());
}
}
return NULL;
}
Over at returnListOfRecipes() I get this error:
no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=SingleRecipe, _Alloc=std::allocator<SingleRecipe>]" matches the argument list
argument types are: (void)
object type is: std::vector<SingleRecipe, std::allocator<SingleRecipe>> c:\Users\Ventus\Documents\Visual Studio 2012\Projects\Recipe\Recipe\BookAndRecipe.cpp 83 8
I suspect it might have something wrong with my for loop, but I'm not very experienced, so I might be doing something very wrong here.
I appreciate all help that's given!
pop_back() doesn't return a value, it just drops the last element of the container. You probably want:
*two->push_back(recipe[i].back());
recipe[i].pop_back();
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.