0

Hello guys in my c++ program I have four classes (A,B,C,D)

  • A is the base class
  • B inherits from A
  • C inherits from A
  • D inherits from B

All of them are template classes template<class Type> and each of them has a print method that prints its private members and the private members of the class that it inherits from.

So B would print B private members and A private members, C would print C private members and A private members, D would print its private members and B,A private members.

In the main function I want to create an Array of pointers for class A that have 3 locations for an object of each class then I want to loop each object print method.

the problem is when I change the classes to a template classes I got an error message says "that my classes don't have constructors" ; however they do have them.

here is my code Please Help (Note I commented where the errors occurs for you):

#include <iostream>
#include <string>
using namespace std;

template <class Type>
class A
{
public:
virtual void print()
{
    cout<<"the base class (A) private (x) is : "<<x<<endl;
}

A(Type X = 0)
{
    x = X;
}
void setX(Type X)
{
    x = X;
}

Type getX() const
{
    return x;
}

private:
Type x;
};


template <class Type>
class B:public A
{
public:
B(Type X = 0,Type Y = 0)
{
    setX(X);
    y = Y;
}
void setY(Type Y)
{
    y = Y;
}

Type getY() const
{
    return y;
}

void print()
{
    A::print();

    cout<<"private (y) in class (B) is : "<<getY()<<endl;
}

private:
Type y;
};

template <class Type>
class C:public A
{
public:
C(Type X = 0,Type Z = 0)
{
    setX(X);
    z = Z;
}
void setZ(Type Z)
{
    z = Z;
}

Type getZ() const
{
    return z;
}

void print()
{
    A::print();

    cout<<"private (z) in class (C) is : "<<getZ()<<endl<<endl;
}

private:
Type z;
};


template <class Type>
class D:public B
{
public:
D(Type X = 0,Type Y = 0,Type W = 0)
{
    setX(X);
    setY(Y);
    w = W;
}
void setW(Type W)
{
    w = W;
}

Type getW() const
{
    return w;
}

void print()
{
    B::print();

    cout<<"private (w) in class (D) is : "<<getW()<<endl;
}

private:
Type w;
};


void main()
{
A<int>* arrayOfPointers[3];

arrayOfPointers[0] = new B(1,100);//error here
arrayOfPointers[1] = new C(2,200);//error here
arrayOfPointers[2] = new D(3,300,3000);//error here

for(int i = 0 ; i<3;i++)
{
    cout<<typeid(*arrayOfPointers[i]).name()<<" Print method : \n"<<endl;
    arrayOfPointers[i]->print();
    cout<<"**********************\n"<<endl;
}

}
  • 1
    When posting on SO, try to come up with the smallest example that still displays the error, and provide the exact error message. You probably only need A & B to produce the error here, so you can cut your code by half. – Nir Friedman Jul 22 '16 at 13:31
  • 2
    In `class B:public A` what `A` are you inheriting from? Since `A` is a template its name is not enough. – NathanOliver Jul 22 '16 at 13:31
  • 2
    I think `All of them are template classes` is symptomatic of the misunderstanding you're under. They're class templates. Meaning that if you have `template class A` then `A` is not a class by itself. Only once you specify all the template parameters does it become an actual class. So `A` for example would be a class. – PeterT Jul 22 '16 at 13:43
  • Okay thanks all and I will make sure to make my question smaller next time – Mohammad Ghazi Istanboli Jul 22 '16 at 13:53

2 Answers2

2

You forgot two things:

1) Your inheritance needs to specify template arguments for the class they are inheriting from. For example:

template <class Type>
class B : public A<Type>
{
    ...
}

2) When you are instantiating your classes, you need to provide template arguments, too:

arrayOfPointers[0] = new B<int>(1, 100);
arrayOfPointers[1] = new C<int>(2, 200);
arrayOfPointers[2] = new D<int>(3, 300, 3000);

However, you can also provide template functions to instantiate these classes from the provided arguments, like those make_(...) methods from the std library:

template <class Type>
B<Type>* create_B(const Type& t1, const Type& t2)
{
    return new B<Type>(t1, t2);
}

and use it like this:

 arrayOfPointers[0] = create_B(1, 100);

However, be aware that those methods are creating raw pointers to allocated heap memory, so you are responsible for deleting it (you might use shared_ptrs or whatever to overcome that or just return an object etc but this is not actually part of your question/my answer).

CppChris
  • 1,226
  • 9
  • 14
0

You inherit from A, but you should inherit from a specific instantiation A<T>. Maybe class B:public A<Type> ?

Dutow
  • 5,638
  • 1
  • 30
  • 40
  • 1
    as for 1) `A(Type X = 0)` is a valid substitute for a default contructor. And as for 3) he already has an array of pointers – PeterT Jul 22 '16 at 13:40
  • Right, removed the first (and I already noticed and removed the third) – Dutow Jul 22 '16 at 13:41