Array class implementation, home problems

As the name says, one part of my purpose is to implement an array class. The professor gave us a heading style to start with, creating function declarations for us. Using the previous examples, I tried my best to define these functions, but I have many problems understanding what is going on. It may be a bit to post the entire header file, but I will try to explain the problems that I have independently. Here is my code:

#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <string>
using namespace std;

template <class T>
class Array {
private:
    int size;
    T * arr;
public:
    int getSize();

    Array();
    Array(int size);
    Array(Array & other);
    Array(T[], int n);
    ~Array();
    Array & operator=(Array & rhs);
    Array & operator+(Array & rhs); // append
    T & operator[](int i); //allow read and write
    const T & operator[](int n) const; // readonly
    void print(int n = 5);
    void print(ostream & out, int n);
    operator int *() { return arr; }
    friend ostream & operator <<(ostream & out, Array <T>& rhs);
};

template <class T>
int Array<T>::getSize() {
    return size;
}

template <class T>
Array<T>::Array() {
    arr = new T[];
}

template <class T>
Array<T>::Array(int size) {
    arr = new T[size];
}

template <class T>
Array<T>::Array(Array & other) {
    size = other.getSize();
    arr = new T[size];
    copy(arr[0], arr[size + 1], other.arr[0]);
}

template <class T>
Array<T>::Array(T[], int n) {
    size = n;
    arr = new T[n];
}

template <class T>
Array<T>::~Array() {
    if (arr) {
        delete arr;
    }
}

template <class T>
Array<T>& Array<T>::operator=(Array & rhs) {
    if (this != &rhs) {
        delete[] arr;
        size = rhs.getSize();
        arr = new T[size];
        copy(arr[0], arr[size+1], rhs.arr[0]);
    }
    return *this;
}

template <class T>
Array<T>& Array<T>::operator+(Array & rhs) {
    Array *tmp;

    tmp = new Array(size + rhs.getSize());
    return *tmp;
}

template <class T>
T& Array<T>::operator[](int i) {
    assert(0 <= i && i < size);
    return arr[i];
}

template <class T>
const T& Array<T>::operator[] (int n) const {
    assert(0 <= i && i < size);
    return arr[i];
}

template <class T>
void Array<T>::print(ostream &out, int n) {
    for (size_t i = 0; i < n; i++) {
        out << arr[i] << " ";
    }
}

template <class T>
void Array<T>::print(int n = 5) {
     print(cout, n);
}

template <class T>
ostream & operator << (ostream & out, Array<T> & rhs) {
    out << "( " << rhs.getSize() << ")";
    return out;
}

#endif

I would appreciate any hints just to help me figure out what's going on in this class. I apologize if this will be very much to ask.

Edit: Thanks again for the help! Linker errors have been fixed. I should have indicated that the seal was a member of the class, not just free.

+4
1
  • Array(Array & other); 46 error C2664: 'char *strcpy(char *,const char *)' : cannot convert argument 1 from 'int *' to 'char *' strcpy, , . , - cannot convert argument 1 from 'std::string *' to 'char *'

, strcpy char . , T , strcpy , . ( ) :

template <class T>
Array<T>::Array(const Array &other) : size(other.size), arr(new T[size])
{
  for (int i = 0; i < size; ++i)
    arr[i] = other.arr[i];
}

2) strcpy 71 operator=. cannot convert argument 1 from 'std::string *' to 'char *'

, , : C-style str* raw char * , . , std::string ++ - . , std::string , .

:

template <class T>
Array<T>& Array<T>::operator = (Array rhs)
{
  std::swap(size, rhs.size);
  std::swap(arr, rhs.arr);
  return *this;
}

3) T & operator[](int i); //allow read and write 85 , i . , . , Array<T>::T & operator[] (int n) const; , , n .

operator [] Array<T> i. :

Array<int> arr(42);
std::cout << arr[0];

, - . const, non-const. , const, const Array<T>:

template <class T>
T& Array<T>::operator[] (int i) 
{
  assert(0 <= i && i < size);
  return arr[i];
}

template <class T>
const T& Array<T>::operator[] (int i) const
{
  assert(0 <= i && i < size);
  return arr[i];
}

4) void print(int n = 5);. , , , . for, .

5) , void print(ostream & out, int n);. ?

, , "". "" ++, .

, , print ( cout btw). print . :

void print(ostream &out, int n)
{
  for (size_t i = 0; i < n; ++i)
    out << pets[i] << " ";
}

void print(int n)
{
  print(cout, n);
}

. print, , , , (?) .

: print, , , , . :

  template <typename T>
  void print(const Array<T> &arr, int n)
  {
    print(arr, n, cout);
  }
+3

All Articles