【问题标题】:Template Nested Class with Inheritance File Separation [closed]具有继承文件分离的模板嵌套类 [关闭]
【发布时间】:2017-12-13 21:51:46
【问题描述】:

我只在一个文件中编写了一个程序。 但是现在,我想将头文件和 cpp 文件分开。 但是在我的程序中有模板类,继承和嵌套类。所以我很困惑。

我有 4 个类:BaseAbstract、Derived1、Nested、Derived2。

我认为有 4*2+1=9 文件,但我不确定。

你可以看到我的代码。(我简化了它以方便你的回答)

 #include <iostream>

    using namespace std;



    template <class T>
    class BaseAbstract{
    public:
        class NestedClass;
        virtual int count (const T& val)=0;
    };


    template <class T>
    class Derived1:public BaseAbstract<T>{

    protected:
        shared_ptr<T>dataS;
        int sizeS;
        int capacity;

    public:

        Derived1(){
            sizeS=0;
            capacity=0;
        }

        class NestedClass{
        protected:
            T* data;
        public:
            NestedClass(){
                data=new T;
            }
            T* getData(){
                return data;
            }
            NestedClass& operator++(){
                data++;
                return *this;
            }

        };

        int count (const T& val){
            for(int i=0;i<sizeS;i++){
                if(dataS.get()[i]==val){
                    return 1;
                }
            }
            return 0;
        }

    };


    template <class K,class V>
    class Derived2:public Derived1<pair<K, V> >{
    public:
        Derived2():Derived1<pair<K, V> >(){

            this->capacity=10000;

            this->dataS=dataS1;
        }


    };

    int main(void){

        Derived1<int> a;

    }

【问题讨论】:

  • 通常你只需要2个文件头和实现。如果包含 main,则只需要 2 或 3 个
  • @JakeFreeman 但我想为所有类创建 .cpp 和标题。
  • 在这种情况下,您需要 6 个文件,因为您的嵌套类通常位于它所嵌套的类中。 7 如果你包括主要。也是 4*2 + 1 = 9 而不是 10
  • @JakeFreeman 是的,它必须是 9 。我写错了。能分开吗?对于此代码。
  • 好的,我现在就这样做

标签: c++ file templates inheritance nested


【解决方案1】:

您需要将代码放入这些文件中。类BaseAbstract 进入BaseAbstract.h。类Derived1的实现包括Nested类到Dervived1.cppDerived1.h中的定义。类Derived2 定义应该在Derived2.h 中,实现在Derived2.cpp 中。我稍后会发布分离的代码。

在文件BaseAbstract.h 标准C++ 包含和命名空间。

template <class T>
class BaseAbstract{
public:
    class NestedClass;
    virtual int count (const T& val)=0;
};

在文件Derived1.h 中包含BaseAbstract.h

template <class T>
class Derived1:public BaseAbstract<T>{

protected:
    shared_ptr<T>dataS;
    int sizeS;
    int capacity;

public:

    Derived1();

    class NestedClass;

    int count (const T& val);

};

在文件Derived1.cpp 中包含Derived1.h

Derived1::Derived1()
{
    sizeS = 0;
    capacity = 0;
}

在文件 Nested.h 中包含 Derived1.h

    Derived1::NestedClass
    {
    protected:
        T* data;
    public:
        NestedClass();

        T* getData();
        NestedClass& operator++();

    };

在文件 Nested.cpp 中包含 Nested.h

template<class T>
Derived1::NestedClass::NestedClass() { data = new T; }

template<class T>
T* Derived1::NestedClass::getData() { return data; }

template<class T>
NestedClass& Derived1::NestedClass::operator++()
{
    data++;
    return data;
}

在文件Derived2.h 中包含Derived1.h

template <class K,class V>
class Derived2:public Derived1<pair<K, V> >{
public:
    Derived2():Derived1<pair<K, V> >();


};

在文件Derived2.cpp 中包含 Derived2.h

    Derived2():Derived1<pair<K, V> >()
    {

        this->capacity=10000;

        this->dataS=dataS1;
    }

在文件main.cpp 中包含Derived2.h

int main(void){

    Derived1<int> a;

}

【讨论】:

  • 哪些文件必须包含在顶部?
  • @EasterGamer 好的,我补充了
  • 非常感谢。我认为在 main.cpp 中必须包含 .cpp 文件,因为 tamplates。你为什么不这样做?
  • 因为它在定义文件中。如果这回答了您的问题,请投票并接受
  • 能否将 NestedClass 写入另一个文件(NestedClass.cpp 和 NestedClass.h)?如果有时间?
猜你喜欢
  • 1970-01-01
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 2013-06-12
  • 2017-07-07
  • 1970-01-01
  • 2017-05-15
相关资源
最近更新 更多