0

Image.h

#include <iostream>
#include "Array.h"

using namespace math;

namespace imaging
{
    class Image: public Array
    {
        public:
            Image();
    };
}

Array.h

namespace math
{

    template <typename T>
    class Array
    {
        protected:
            T * buffer;
            unsigned int width, height;
        public:
            Array(unsigned int w, unsigned int h);
    };
}

and Array.cpp

#include <iostream>
using namespace std;

namespace math
{
    Array::Array(unsigned int w, unsigned int h)
    {
        this->width = w;
        this->height = h;
    }
}

I have these errors: Image.h:12:2: error: expected class-name before { token

{

^ In file included from Image.cpp:1:0: Image.h:12:2: error: expected class-name before { token

{

^ Array.cpp:8:2: error: invalid use of template-name math::Array without an argument list Array::Array(unsigned int w, unsigned int h)

Any help for this??? Thnx

chris
  • 15
  • 2
  • 1
    You'll have to template-ize `class Image` . `template class Image: public Array { }` – P0W Dec 19 '16 at 12:20
  • Small comment about names: array is known as the basic 1D collection of entries in C and C++. in C++ there is also `std::array`, so naming an 2D-array `Array` maybe okay in the problem domain, but is confusing in C++. – stefaanv Dec 19 '16 at 12:36

2 Answers2

2

You need to put you definition of the Array constructor in the header, with the correct syntax for a template:

namespace math
{
    template<typename T>
    Array::Array(unsigned int w, unsigned int h)
    {
        this->width = w;
        this->height = h;
    }
}

An important rule of thumb is that you should not cannot define functions for template classes in source files. (You can in certain circumstances but do regard that as the exception rather than the rule.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

Without the <>, Array is assumed as a non-template class which is not the case here, thus resulting into the error.

You have a problem here:

namespace imaging
{
    class Image: public Array

it should be

namespace imaging
{
   template <typename T>
   class Image: public Array<T>
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79