0

it seem , i have a problem to get from template derive class into variables which found on base class.

the error i get is :

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

here the code:

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

-->>main

#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;
}

please help me to solve this issue.

Joseph D.
  • 11,804
  • 3
  • 34
  • 67
NoamY
  • 1
  • 1
  • 4
    You should see [why-can-templates-only-be-implemented-in-the-header-file](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Jarod42 Jun 21 '18 at 13:05
  • dependent name, you have to use `return this->a * this->b;` or `return base::a * base::b;`. – Jarod42 Jun 21 '18 at 13:05
  • thanks u , its working. – NoamY Jun 21 '18 at 15:02

0 Answers0