0

I have to use C++ for a project (normally doing Haskell and C) and I completely hang on headers for namespaces. When I leave away the "arrays.hpp" and directly include "arrays.cpp" into other files (which is bad practice, I know). The code works fine, but when I try to define the header for the following code it no longer works (of course, I still pass all the .cpps to g++). I already read several Stack Overflow posts on that topic, but none of the proposed solutions worked for me, so I would really appreciate it if you could tell me how the header for this code might look.

#include <vector>
#include <stdlib.h>

#include "arrays.hpp"

namespace Arrays {
  template<typename Field> class Array {
    private:
    int rank;
    int* shape;
    Field* data;
    public:
    Array(int _rank, int* _shape, Field* _data) {
      rank = _rank;
      shape = _shape;
      data = _data;
    }
    Array(std::vector<Field> vector) {
      rank = 1;
      shape = (int*)malloc(sizeof(int));
      shape[0] = (int)vector.size();
      data = (Field*)malloc(sizeof(Field)*vector.size());
      for(int i = 0; i < vector.size(); i++)
        data[i] = vector[i];
    }
  };
}

Here is what I tried so far:

#ifndef ARRAYS_HPP

#define ARRAYS_HPP

namespace Arrays {
  template<typename Field> class Array;
}

#endif

And this is G++'s response after invoking it with g++ *.cpp):

error: variable ‘Arrays::Array<int> array’ has initializer but incomplete type
ein mensch
  • 311
  • 2
  • 9

0 Answers0