自定义向量类的重载赋值运算符 这样,对于一个赋值,我创建了一个类,它模拟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; } //stdafx.h #布拉格语一次 #包括“targetver.h” #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; //向量h 类向量 { double*arr;//指向此向量的第一个元素的指针 int cap;//arr可以容纳的元素数(即基础数组的大小) int sz;//此向量的大小 //增容函数 //目的:增加载体的容量 //参数:vecoter的新容量 //退货:无 空位增加容量(新的空位上限) { //将基础阵列的容量增加到sz。如果sz //小于当前容量,则不执行任何操作。 double*new_arr=new double[new_cap];//分配一个新数组 对于(int i=0;i

自定义向量类的重载赋值运算符 这样,对于一个赋值,我创建了一个类,它模拟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; } //stdafx.h #布拉格语一次 #包括“targetver.h” #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; //向量h 类向量 { double*arr;//指向此向量的第一个元素的指针 int cap;//arr可以容纳的元素数(即基础数组的大小) int sz;//此向量的大小 //增容函数 //目的:增加载体的容量 //参数:vecoter的新容量 //退货:无 空位增加容量(新的空位上限) { //将基础阵列的容量增加到sz。如果sz //小于当前容量,则不执行任何操作。 double*new_arr=new double[new_cap];//分配一个新数组 对于(int i=0;i,c++,vector,operator-overloading,C++,Vector,Operator Overloading,导致此错误的原因是您正在使用未在任何地方定义的变量lho。您需要定义它或使用不同的变量。您遇到了哪些具体问题?请问一个特定的问题——堆栈溢出不适用于代码审查。您的帖子似乎没有足够的信息,比如您的问题是什么?SO上的很多人都不想逐行筛选您的整个代码,因此如果您能够缩小您认为未来问题所在的范围,这可能会有所帮助。错误C2448'printV':函数样式初始值设定项似乎是函数定义project4\driver。cpp 102错误(活动)标识符“lho”是未定义的Project4\driver。cpp

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

您遇到了哪些具体问题?请问一个特定的问题——堆栈溢出不适用于代码审查。您的帖子似乎没有足够的信息,比如您的问题是什么?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将错误消息放入问题中并正确格式化。你可以编辑你的问题——不要把它放在评论里。您还需要在问题中发布格式正确的代码。我尝试将其定义为const-Vector&iho-Vector::operator=(const-Vector&rho),但这并没有解决任何问题,结果导致了更多问题…尝试将
lho
替换为
this
您的意思是将其替换为const-Vector&this-Vector::operator=(const-Vector&rho)?声明可能应该是
Vector&operator=(const-Vector&rho)
。在函数内部,您可以使用
this
作为变量(它是唯一隐式声明的变量,它指的是“当前”对象,在本例中是运算符的左侧)。
Error (active)  identifier "lho" is undefined