【问题标题】:Overloading assignment operator for homemade Vector class自制 Vector 类的重载赋值运算符
【发布时间】:2017-08-06 04:08:41
【问题描述】:

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

//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;
}

【问题讨论】:

  • 您遇到了哪些具体问题?请提出一个具体问题——堆栈溢出不适用于代码审查。
  • 您的帖子似乎没有足够的信息,例如您的问题是什么? SO 上的很多人都不想逐行筛选您的整个代码,因此如果您可以缩小您认为未来问题所在的范围,这可能会有所帮助。
  • 错误 C2448 'printV': 函数样式初始化程序似乎是函数定义 project4\driver.cpp 102 错误(活动)标识符“lho”未定义 Project4\driver.cpp 45 错误(活动) “Vector”类没有成员“lho” Project4\driver.cpp 46 错误(活动)类“Vector”没有成员“lho” Project4\driver.cpp 51 错误(活动)类“Vector”没有成员“lho” Project4\driver.cpp 51 错误 C2065 'Vector': 未声明的标识符 project4\driver.cpp 20 错误 C2065 'v': 未声明的标识符 project4\driver.cpp 20
  • 有很多错误,但主要问题似乎解决了 operator= 重载函数。
  • @jonthie 将错误消息放入问题中并正确格式化。您可以编辑您的问题 - 不要将其放在评论中。您还需要在问题中发布格式正确的代码。

标签: c++ vector operator-overloading


【解决方案1】:
Error (active)  identifier "lho" is undefined

这个错误是因为你使用了变量lho,它没有在任何地方定义。您需要定义它或使用不同的变量。

【讨论】:

  • 我尝试定义它 const Vector& iho Vector::operator=(const Vector& rho) 但这并没有解决任何问题,最终导致更多问题......
  • 尝试用this替换lho
  • 你的意思是用 const Vector& this Vector::operator=(const Vector& rho) 替换它?
  • 声明应该是Vector&amp; operator=(const Vector&amp; rho)。在函数内部,您可以使用this 作为变量(它是唯一隐式声明的变量,它指的是“当前”对象,在这种情况下是运算符的左侧)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-04
  • 2017-10-07
  • 2013-01-03
  • 2014-08-16
  • 2016-02-16
  • 1970-01-01
相关资源
最近更新 更多