0

这是一个基本程序,用于获取两个 5 位数字作为字符串,并利用 '+' 上的运算符重载对 2 个数字使用加法。

#include <iostream>
#include <limits>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;


class IntStr
{
   int InputNum;
   public:
     //IntStr();
     IntStr::IntStr(int num);
     IntStr operator+ (const IntStr &);
     //~IntStr();
     void Display();
};

IntStr::IntStr(int num)
{
  InputNum = num;
}

void IntStr::Display()
{
   cout << "Number is (via Display) : " << InputNum <<endl;
}


IntStr IntStr::operator+ (const IntStr & second) {
        int add_result = InputNum + second.InputNum;
        return IntStr(add_result);
        }



int main()
{
    string str;
    bool option = true;
    bool option2 = true;
    while (option)
    {
    cout << "Enter the number : " ;
    if (!getline(cin, str)) 
    {
       cerr << "Something went seriously wrong...\n";
    }

    istringstream iss(str);
    int i;
    iss >> i;    // Extract an integer value from the stream that wraps str

    if (!iss) 
    {
       // Extraction failed (or a more serious problem like EOF reached)
       cerr << "Enter a number dammit!\n";
    } 
    else if (i < 10000 || i > 99999) 
    {
    cerr << "Out of range!\n";
    } 
    else 
    {
      // Process i
      //cout << "Stream is: " << iss << endl; //For debugging purposesc only
      cout << "Number is : " << i << endl;
      option = false;
      IntStr obj1 = IntStr(i);
      obj1.Display();
    }
    }//while


    while (option2)
    {
    cout << "Enter the second number : " ;
    if (!getline(cin, str)) 
    {
       cerr << "Something went seriously wrong...\n";
    }

    istringstream iss(str);
    int i;
    iss >> i;    // Extract an integer value from the stream that wraps str

    if (!iss)  //------------------------------------------> (i)
    {
       // Extraction failed (or a more serious problem like EOF reached)
       cerr << "Enter a number dammit!\n";
    } 
    else if (i < 10000 || i > 99999) 
    {
    cerr << "Out of range!\n";
    } 
    else 
    {
      // Process i
      //cout << "Stream is: " << iss << endl; //For debugging purposes only
      cout << "Number is : " << i << endl;
      option2 = false;
      IntStr obj2 = IntStr(i);
      obj2.Display();
      //obj1->Display();
    }
    }//while

    //IntStr Result = obj1 + obj2; // --------------------> (ii)
    //Result.Display();

    cin.get();
}

需要澄清上述代码中的 (i) 和 (ii) 点...

(1) (i) 实际上做了什么?

(2) (ii) -> 不编译.. 出现错误“obj1 未声明(首先使用此函数)”。这是因为 obj1 和 obj2 仅在 while 循环内声明吗?我如何在全球范围内访问它们?

4

3 回答 3

1

1) 来自http://www.cplusplus.com/reference/iostream/ios/operatornot/

布尔运算符!( ) 常量;评估流对象

如果在流上设置了任一错误标志(failbit 或 badbit),则返回 true。否则返回false。

来自http://www.cplusplus.com/reference/iostream/ios/fail/

当错误与操作本身的内部逻辑有关时,通常由输入操作设置failbit,而当错误涉及流的完整性丢失时,通常会设置badbit,即使不同的操作也可能持续存在在流上执行。

2)这两个对象不在范围内,它们只存在于前面的括号中。

于 2009-04-20T08:36:25.420 回答
0
if (!iss) 

测试流是否处于错误状态,如果转换失败或您处于流的末尾,就会出现这种情况

obj1 在这里定义:

else 
    {
      // Process i
      //cout << "Stream is: " << iss << endl; //For debugging purposesc only
      cout << "Number is : " << i << endl;
      option = false;
      IntStr obj1 = IntStr(i);
      obj1.Display();
    }

因此它是 else-block 的本地,并且不能在它之外访问。如果你想增加它的范围,修改它在块之外的定义。然而,将它移到所有块之外(即使其成为全局)并不是一个好主意。

于 2009-04-20T08:31:57.600 回答
0
  1. 调用在布尔上下文中评估流的重载运算符。这将检查流的状态以查看先前的操作是否失败 - 如果是这样,您不能依赖整数变量中的值i是有效的,因为流上的输入不是整数。

  2. 变量obj1obj2在 while 循环的范围内定义 - 它们在范围之外不可用。您可以在 while 范围之外声明它们,在这种情况下,变量将保存它在 while 循环中保存的最后一个值。

于 2009-04-20T08:34:30.183 回答