重载自制Vector类的赋值运算符

时间:2017-08-06 04:08:42

标签: c++ vector operator-overloading

所以,对于一个赋值,我创建了一个模拟通常在C ++中找到的Vector质量的类,并且我试图重载赋值运算符(=),以便可以在两个“向量”之间执行深度复制,但我一直在遇到问题。我意识到我的代码有点半生不熟,但有人可以帮助我吗?

//stdafx.h
#pragma once

#include "targetver.h"
#include <iomanip>
#include <array>
#include <stdio.h>
#include <tchar.h>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

//Vector.h
class Vector
{

double* arr;  // pointer to the first element of this vector
int cap; // number of elements arr can hold (i.e. size of underlying array)
int sz;// size of this vector


       // The increase_capacity function
       // Purpose: Dincrease capacity of vector
       // Parameters: new capacity of vecoter
       // Returns: none
void increase_capacity(int new_cap)
{
    // Increases the capacity of the underlying array to be sz. If sz
    // is smaller than the current capacity then nothing is done.
    double* new_arr = new double[new_cap];   // allocate a new array

    for (int i = 0; i < cap; ++i)
    { // copy old vector into new one
        new_arr[i] = arr[i];
    }
    cap = new_cap;//set new capacity of vector

    delete[] arr;//delete old vector from memory
    arr = new_arr;//set old vector to new vector
}

public:
    // The non-parameterized constructer
    // Purpose: create an empty vector with capacity of 2 and size of 0
    // Parameters: none
    // Returns: vector
    Vector();

    // The Parameterized Constructer
    // Purpose: create an empty vector with capacity of n and size of 0
    // Parameters: int n
    // Returns: vector
    Vector(int n);

    // The size function
    // Purpose: get current size of vector
    // Parameters: 
    // Returns: vector size as an int
    int size() const; 

    // The push_back function
    // Purpose: push back values into vector
    // Parameters: int n
    // Returns: none
    void push_back(int n); 

    // The capacity function
    // Purpose: get current capacity of vector
    // Parameters: none
    // Returns: capacity of vector as a double
    double capacity();

    // The at function
    // Purpose: get value stored in vector at index n
    // Parameters: int n
    // Returns: value stored at index n as a double
    double at(int n) const;

    // The clear function
    // Purpose: clear and reset vector to an empty vector with capacity of n and size of 0
    // Parameters: none
    // Returns: none
    void clear();

    const Vector& Vector::operator=(const Vector & rho);

    friend ostream& operator<<(ostream& os, const Vector& vctr);//allows overloaded insertion operator

    // The destructor
    // Purpose: clears data on heap and prevents memory leaks
    // Parameters: none
    // Returns: none
    ~Vector();

};

//Vector.cpp
#include "stdafx.h"
#include "Vector.h"

//const int to hold value of 2
const int DUO = 2;
//const int to hold badIdex throw value
const int BAD = -6;


// The non-parameterized constructer
// Purpose: create an empty vector with capacity of 2 and size of 0
// Parameters: none
// Returns: vector
Vector::Vector()
{
    arr = new double[DUO];
    cap = 2;
    sz = 0;
}

// The Parameterized Constructer
// Purpose: create an empty vector with capacity of n and size of 0
// Parameters: int n
// Returns: vector
Vector::Vector(int n)
{
    arr = new double[n];
    cap = n;
    sz = 0;
}

// The destructor
// Purpose: clears data on heap and prevents memory leaks
// Parameters: none
// Returns: none
Vector::~Vector()
{       
    delete[] arr;
}

// The size function
// Purpose: get current size of vector
// Parameters: 
// Returns: vector size as an int
int  Vector::size() const
{
    return sz;
}

// The push_back function
// Purpose: push back values into vector
// Parameters: int n
// Returns: none
void Vector::push_back(int n)
{
    if (sz >= cap) increase_capacity(DUO * cap);
    arr[sz] = n;
    ++sz;
}

// The capacity function
// Purpose: get current capacity of vector
// Parameters: none
// Returns: capacity of vector as a double
double  Vector::capacity()
{
    return cap;
}

// The at function
// Purpose: get value stored in vector at index n
// Parameters: int n
// Returns: value stored at index n as a double
double Vector::at(int n) const
{
    if (arr[n] >= 0)
    {
        return arr[n];
    }
    else
    {
        throw BAD;
    }
}

// The clear function
// Purpose: clear and reset vector to an empty vector with capacity of n and size of 0
// Parameters: none
// Returns: none
void Vector::clear()
{
    delete[] arr;
    arr = new double[DUO]; 
    cap = 2;
    sz = 0;
}

//Driver.cpp
#include <iostream>
#include "Vector.h"
#include "Vector.cpp"
#include "stdafx.h"
using namespace std;


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


// The overloaded stream insertion function
//Purpose: allow cout to print objects
// Parameters: pointer to ostream object, pointer to Money object
// Returns: os object
ostream& operator<<(ostream& os, const Vector& vctr)
{

os << vctr;//allow printing of objects from Money class
return os;//return printed information
}

const Vector& Vector::operator=(const Vector & rho)
{
// TODO: insert return statement here
// test for self assignment
if (this == &rho)
    return *this;
sz = rho.size;
// clean up array in left hand object (this)
delete[] this;

// create a new array big enough to hold right hand object's data
lho.size = rho.size;
this->lho = new char[sz];

// copy the data
for (int i = 0; i < sz; i++)
{
    this->lho[i] = rho.lho[i];
}
// return this object
return *this;
}


int main( )
{
cout << "\nCreating a vector Sam of size 4.";
Vector 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.";
Vector 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);


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

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

1 个答案:

答案 0 :(得分:0)

Error (active)  identifier "lho" is undefined

导致此错误的原因是您使用的是未在任何位置定义的变量lho。您需要定义它或使用不同的变量。