如果析构函数里什么都不写的话是不报错的 但是不知道这个析构函数应该怎么写 我写的1 delete【】 a不对!
#pragma once
#include<iostream>
class MyVector {
public:
// Implement constructor & destructor
MyVector();
MyVector(int _length);
~MyVector();
// Implement operators
MyVector operator+(const MyVector& b);
MyVector operator-(const MyVector& b);
MyVector operator+(const int b);
MyVector operator-(const int b);
friend std::ostream& operator<< (std::ostream& out, MyVector& b);
friend std::istream& operator>> (std::istream& in, MyVector& b);
// Add an additional constructor or operator if needed.
private:
int length;
double* a;
};
#include"my_vector.h"
#include<iostream>
using namespace std;
MyVector::MyVector() {
}
MyVector::MyVector(int _length) :length(_length) {
this->a = new double[_length];
}
MyVector::~MyVector() {
cout << "??????";
}
MyVector MyVector::operator+(const MyVector& b) {
MyVector tmp(this->length);
for (int i = 0; i < this->length; i++) {
tmp.a[i] = this->a[i] + b.a[i];
}
return tmp;
}
MyVector MyVector::operator-(const MyVector& b) {
MyVector tmp(this->length);
for (int i = 0; i < this->length; i++) {
tmp.a[i] = this->a[i] - b.a[i];
}
return tmp;
}
MyVector MyVector::operator+(const int b) {
MyVector tmp(this->length);
for (int i = 0; i < this->length; i++) {
tmp.a[i] = this->a[i] + b;
}
return tmp;
}
MyVector MyVector::operator-(const int b) {
MyVector tmp(this->length);
for (int i = 0; i < this->length; i++) {
tmp.a[i] = this->a[i] - b;
}
return tmp;
}
ostream& operator<< (std::ostream& out, MyVector& b) {
for (int i = 0; i < b.length; i++) {
out << b.a[i] << " ";
}
out << endl;
return out;
}
istream& operator>> (std::istream& in, MyVector& b) {
for (int i = 0; i < b.length; i++) {
in >> b.a[i];
}
return in;
}
#include"my_vector.h"
#include<iostream>
#include<sstream>
using namespace std;
int main() {
string input;
getline(cin, input);
stringstream s(input);
string create;
s >> create;
if (create == "new") {
int num;
s >> num;
MyVector a(num);
MyVector b(num);
MyVector c(num);
cout << "enter a" << endl;
cin >> a;
cout << "enter b" << endl;
cin >> b;
while (1) {
string input_1;
getline(cin, input_1);
stringstream ss(input_1);
string command;
ss >> command;
if (command == "quit") {
break;
}
else if (command == "print") {
string obj_name;
ss >> obj_name;
if (obj_name == "a") {
cout << a;
}
else if (obj_name == "b") {
cout << b;
}
else if (obj_name == "c") {
cout << c;
}
}
else if (command == "c=") {
string ob1;
char ob2;
char op;
int num;
ss >> ob1 >> op;
if (op == '+') {
ss >> ob2;
if (isdigit(ob2) != 0) {
num = ob2 - '0';
if (ob1 == "a") {
c = a + num;
}
else if (ob1 == "b") {
c = b + num;
}
}
else if (isalpha(ob2) != 0) {
if (ob1 == "a") {
if (ob2 == 'a') {
c = a + a;
}
else if (ob2 == 'b') {
c = a + b;
}
}
else if (ob1 == "b") {
if (ob2 == 'a') {
c = b + a;
}
else if (ob2 == 'b') {
c = b + b;
}
}
}
}
else if (op == '-') {
ss >> ob2;
if (isdigit(ob2) != 0) {
num = ob2 - '0';
if (ob1 == "a") {
c = a - num;
}
else if (ob1 == "b") {
c = b - num;
}
}
else if (isalpha(ob2) != 0) {
if (ob1 == "a") {
if (ob2 == 'a') {
c = a - a;
}
else if (ob2 == 'b') {
c = a - b;
}
}
else if (ob1 == "b") {
if (ob2 == 'a') {
c = b - a;
}
else if (ob2 == 'b') {
c = b - b;
}
}
}
}
}
}
}
return 0;
}
因为你重载了 加号 和 减号。
而你返回的是一个类型
MyVector MyVector::operator+(const int b)
当函数的返回值是对象,函数执行完成返回调用者时 c++会调用拷贝构造函数。
同样的,也会调用析构函数。
而你的a指针 并没有在这期间进行初始化。
所以产生了内存异常。
#include <stdio.h>
#include <ctype.h>
#include<iostream>
#include<sstream>
#pragma warning(disable:4996)
class MyVector {
public:
// Implement constructor & destructor
MyVector();
MyVector(int _length);
~MyVector();
// Implement operators
MyVector & operator+(const MyVector& b);
MyVector & operator-(const MyVector& b);
MyVector & operator+(const int b);
MyVector & operator-(const int b);
friend std::ostream& operator<< (std::ostream& out, MyVector& b);
friend std::istream& operator >> (std::istream& in, MyVector& b);
// Add an additional constructor or operator if needed.
private:
int length;
double* a;
};
using namespace std;
MyVector::MyVector() {
}
MyVector::MyVector(int _length) :length(_length) {
this->a = new double[_length];
}
MyVector::~MyVector() {
delete a;
}
MyVector & MyVector::operator+(const MyVector& b) {
MyVector tmp(this->length);
for (int i = 0; i < this->length; i++) {
tmp.a[i] = this->a[i] + b.a[i];
}
return tmp;
}
MyVector & MyVector::operator-(const MyVector& b) {
MyVector tmp(this->length);
for (int i = 0; i < this->length; i++) {
tmp.a[i] = this->a[i] - b.a[i];
}
return tmp;
}
MyVector & MyVector::operator+(const int b) {
MyVector tmp(this->length);
for (int i = 0; i < this->length; i++) {
tmp.a[i] = this->a[i] + b;
}
return tmp;
}
MyVector & MyVector::operator-(const int b) {
MyVector tmp(this->length);
for (int i = 0; i < this->length; i++) {
tmp.a[i] = this->a[i] - b;
}
return tmp;
}
ostream& operator<< (std::ostream& out, MyVector& b) {
for (int i = 0; i < b.length; i++) {
out << b.a[i] << " ";
}
out << endl;
return out;
}
istream& operator >> (std::istream& in, MyVector& b) {
for (int i = 0; i < b.length; i++) {
in >> b.a[i];
}
return in;
}
int main() {
string input;
getline(cin, input);
stringstream s(input);
string create;
s >> create;
if (create == "new") {
int num;
s >> num;
MyVector a(num);
MyVector b(num);
MyVector c(num);
cout << "enter a" << endl;
cin >> a;
cout << "enter b" << endl;
cin >> b;
while (1) {
string input_1;
getline(cin, input_1);
stringstream ss(input_1);
string command;
ss >> command;
if (command == "quit") {
break;
}
else if (command == "print") {
string obj_name;
ss >> obj_name;
if (obj_name == "a") {
cout << a;
}
else if (obj_name == "b") {
cout << b;
}
else if (obj_name == "c") {
cout << c;
}
}
else if (command == "c=") {
string ob1;
char ob2;
char op;
int num;
ss >> ob1 >> op;
if (op == '+') {
ss >> ob2;
if (isdigit(ob2) != 0) {
num = ob2 - '0';
if (ob1 == "a") {
c = a + num;
}
else if (ob1 == "b") {
c = b + num;
}
}
else if (isalpha(ob2) != 0) {
if (ob1 == "a") {
if (ob2 == 'a') {
c = a + a;
}
else if (ob2 == 'b') {
c = a + b;
}
}
else if (ob1 == "b") {
if (ob2 == 'a') {
c = b + a;
}
else if (ob2 == 'b') {
c = b + b;
}
}
}
}
else if (op == '-') {
ss >> ob2;
if (isdigit(ob2) != 0) {
num = ob2 - '0';
if (ob1 == "a") {
c = a - num;
}
else if (ob1 == "b") {
c = b - num;
}
}
else if (isalpha(ob2) != 0) {
if (ob1 == "a") {
if (ob2 == 'a') {
c = a - a;
}
else if (ob2 == 'b') {
c = a - b;
}
}
else if (ob1 == "b") {
if (ob2 == 'a') {
c = b - a;
}
else if (ob2 == 'b') {
c = b - b;
}
}
}
}
}
}
}
return 0;
}
将返回对象改成返回引用,就不会产生此异常。
更加深入的
#include <iostream>
using namespace std;
class Line
{
public:
int getLength(void);
Line(int len); // 简单的构造函数
Line(const Line &obj); // 拷贝构造函数
~Line(); // 析构函数
private:
int *ptr;
};
// 成员函数定义,包括构造函数
Line::Line(int len)
{
cout << "调用构造函数" << endl;
// 为指针分配内存
ptr = new int;
*ptr = len;
}
Line::Line(const Line &obj)
{
cout << "调用拷贝构造函数并为指针 ptr 分配内存" << endl;
ptr = new int;
*ptr = *obj.ptr; // 拷贝值
}
Line::~Line(void)
{
cout << "释放内存" << endl;
delete ptr;
}
int Line::getLength(void)
{
return *ptr;
}
void display(Line obj)
{
cout << "line 大小 : " << obj.getLength() << endl;
}
void displayV(Line & obj)
{
cout << "line 大小 : " << obj.getLength() << endl;
}
Line getLine() {
Line line(10);
return line;
}
Line & getLineV() {
Line line(10);
return line;
}
// 程序的主函数
int main()
{
Line line(10);
Line &v = line;
cout << "***用形参是对象来写函数***" << endl;
display(line);
cout << "***用形参是对象调用结束***" << endl;
cout << "***用形参是引用来写函数***" << endl;
displayV(v);
cout << "***用形参是引用调用结束***" << endl;
cout << "***用返回值对象来写函数***"<<endl;
getLine();
cout << "***用返回值对象调用结束***" << endl;
cout << "***用返回值引用来写函数***" << endl;
getLineV();
cout << "***用返回值引用调用结束***" << endl;
return 0;
}
调用构造函数
***用形参是对象来写函数***
调用拷贝构造函数并为指针 ptr 分配内存
line 大小 : 10
释放内存
***用形参是对象调用结束***
***用形参是引用来写函数***
line 大小 : 10
***用形参是引用调用结束***
***用返回值对象来写函数***
调用构造函数
调用拷贝构造函数并为指针 ptr 分配内存
释放内存
释放内存
***用返回值对象调用结束***
***用返回值引用来写函数***
调用构造函数
释放内存
***用返回值引用调用结束***
释放内存
在形参是对象,返回值是对象,等号左侧是对象时
都会调用拷贝构造函数。
而如果是引用,则不会调用。