Jovi Zhihao Zhou Jovi Zhihao Zhou - 20 days ago 9
C++ Question

Class Composition Constructor d Must Explicitly initialize the reference member

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();
}

Answer

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.

Comments