【发布时间】:2016-09-26 06:27:26
【问题描述】:
我正在尝试检查字符串表示是否等于给定的整数。我打算在函数中为此使用 stringstream。我也有一个operator=。
我对如何一起执行这些操作以及我是否遗漏了一些东西感到有些困惑。这是我作业的最后一点,这只是我整个程序的一个小sn-p。我找不到很多这方面的指南,我感觉他们都将我引导到 atoi 或 atod,我不允许使用它们。
#ifndef INTEGER
#define INTEGER
using std::string;
class Integer
{
private:
int intOne;
string strOne;
public:
Integer() {
intOne = 0;
}
Integer(int y) {
intOne = y;
}
Integer(string x) {
strOne = x;
}
void equals(string a);
Integer &operator=(const string*);
string toString();
};
#endif
在此标头中,我不确定要为 = 运算符使用什么参数。
#include <iostream>
#include <sstream>
#include <string>
#include "Integer.h"
using namespace std;
Integer &Integer::operator=(const string*)
{
this->equals(strOne);
return *this;
}
void Integer::equals(string a)
{
strOne = a;
toString(strOne);
}
string Integer::toString()
{
stringstream ss;
ss << intOne;
return ss.str();
}
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <string>
#include <ostream>
using namespace std;
#include "Menu.h"
#include "Integer.h"
#include "Double.h"
int main()
{
Integer i1;
i1.equals("33");
cout << i1;
}
对不起,如果这是一个不好的问题,我对这种类型的作业不太熟悉,我会寻求任何可以得到的帮助。谢谢。
【问题讨论】:
标签: c++ operator-overloading stringstream