创建了我自己的矢量类。能够超载流操作符,但不能按值c ++打印

时间:2013-07-19 23:42:01

标签: c++

我对编程很新,我们的老师让我们创建了自己的矢量类。一切都按需要工作,但是一旦我触及底部并尝试使用printV函数,函数的大小就会增加到一个非常大的数字,而不是保留向量的大小。我将包括下面的所有代码。

using namespace std;
class MyVector
{
private:
    int vectorSize;
    int maxCapacity;
    int *myArray;
public:
    //default constructor
    //purpose: Initializes all variables
    MyVector(void);
    //Parameterized Constructor
    //Purpose: creates a vector of capacity n
    //parameters: int
    MyVector(int);
    //destructor
    //Purpose: deletes and dynamically allocated storage
    ~MyVector(void);
    // Overloaded assignment operator
    // Purpose: to do assignment from one vector to another
    // Parameters: a MyVector object
    // Returns: A MyVector object
    MyVector operator + (MyVector&);
    // Copy constructor
    // Purpose: Copy the data into this vector
    // Parameters: a MyVector object
    // Returns: none
    MyVector(const MyVector&);
    //size function
    //purpose: gets the size of the vector
    //returns: size
    int size() const;
    //capacity function
    //purpose: gets the capacity of the vector
    //returns: capacity
    int capacity() const;
    //clear function
    //purpose: deletes all elements from the vector and resets its size to zero and capacity 2
    //returns: nothing
    void clear();
    //push_back function
    //purpose: adds an integer value at the end of the vector
    //returns: nothing
    void push_back(int n);
    //at function
    //purpose: returns the value of the element at position i in the vector
    int at(int) const;
};
ostream& operator<<(ostream&, const MyVector&);

这是cpp文件

#include "MyVector.h"


MyVector::MyVector(void)
{
    maxCapacity = 2;
    myArray = new int[maxCapacity];
    vectorSize = 0;
}

MyVector::MyVector(int i)
{
    maxCapacity = i;
    myArray = new int[maxCapacity];
    vectorSize = 0;
}

MyVector::~MyVector(void)
{
    if (myArray != NULL);
    {
        delete [] myArray;
        myArray = NULL;
    }
}
MyVector MyVector::operator+(MyVector& rho)
{
    if (this == &rho)
    {
        return *this;
    }
    delete [ ] this->myArray;
    vectorSize = rho.vectorSize;
    this->myArray = new int[maxCapacity];
    for(int i = 0; i < maxCapacity; i++)
    {
        this->myArray[i] = rho.myArray[i];
    }
    return *this;
}
MyVector::MyVector(const MyVector& b)
{
    maxCapacity = b.maxCapacity;
    myArray = new int[maxCapacity];
    for (int i = 0; i < maxCapacity; i++)
    {
        this->myArray[i] = b.myArray[i];
    }
}
int MyVector::size() const
{
    return vectorSize;
}
int MyVector::capacity() const
{
    return maxCapacity;
}
void MyVector::clear()
{
    delete [] myArray;
    maxCapacity = 2;
    myArray = new int[maxCapacity];
    vectorSize = 0;
}
void MyVector::push_back(int add_Element)
{
    if(vectorSize+1>maxCapacity)
    {
        maxCapacity = maxCapacity*2;
        int* tmp = new int[maxCapacity];
        for(int i=0; i <vectorSize; i++)
        {
            tmp[i] = myArray[i];
        }
        delete[] myArray;
        myArray = tmp;
    }

    myArray[vectorSize] = add_Element;
    vectorSize++;
}
int MyVector::at(int i) const
{
    if(i >= vectorSize)
    {
        throw i;
    }
    else
    {
        return myArray[i];
    }
}
ostream& operator<<(ostream& theStream, const MyVector& aVector)
{
    for (int i = 0; i < aVector.size(); i++)
    {
        theStream << aVector.at(i);
    }
    return theStream;
}

当然还有司机。我无法更改驱动程序

#include <iostream>
#include "MyVector.h"     
using namespace std;

// the printV function
// used to test the copy constructor
// parameter: a MyVector object
void printV(MyVector);

int main( )
{
    cout << "\nCreating a vector Sam of size 4.";
    MyVector sam( 4 );

    cout << "\nPush 12 values into the vector.";
    for (int i = 0; i < 12; i++)
        sam.push_back(i);

    cout << "\nHere is sam: ";
    cout << sam;
    cout << "\n---------------\n";

        cout << "\nCreating a vector Joe of size 4.";
    MyVector joe( 4 );
    cout << "\nPush 6 values into the vector.";
    for (int i = 0; i < 6; i++)
        joe.push_back(i * 3);

    cout << "\nHere is joe: ";
    cout << joe;
    cout << "\n---------------\n";

    cout << "\nTest the overloaded assignment operator \"joe = sam\": ";
    joe = sam;

    cout << "\nHere is sam: ";
    cout << sam;
    cout << "\n---------------\n";

    cout << "\nHere is joe: ";
    cout << joe;
    cout << "\n---------------\n";

    // pass a copy of sam by value
    printV(sam);//my problem is here! It changes the array size to a very large number


    cout << endl;
    system("PAUSE");
    return 0;
}

void printV(MyVector v)
{
    cout << "\n--------------------\n";
    cout << "Printing a copy of a vector\n";
    cout << v;
}

1 个答案:

答案 0 :(得分:3)

我认为你错过了

vectorSize = b.vectorSize;

复制构造函数MyVector::MyVector(const MyVector& b)

中的行