操作员的意外输出<<在C++上课和用STD ::变种,STD ::访问Union
当前,如果我直接在main()中使用参数声明类对象,我的代码可以产生正确的输出。但是,当我将班级声明为实例以输出操作员<< ,结果不正确。如果有人可以帮助我解决这个问题,我将不胜感激。
这是我的Main()获得正确的字符串
int main() {
Product dieselMotorBike("A Diesel Motor Cycle", 5488.00, 1);
Product apples("Granny duBois", 0.78, 2);
}
输出:
A Diesel Motor Cycle
Granny duBois
这是使用操作员<<的完整代码。打印:
#include <iostream>
#include <variant>
#include <string>
#include <cstring>
#include<functional>
using namespace std;
// std::variant<int, float> units, kgs;
using Var = std::variant<int, float>;
typedef union {
int units;
float kgs;
} Amount;
class Product {
public:
char* selling;
unsigned length;
float unitPrice;
int unitType;
Amount howMuch;
Var v;
// Constructor without arguments
Product();
// Constructor with arguments
Product(const char* s, float unitPrice, int unitType);
~Product() { delete[] selling; }
/*!
* @brief String length.
* @return Value in String @c length.
*/
unsigned len ( ) const;
/*!
* @brief Append to String.
* @param[in] s A String object.
* @return A String reference to *this.
* @post String will equal the concatenation of itself with @a s.
*/
Product& operator+= (const Product& s);
char& operator[](size_t idx);
char operator[](size_t idx) const;
friend std::ostream& operator<<(std::ostream& os, const Product& s) {
os.write(s.selling, s.length);
return os;
}
// Return string length
int size() { return length; }
};
// Constructor with no arguments
Product::Product() : selling{new char[1] {'\0'}}, unitPrice{0.00}, unitType{0} {}
// Constructor with one argument
Product::Product(const char* s, float unitPrice, int unitType) {
if (s == nullptr) {
selling = new char[1]{'\0'};
length = 0;
this->unitPrice = 0.00;
this->unitType = 0;
} else {
length = std::strlen(s);
selling = new char[length + 1];
std::copy(s, s + length + 1, selling);
this->unitPrice = unitPrice;
this->unitType = unitType;
std::cout << s << "\n";
}
}
unsigned Product::len ( ) const
{
return length;
}
Product& Product::operator+=(const Product& s) {
unsigned len = length + s.length;
char* str = new char[len + 1];
for (unsigned j = 0; j < length; j++) str[j] = selling[j];
for (unsigned i = 0; i < s.length; i++) str[length + i] = s.selling[i];
str[len] = '\0';
delete[] selling;
length = len;
selling = str;
return *this;
}
Product operator+(const Product& lhs, const Product& rhs) {
Product rv(lhs);
rv += rhs;
return rv;
}
char& Product::operator[](size_t idx) { return selling[idx]; }
char Product::operator[](size_t idx) const { return selling[idx]; }
int main() {
int numItems = 2; int i;
char temp1[] = "A Diesel Motor Cycle";
char temp2[] = "Granny duBois";
Product dieselMotorBike;
Product apples;
// dieselMotorBike.selling = temp1;
strcpy(dieselMotorBike.selling, temp1);
dieselMotorBike.unitPrice = 5488.00;
dieselMotorBike.unitType = 1;
dieselMotorBike.howMuch.units = 4;
cout << dieselMotorBike << "\n";
//Product apples("Granny duBois", 0.78, 2);
Product * myEbayStore[2];
strcpy(apples.selling, temp2);
apples.unitPrice = 0.78;
apples.unitType = 2;
apples.howMuch.kgs = 0.5;
cout << apples << "\n";
myEbayStore[0] = &dieselMotorBike;
myEbayStore[1] = &apples;
for (i = 0; i < numItems; i++) {
printf("\n%s\n", myEbayStore[i]->selling);
switch (myEbayStore[i]->unitType) {
case 1:
printf("We have %d units for sale\n",
myEbayStore[i]->howMuch.units);
break;
case 2:
printf("We have %f kgs for sale\n",
myEbayStore[i]->howMuch.kgs);
break;
}
}
}
输出:
A Diesel Motor Cycle!�
A Diesel Motor Cycle
We have 4 units for sale
Granny duBois
We have 0.500000 kgs for sale
它输出更多“柴油摩托车”的线。我什至不知道如何!”出现在第一行。如何解决?
此外,我阅读了一些用STD ::变体,STD ::访问班级的方法,但不知道该如何编写。请告诉我示例代码以替换它。
Currently, my code can produce correct output if I declare class object with arguments in main() directly. But when I declare class to an instance to output by operator<< , it get incorrect result. I'd appreciate if someone can help me fix this issue.
Here's my main() that get correct string
int main() {
Product dieselMotorBike("A Diesel Motor Cycle", 5488.00, 1);
Product apples("Granny duBois", 0.78, 2);
}
Output:
A Diesel Motor Cycle
Granny duBois
Here's my full code that uses operator<< to print:
#include <iostream>
#include <variant>
#include <string>
#include <cstring>
#include<functional>
using namespace std;
// std::variant<int, float> units, kgs;
using Var = std::variant<int, float>;
typedef union {
int units;
float kgs;
} Amount;
class Product {
public:
char* selling;
unsigned length;
float unitPrice;
int unitType;
Amount howMuch;
Var v;
// Constructor without arguments
Product();
// Constructor with arguments
Product(const char* s, float unitPrice, int unitType);
~Product() { delete[] selling; }
/*!
* @brief String length.
* @return Value in String @c length.
*/
unsigned len ( ) const;
/*!
* @brief Append to String.
* @param[in] s A String object.
* @return A String reference to *this.
* @post String will equal the concatenation of itself with @a s.
*/
Product& operator+= (const Product& s);
char& operator[](size_t idx);
char operator[](size_t idx) const;
friend std::ostream& operator<<(std::ostream& os, const Product& s) {
os.write(s.selling, s.length);
return os;
}
// Return string length
int size() { return length; }
};
// Constructor with no arguments
Product::Product() : selling{new char[1] {'\0'}}, unitPrice{0.00}, unitType{0} {}
// Constructor with one argument
Product::Product(const char* s, float unitPrice, int unitType) {
if (s == nullptr) {
selling = new char[1]{'\0'};
length = 0;
this->unitPrice = 0.00;
this->unitType = 0;
} else {
length = std::strlen(s);
selling = new char[length + 1];
std::copy(s, s + length + 1, selling);
this->unitPrice = unitPrice;
this->unitType = unitType;
std::cout << s << "\n";
}
}
unsigned Product::len ( ) const
{
return length;
}
Product& Product::operator+=(const Product& s) {
unsigned len = length + s.length;
char* str = new char[len + 1];
for (unsigned j = 0; j < length; j++) str[j] = selling[j];
for (unsigned i = 0; i < s.length; i++) str[length + i] = s.selling[i];
str[len] = '\0';
delete[] selling;
length = len;
selling = str;
return *this;
}
Product operator+(const Product& lhs, const Product& rhs) {
Product rv(lhs);
rv += rhs;
return rv;
}
char& Product::operator[](size_t idx) { return selling[idx]; }
char Product::operator[](size_t idx) const { return selling[idx]; }
int main() {
int numItems = 2; int i;
char temp1[] = "A Diesel Motor Cycle";
char temp2[] = "Granny duBois";
Product dieselMotorBike;
Product apples;
// dieselMotorBike.selling = temp1;
strcpy(dieselMotorBike.selling, temp1);
dieselMotorBike.unitPrice = 5488.00;
dieselMotorBike.unitType = 1;
dieselMotorBike.howMuch.units = 4;
cout << dieselMotorBike << "\n";
//Product apples("Granny duBois", 0.78, 2);
Product * myEbayStore[2];
strcpy(apples.selling, temp2);
apples.unitPrice = 0.78;
apples.unitType = 2;
apples.howMuch.kgs = 0.5;
cout << apples << "\n";
myEbayStore[0] = &dieselMotorBike;
myEbayStore[1] = &apples;
for (i = 0; i < numItems; i++) {
printf("\n%s\n", myEbayStore[i]->selling);
switch (myEbayStore[i]->unitType) {
case 1:
printf("We have %d units for sale\n",
myEbayStore[i]->howMuch.units);
break;
case 2:
printf("We have %f kgs for sale\n",
myEbayStore[i]->howMuch.kgs);
break;
}
}
}
It outputs:
A Diesel Motor Cycle!�
A Diesel Motor Cycle
We have 4 units for sale
Granny duBois
We have 0.500000 kgs for sale
It output more line of "A Diesel Motor Cycle". I even don't know how !� appeared in the 1st line. How to solve it?
Besides, I read some ways to replace union by std::variant, std::visit in the class, but don't know how to write it. Please show me sample code to replace it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论