-3

(*i have to use my array struct, and it has to be dynamical)

I want the Array struct to be filled with Expe class objects. I`m using Templates but somehow my struct header doesn't recognize the template I have created.

Struct header:

template <class T>;
struct Arr{
    int days;
    T * M;
};
typedef Arr* Array;

Struct cpp:

void constr(Array &o){
    //Construct of 31*1 Matrix
    o=new Arr;
    o->days = 31;
    o->M = new T[o->days];

It should be fine I think, but I get error:

..\ListStruc.cpp:26:13: error: expected type-specifier before 'T'
MSalters
  • 173,980
  • 10
  • 155
  • 350
Bogdan Maier
  • 663
  • 2
  • 13
  • 19
  • Where is the other 90% of the question that includes the code? – Jon Apr 12 '12 at 13:14
  • added, i pressed enter by mistake... – Bogdan Maier Apr 12 '12 at 13:16
  • I have no idea if these are related to the problem (I can't make up heads or tails of the question) but I'm sure they'll be helpful: [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021), [What is the Rule of Three?](http://stackoverflow.com/questions/4172722), and [Why does the use of `new` cause memory leaks in C++?](http://stackoverflow.com/questions/8839943/why-does-the-use-of-new-cause-memory-leaks-in-c/8840302) – R. Martinho Fernandes Apr 12 '12 at 13:19

2 Answers2

1

You have a semicolon between template<class T> and struct Arr, which doesn't belong there. So change it to

template <class T> struct Arr{ int days; T * M; };

Furthermore Arr is a template and therefore you can't typedef Arr* to Array, only Arr<someConcreteType>*.

Last template functions must be implemented in the header and, as mentioned you typedef doesn't work so put constr in a header and change it to:

template<class T> void constr(Arr<T>*& o){
  o=new Arr<T>;
  o->days = 31;
  o->M = new T[o->days];

There might be other problems, but that is what I can see from the question.

Of course there are problems with your code apart from the compilation (violation of the rule of three and lack of exception safety).

Community
  • 1
  • 1
Grizzly
  • 19,595
  • 4
  • 60
  • 78
  • I think there's a clear concrete class here, `Expe` so you'd be able to write `typedef Arr* Array`. This will allow you to write `const(Array& o) { ... }` in the .cpp file (i.e. non-templated), but of course you would then need `o->M = new Expe[o->days];` – MSalters Oct 01 '12 at 14:57
0

You have to remove the semicolon in the structure declaration. Also, when you create an object of this type, you have to specify the template class name. And you cannot put a pointer into a reference. In fact you can't put anything in a reference, they are constants. Use a pointer instead of reference as the constr function parameter.

  • references are most definetly not constants. The template is not instantiated, but other then that putting a pointer in a reference works fine, so `Arr*&` is perfectly valid code and can be used as the OP wants to. The only reason to use a pointer (to a pointer) at this point would be to make it clearer that it is changed, but that is purely a question of preference instead of necessarity. – Grizzly Apr 12 '12 at 13:33