0

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++

SSBASE
  • 35
  • 1
  • 8
  • 2
    With all those getters and setters, you might just as well make the thing a struct and save yourself a lot of work. –  Apr 07 '18 at 16:40
  • In stock.h you include the list header. Which does not include stock.h anymore due to the ifdef. Hence it does not know what stock_type is. Just define and write `class stock_type;` on top of stock_list_type.h. this way stock_data knows it exists but does not need to know what it is. – kvantour Apr 07 '18 at 16:47
  • Or just remove the `include "stock_list_type.h"` from the stock.h if you don't need it. – kvantour Apr 07 '18 at 16:50
  • @NeilButterworth good eye, I think it can work, but I am not sure if that's what my professor wants. Though, objects are instances of structs or classes, so thank you. – SSBASE Apr 07 '18 at 16:52
  • @SSBASE Just FYI -- Your `Stock_List_Type` assignment operator has a flaw. If `new` throws an exception, you've corrupted the object due to you deleting the data prematurely. You can simply use the copy/swap idiom to do the copy instead of duplicating code that is already in the copy constructor. – PaulMcKenzie Apr 07 '18 at 16:54
  • Another thing -- you can't use `Stock_List_Type::operator[ ]` in a function that takes a `const Stock_List_Type`. The bracket operator should have two overloads, one for non-const and another for `const` usage. – PaulMcKenzie Apr 07 '18 at 16:58
  • @kvantour thank for this, it was the extraneous `include"stock_list_type.h"` that was causing this problem. Before embarking on my bachelor's, my only experience was with JS and Python, but C++ has taught me not just programming, but computer science as well. – SSBASE Apr 08 '18 at 12:12

0 Answers0