1

I've been trying to create a copy constructor that will work with my program, however, when I run my code it always gives me an error of:

1.) "member 'lengthOfArray' was not initialized in this constructor"
2.) "member 'lengthOfArray' was not initialized in this constructor"

Now I understand what is going on with the above two errors, but the two errors I don't understand are these two:

3.) previous definition is here
4.) redefinition of 'copy'

Here's what I currently have:

simpleVector(const simpleVector& copy) {
        simpleVector cy;
        simpleVector copy(cy);
    }

Now the instructions for what I'm trying to implement are:

your own copy constructor that performs a deep copy, i.e., create a dynamic array and copy elements from the other array that was passed as an argument to the copy constructor.

I've never created a copy constructor before nor have they covered it in class so I'm not to sure exactly how to implement one, but I've searched many sources with not much luck. Is it typical to use a for loop within a copy constructor? Any help with what I'm doing wrong would be appreciated. My entire code:

#include <iostream>
using namespace std;

// simpleVector template
template<class Temp>

class simpleVector {

// private members
private:
    Temp* tempPointer;
    int lengthOfArray;

public:

    // default no-arg constructor
    simpleVector() {
        tempPointer = NULL;
        lengthOfArray = 0;
    }

    // single argument constructor
    simpleVector(int dynamicArray) {
        lengthOfArray = dynamicArray;
        tempPointer = new Temp[lengthOfArray];
    }

    // Copy constructor
    simpleVector(const simpleVector& copy) {
        simpleVector cy;
        simpleVector copy(cy);
    }

};
mur7ay
  • 803
  • 3
  • 16
  • 44
  • how come you use templates but you still didn't learn to write copy constructor? seems like you jumped to advanced stuff before teh basics.. – David Haim Nov 12 '15 at 15:46
  • *I've never created a copy constructor before nor have they covered it in class so I'm not to sure exactly how to implement one* -- What kind of C++ class is this, one that doesn't make you aware of the proper ways to write one and reasons for a copy constructor? Also, you should write an assignment operator, not just a copy constructor for your class. I also don't get why you're using templates and not know what a copy constructor is. Might as well just forget about it and use `std::vector`, as that does everything your class is attempting to do. – PaulMcKenzie Nov 12 '15 at 15:50
  • @PaulMcKenzie a C++ class that produce "C++ is hard and un-productive language" developers. – David Haim Nov 12 '15 at 15:52
  • Write a copy method (`void copy( const simpleVector& source )` ) and call it in copy constructor and assignment operator. – zdf Nov 12 '15 at 15:52
  • @ZDF why? Why not use the copy-and-swap idiom by taking by-value in the assignment operator? – TartanLlama Nov 12 '15 at 15:53
  • Yeah, its the fifth week now and I haven't seen copy anywhere in the lecture slides. – mur7ay Nov 12 '15 at 15:55
  • @mur7ay All you need to do is to do as stated -- make a copy of the passed in vector. You do that by allocating memory for the data, and copying the contents of the passed in object's vector to the new memory, and set the length member variable in the new object. Done. My advice is to remove that `copy` "trick" you're doing now, as it is probably confusing you -- just do things as I described. – PaulMcKenzie Nov 12 '15 at 15:58
  • 1
    @mur7ay Slides? Surely you're using more literature than just slides. – molbdnilo Nov 12 '15 at 15:58
  • @TartanLlama He is already having enough troubles. – zdf Nov 12 '15 at 16:01
  • @ZDF and implementing a separate copy function rather than just writing a copy constructor will give him another. – TartanLlama Nov 12 '15 at 16:01
  • @molbdnilo I'd prefer using a book, but they said one wasn't required. So I've been using veriest C++ websites in addition to the slides. – mur7ay Nov 12 '15 at 16:04
  • 2
    @mur7ay *but they said one wasn't required* -- The C++ language is not a good language to learn by using slides. Get yourself a *good* book. Maybe other languages are ok to be "learning by slides", but not so with C++. – PaulMcKenzie Nov 12 '15 at 16:06
  • 2
    @mur7ay Ouch. I would call such a claim irresponsible and unfair to the students. See if you can find a book from [this list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). (Website tutorials aren't exactly a good learning source either.) – molbdnilo Nov 12 '15 at 16:08
  • @PaulMcKenzie I'm definitely going to pick one up. Thanks everyone! – mur7ay Nov 12 '15 at 16:09

1 Answers1

2
simpleVector(const simpleVector& copy) {  // copy decleared here
    simpleVector cy;
    simpleVector copy(cy);                // Being defined here again.
}

That's what the compiler is complaining about.

You need something like:

simpleVector(const simpleVector& copy) : lengthOfArray(copy.lengthOfArray),
                                         tempPointer(new int[copy.lengthOfArray])
{
  // Add code to copy the data from copy.tempPointer to this->tempPointer.
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270