這是什么情況.
頭檔案
#pragma once
class HZ
{
public:
HZ(double lengthValue, double widthValue, double heightValue);
HZ(const HZ& obj);
friend HZ operator-(HZ& c1, HZ& c2);
void print_1();
HZ& operator+(HZ& c1);
protected:
private:
public:
double length;
double width;
double height;
};
HZ operator-(HZ& c1,HZ& c2);
類實作.cpp
#include "HZ.h"
#include <iostream>
using namespace std;
using std::cout;
using std::endl;
HZ::HZ(double lengthValue, double widthValue, double heightValue)
{
this->length = lengthValue;
this->width = widthValue;
this->height = heightValue;
}
HZ::HZ(const HZ& obj)
{
this->length = obj.length;
this->width = obj.width;
this->height = obj.height; // 運算子多載加號 obj.height 輸出是亂碼
}
void HZ :: print_1()
{
cout << "length:" << this->length << "width:" << this->width << "height:" << this->height << endl;
}
HZ operator-(HZ& c1, HZ& c2)
{
HZ tmp(c1.length-c2.length,c1.width-c2.width, c1.height - c2.height);
return tmp;
}
HZ& HZ:: operator+(HZ& c1)
{
HZ tmp(this->length+c1.length,this->width+c1.width, this->height + c1.height);
return tmp;
}
主函式.cpp
int main()
{
HZ t1(1.0, 3.1, 4.0);
HZ t3(9.0, 2.0, 5.0);
HZ t4 = t1 + t3;
t4.print_1();
HZ t5 = t1 - t3;
t5.print_1();
return 0;
}


uj5u.com熱心網友回復:
錯誤不少HZ& HZ:: operator+(HZ& c1)
{
HZ tmp(this->length+c1.length,this->width+c1.width, this->height + c1.height);
return tmp; //這里回傳的是區域變數的參考,把這個函式改為HZ.
}
uj5u.com熱心網友回復:
VS2015親測好像沒問題:class HZ
{
public:
HZ(double lengthValue, double widthValue, double heightValue);
HZ(const HZ& obj);
friend HZ operator-(HZ& c1, HZ& c2);
void print_1();
HZ& operator+(HZ& c1);
protected:
private:
public:
double length;
double width;
double height;
};
HZ operator-(HZ& c1,HZ& c2);
#include <iostream>
using namespace std;
using std::cout;
using std::endl;
HZ::HZ(double lengthValue, double widthValue, double heightValue)
{
this->length = lengthValue;
this->width = widthValue;
this->height = heightValue;
}
HZ::HZ(const HZ& obj)
{
this->length = obj.length;
this->width = obj.width;
this->height = obj.height; // 運算子多載加號 obj.height 輸出是亂碼
}
void HZ :: print_1()
{
cout << "length:" << this->length << "width:" << this->width << "height:" << this->height << endl;
}
HZ operator-(HZ& c1, HZ& c2)
{
HZ tmp(c1.length-c2.length,c1.width-c2.width, c1.height - c2.height);
return tmp;
}
HZ& HZ:: operator+(HZ& c1)
{
HZ tmp(this->length+c1.length,this->width+c1.width, this->height + c1.height);
return tmp;
}
int main()
{
HZ t1(1.0, 3.1, 4.0);
HZ t3(9.0, 2.0, 5.0);
HZ t4 = t1 + t3;
t4.print_1();
HZ t5 = t1 - t3;
t5.print_1();
return 0;
}
//length:10width:5.1height:9
//length:-8width:1.1height:-1
//
uj5u.com熱心網友回復:
HZ& operator+(HZ& c1); 不要回傳參考,+=才回傳參考uj5u.com熱心網友回復:
供參考:#include <iostream>
//#pragma once
class HZ
{
public:
HZ(double lengthValue, double widthValue, double heightValue);
HZ(const HZ& obj);
friend HZ operator-(HZ& c1, HZ& c2);
void print_1();
HZ operator+(HZ& c1); //HZ& operator+(HZ& c1);
protected:
private:
public:
double length;
double width;
double height;
};
HZ operator-(HZ& c1,HZ& c2);
//#include "HZ.h"
//#include <iostream>
using namespace std;
using std::cout;
using std::endl;
HZ::HZ(double lengthValue, double widthValue, double heightValue)
{
this->length = lengthValue;
this->width = widthValue;
this->height = heightValue;
}
HZ::HZ(const HZ& obj)
{
this->length = obj.length;
this->width = obj.width;
this->height = obj.height; // 運算子多載加號obj.height 輸出是亂碼
}
void HZ :: print_1()
{
cout << "length:" << this->length << " width:" << this->width << " height:" << this->height << endl;
}
HZ operator-(HZ& c1, HZ& c2)
{
HZ tmp(c1.length-c2.length,c1.width-c2.width, c1.height - c2.height);
return tmp;
}
HZ HZ::operator+(HZ& c1) //HZ& HZ:: operator+(HZ& c1)
{
return HZ(this->length+c1.length,this->width+c1.width, this->height + c1.height);
//HZ tmp(this->length+c1.length,this->width+c1.width, this->height + c1.height);
//return tmp;
}
int main()
{
HZ t1(1.0, 3.1, 4.0);
HZ t3(9.0, 2.0, 5.0);
HZ t4 = t1 + t3;
t4.print_1();
HZ t6(t4);
t6.print_1();
HZ t5 = t1 - t3;
t5.print_1();
return 0;
}
//length:10 width:5.1 height:9
//length:10 width:5.1 height:9
//length:-8 width:1.1 height:-1
//請按任意鍵繼續. . .
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/270279.html
標籤:C++ 語言
上一篇:無法打開檔案“xxx.lib”
