4

I'm a student and I'm doing a static library for arrays in C++, so I don't have to rewrite code every time during lessons.

I'm at second year in a secondary school so I'm not an expert. I want my code to be compatible with all type (int, float, ecc.) but I'm having some trouble.

Can you give a look at my code?

// slarray.h
#if !defined _SLARRAY_
#define _SLARRAY_

template <typename Tipo> class Array {
  public:
    void inserisci();
    void visualizza();
    void copia(Tipo*);
    Array(short);
    ~Array();
  private:
    Tipo* ary;
    short* siz;
};

#endif

// slarray.cpp   
#include <iostream>
#include "slarray.h"

unsigned short i;
unsigned short j;

template <typename Tipo> void Array<Tipo>::inserisci() {
  for (i = 0; i < *siz; i++) {
    std::cout << i << ": ";
    std::cin  >> ary[i];
  }
}
template <typename Tipo> void Array<Tipo>::visualizza() {
  for (i = 0; i < *siz; i++) {
    std::cout << ary[i] << " ";
  }
}
template <typename Tipo> void Array<Tipo>::copia(Tipo* arycpy) {
  for (i = 0; i < *siz; i++) {
    *(arycpy + i) = ary[i];
  }
}
template <typename Tipo> Array<Tipo>::Array(short n) {
  siz = new short;
  *siz = n;
  ary = new Tipo[n];
}
template <typename Tipo> Array<Tipo>::~Array() {
  delete[] ary;
  delete siz;
}

The code gives me errors when I try to inizialize the class with:

Array <int> vct(5);
Drise
  • 4,310
  • 5
  • 41
  • 66
LppEdd
  • 20,274
  • 11
  • 84
  • 139
  • You do know about [`std::array<>`](http://en.cppreference.com/w/cpp/container/array) and [`boost::array<>`](http://www.boost.org/libs/array/), right? And [`std::vector<>`](http://en.cppreference.com/w/cpp/container/vector)? – ildjarn May 25 '12 at 18:22
  • Also, [`std::vector<>`](http://en.cppreference.com/w/cpp/container/vector). – Robᵩ May 25 '12 at 18:22
  • The method definitions should be in the header file. – user258808 May 25 '12 at 18:24
  • 1
    You might get betters answers at our sister site, http://codereview.stackexchange.com/. – Robᵩ May 25 '12 at 18:24
  • *Aside*: You have violated the [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – Robᵩ May 25 '12 at 18:24

1 Answers1

2

Template implementations need to be visible to translation units that specialize them.

Move the implementations to the header file from the cpp.

A few other notes:

  • unsigned short i;unsigned short j; should be made local, there's no need to have them as global variables.

  • Macros starting with _ followed by a capital letter are reserved, so _SLARRAY_ is illegal, rename it.

  • Implement an assignment operator and copy constructor, otherwise all copying will be shallow.

I'm assuming you can't use std, otherwise you are aware that containers already exist there, right?

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Now it works, thanks a lot! But, should writing implementation in header file be incorrect? And what should I write in the .cpp file? – LppEdd May 25 '12 at 18:32
  • @Edoardo yes, that's how templates work. There usually isn't a corresponding `cpp` file, just the header. – Luchian Grigore May 25 '12 at 18:35
  • In this way, however, Visual studio doesn't build the .lib file. – LppEdd May 25 '12 at 18:41
  • @Edoardo even if it doesn't, a library isn't needed. No symbols need be exported, because the implementations are all visible. It's just how templates work. – Luchian Grigore May 25 '12 at 18:52
  • Found the way. I added at the bottom of the .cpp file with implementation the instantiations I need: template Array ; – LppEdd May 25 '12 at 18:53
  • @Edoardo that's a corner case. You can define implementations for specializations of the template in a cpp file. But in order to define new specializations - the implementations need to be visible. – Luchian Grigore May 25 '12 at 18:57
  • Template are harder then I was thinking. Thanks you very much for the help. – LppEdd May 25 '12 at 19:01