模板派生类中的变量无法识别。 C ++

时间:2018-06-21 13:03:32

标签: c++

看来,我很难将模板派生类转换为在基类上找到的变量。

我得到的错误是:

Error   C2065   'a': undeclared identifier  
Error   C2065   'b': undeclared identifier

此处是代码:

--- >> base.h

#ifndef _BASE_H
#define _BASE_H

template <class T>
class base
{
    T a, b;
public:
    T getMull1();
    base(T q,T W);
    ~base();
};

#include "base.cpp"

#endif

->> base.cpp

#include "stdafx.h"
#include "base.h"

#ifndef BASE_IMPL__
#define BASE_IMPL__


template<class T>
T base<T>::getMull1()
{
    return a * b;
}

template<class T>
base<T>::base(T q, T W)
{
    this->a = q;
    this->b = W;
}

template <class T>
base<T>::~base()
{
}

#endif

->> derive1.h

#ifndef _DERIVE1_H
#define _DERIVE1_H

#include "base.h"
template <class T>
class derive1 :
    public base<T>
{
public:
    T getMull();
    derive1(T q, T W);
    ~derive1();
};

#include "derive1.cpp"
#endif

->> derive1.cpp

#include "stdafx.h"
#include "derive1.h"


#ifndef DERIVE1_IMPL__
#define DERIVE1_IMPL__

template<class T>
T derive1<T>::getMull() 
{
    return a*b;
}

template<class T>
derive1<T>::derive1(T q, T W):base<T>(q,W)
{
}

template<class T>
derive1<T>::~derive1()
{
}


#endif

->>主要

#include "stdafx.h"
#include "derive1.h"

derive1<int> myDerive(1,4);
int main()
{
    int ans = myDerive.getMull1(); //working fine on the base
        ans = myDerive.getMull();   // not working 
    return 0;
}

请帮助我解决这个问题。

0 个答案:

没有答案