c++ - 为什么这个重载运算符给出错误的输出?
问题描述
加减整数的输出给了我正确的解决方案,但是当尝试将字符串添加到 int 时应该会产生错误,但事实并非如此。不知道我哪里出错了。
ERROR + 3 is ERROR
ERROR - 4 is ERROR
ERROR + hello is ERROR
ERROR - world is ERROR
3 + ERROR is ERROR
3 - ERROR is ERROR
3 + 4 is 7
3 - 4 is -1
**3 + hello is 7
3 - world is -1
4 + ERROR is 7**
4 + 3 is 7
4 - 3 is 1
**4 + hello is 7
4 - world is 1
**hello + ERROR is 7
hello - 3 is 1
hello + 4 is 7**
hello + world is helloworld
**hello - world is 1**
world + ERROR is helloworld
world - ERROR is 1
world + 3 is helloworld
world - 3 is 1
world + 4 is helloworld
world - 4 is 1
world + hello is worldhello
world - hello is 1
下面是带有 getter 的头文件,用于返回一个 in 或一个字符串。
#ifndef VALUE_H
#define VALUE_H
#include <iostream>
#include <string>
using namespace std;
enum ValType
{
VINT,
VSTR,
VERR
};
class Value
{
ValType T;
int I;
string S;
public:
Value() : T(VERR), I(0) {}
Value(int vi) : T(VINT), I(vi) {}
Value(string vs) : T(VSTR), I(0), S(vs) {}
ValType GetType() const { return T; }
bool IsErr() const { return T == VERR; }
bool IsInt() const { return T == VINT; }
bool IsStr() const { return T == VSTR; }
int GetInt() const
{
if (IsInt())
return I;
throw "RUNTIME ERROR: Value not an integer";
}
string GetStr() const
{
if (IsStr())
return S;
throw "RUNTIME ERROR: Value not a string";
}
// add op to this
Value operator+(const Value &op) const
{
try
{
if (IsInt() && op.IsInt()) // integer addition
return GetInt() + op.GetInt();
if (IsStr() && op.IsStr()) // string addition
return GetStr() + op.GetStr();
}
catch (std::string &e)
{
cout << "RUNTIME ERROR " << e << endl;
}
//throw "RUNTIME ERROR...";
}
// subtract op from this
Value operator-(const Value &op) const
{
try
{
if (IsInt() && op.IsInt()) // integer addition
return GetInt() - op.GetInt();
}
catch (std::string &e)
{
cout << "RUNTIME ERROR " << e << endl;
}
//throw "RUNTIME ERROR...";
}
// multiply this by op
Value operator*(const Value &op) const;
// divide this by op
Value operator/(const Value &op) const;
friend ostream &operator<<(ostream &out, const Value &op)
{
if (op.IsInt())
out << op.I;
else if (op.IsStr())
out << op.S;
else
out << "ERROR";
return out;
}
};
#endif
下面是主要驱动,将整数和字符串作为值,循环遍历它们
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include "val.h"
void doOp(const Value &a, const Value &b)
{
cout << a << " + " << b << " is " << flush << a + b << endl;
cout << a << " - " << b << " is " << flush << a - b << endl;
}
int main()
{
Value a;
Value b(3);
Value c(4);
Value d("hello");
Value e("world");
vector<Value> vals({a, b, c, d, e});
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
cout << "d=" << d << endl;
cout << "e=" << e << endl;
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (i != j)
doOp(vals[i], vals[j]);
return 0;
}
解决方案
当被比较的两个对象不是同一类型时,您的operator+
和的返回值operator-
是不确定的。Value
当发生不匹配时,您需要返回一个空的,因为您的默认构造函数将成员Value
初始化为:ValType T
VERR
Value operator+(const Value &op) const
{
if (IsInt() && op.IsInt()) // integer addition
return GetInt() + op.GetInt();
if (IsStr() && op.IsStr()) // string addition
return GetStr() + op.GetStr();
return Value();
}
// subtract op from this
Value operator-(const Value &op) const
{
if (IsInt() && op.IsInt()) // integer subtraction
return GetInt() - op.GetInt();
// in comments, you said you want "hello - world" to be "hello"
if (IsStr() && op.IsStr()) // string subtraction
return GetStr();
return Value();
}
推荐阅读
- eclipse - 黄瓜测试用例执行后在 Eclipse 中出现奇怪的控制台错误/异常
- java - 将嵌套的对象列表持久保存到同一个表中 JPA hibernate
- prisma2 - Prisma2,分组日期
- react-native - 具有反应原生和裸工作流博览会的后台应用程序
- c# - 自定义授权过滤器未触发默认代码
- highcharts - Highcharts - 我无法显示数据集的图例
- java - 使用 Java stream() 查找 Arrays.asList 中 2 个元素的平均值
- android - 从 StateFlow 收集状态
- javascript - 将元素从 onclick 事件渲染到 DOM
- python - ASCII 表中没有模块的漂亮打印列表