I am in the middle (towards the end) of my second C++ class at college. My question is somewhat easy. Basically, I have class called class Stock_Type
defined in one header and I have class template called template<class T> class Stock_List_Type
that has a derived class from this template called class Stock_Data : public Stock_List_Type<Stock_Type>
. Nothing is defined inside this derived class, as when I try to compile the main()
just to see that the headers are fine to compile, i get the following:
errors
In file included from stock.h:4:0,
from sandbox.cpp:1:
stock_list_type.h:53:42: error: ‘Stock_Type’ was not declared in this scope
class Stock_Data: public Stock_List_Type<Stock_Type>
^~~~~~~~~~
stock_list_type.h:53:42: note: suggested alternative: ‘Stock_Data’
class Stock_Data: public Stock_List_Type<Stock_Type>
^~~~~~~~~~
Stock_Data
stock_list_type.h:53:52: error: template argument 1 is invalid
class Stock_Data: public Stock_List_Type<Stock_Type>
^
Here is the totality of my code thusfar
stock.h
#ifndef STOCK_H
#define STOCK_H
#include"stock_list_type.h"
#include<iostream>
class Stock_Type
{
std::string stock_symbol;//record data1
double opening_price, closing_price, high_price, low_price, prev_close;//record data2
int volume;//record data3
double percent_gain;
public:
//constructor overloads-----------------------------------------------------------------------------------------------------
Stock_Type();
Stock_Type(std::string sym, double a, double b, double c, double d, double e, int f, double g) :
stock_symbol(sym), opening_price(a), closing_price(b), high_price(c), low_price(d), prev_close(e), volume(f), percent_gain(g) {}
//default destructor--------------------------------------------------------------------------------------------------------
~Stock_Type();
//accessor functions--------------------------------------------------------------------------------------------------------
void set_Symbol(std::string x){stock_symbol = x;}
void set_Closing_Price(double x){closing_price = x;}
void set_High_Price(double x){high_price = x;}
void set_Low_Price(double x){low_price = x;}
void set_Prev_Close(double x){prev_close = x;}
void set_Volume(int x){volume = x;}
std::string get_Stock_Smybol(){return stock_symbol;}
double get_Opening_Price(){return opening_price;}
double get_Closing_Price(){return closing_price;}
double get_High_Price(){return high_price;}
double get_Low_Price(){return low_price;}
double get_Prev_Close(){return prev_close;}
int get_Volume(){return volume;}
double get_Percent_Gain_Loss(){return get_Closing_Price() - get_Opening_Price();}
//operations on Stock_Type-------------------------------------------------------------------------------------------------------
//operator functions--------------------------------------------------------------------------------------------------------------
};
#endif
stock_list_type.h
#ifndef STOCK_LIST_TYPE_H
#define STOCK_LIST_TYPE_H
#include"stock.h"
template<class T>
class Stock_List_Type
{
//copy constructor
Stock_List_Type(const Stock_List_Type& object_reference)
{
p_Size = object_reference.get_p_Size();
p_Type = new T[p_Size];
for(int i = 0; i < p_Size; i++)
{
p_Type[i] = object_reference[i];
}
}
//destructor
~Stock_List_Type(){delete [] p_Type;}
//accessors
void set_p_Size(const int x){p_Size = x;}
int get_p_Size() const {return p_Size;}
//operator overloads
Stock_List_Type& operator=(const Stock_List_Type& right_operand)
{
if(this == &right_operand){return *this;}
delete [] p_Type;
p_Size = right_operand.get_p_Size();
p_Type = new T[p_Size];
return *this;
}
Stock_List_Type& operator[](int elem){return p_Type[elem];}
//variables declared as public, as it is a more natural representation
//how arrays work
T *p_Type;
int p_Size;
};
class Stock_Data: public Stock_List_Type<Stock_Type>
{
//in this derived class, operations for management of stock file data shall be defined
//data extractors
};
#endif
What I would like to do is use class Stock_List_Type
to create a list of class Stock_Type
objects and use class Stock_Data
in conjunction with with class Stock_List_Type
to manipulate data in/out of a text file that hold the relevant stock information to be stored in class Stock_Type
and then display it all nicely...and stuff. The assignment requires that there be a class template for a "generic" list and derive a specialized class to handle the listing methods of Stock_Type
.
Thank you so much, and I apologize for the long post! Thanks coders!
PS: I am on Ubuntu 17.1 and use g++