问题模板类和继承

时间:2018-04-07 16:38:05

标签: c++ oop templates inheritance generic-programming

我在大学的第二个C ++课程的中间(到最后)。我的问题有点容易。基本上,我在一个标题中定义了一个名为HKLM\System\CurrentControlSet\Services\NPF\TimestampMode的类,我有一个名为class Stock_Type的类模板,它有一个名为template<class T> class Stock_List_Type的模板的派生类。在这个派生类中没有定义任何内容,因为当我尝试编译class Stock_Data : public Stock_List_Type<Stock_Type>只是为了看到标题可以编译时,我得到以下内容:

错误

main()

以下是我的代码的全部内容

stock.h

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

stock_list_type.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

我想要做的是使用#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 创建class Stock_List_Type个对象列表,并使用class Stock_Typeclass Stock_Data一起操作数据进/出一个文本文件,其中包含要存储在class Stock_List_Type中的相关股票信息,然后很好地显示它......以及其他内容。赋值要求“通用”列表有一个类模板,并派生一个专门的类来处理class Stock_Type的列表方法。

非常感谢,我为这篇长篇帖子道歉!感谢编码员!

PS:我使用的是Ubuntu 17.1并使用g ++

0 个答案:

没有答案