奇怪的超出范围错误

Strange out of range error

本文关键字:范围 错误      更新时间:2023-10-16

在下面的代码中,当我运行它时,我遇到了一个奇怪的超出范围的错误。这段代码是Stroustrup的《使用C++编程原理与实践》一书的一个练习的答案。

#include <Simple_window.h>
#include <iostream>
using namespace std;
//-----------------------------------
class Person
{
public:
    Person(string f_n, string s_n, int a) {
        for(int i=0; i<f_n.size() || i<s_n.size(); i++) 
            if(f_n[i] == ';' || f_n[i] == ':' || f_n[i] == ''' || 
                f_n[i] == '[' || f_n[i] == ']' || f_n[i] == '*'  || 
                f_n[i] == '&' || f_n[i] == '^' || f_n[i] == '%'  ||
                f_n[i] == '$' || f_n[i] == '#' || f_n[i] == '@'  ||
                f_n[i] == '!' ||
                s_n[i] == ';' || s_n[i] == ':' || s_n[i] == ''' || 
                s_n[i] == '[' || s_n[i] == ']' || s_n[i] == '*'  || 
                s_n[i] == '&' || s_n[i] == '^' || s_n[i] == '%'  ||
                s_n[i] == '$' || s_n[i] == '#' || s_n[i] == '@'  ||
                s_n[i] == '!')
                error("Bad characters!");
        first_name =  f_n;
        second_name = s_n;
        if(a < 0 || a >=150) 
            error ("Bad age!");
        age = a;
    }
    string get_first_name()  { return first_name; }
    string get_second_name() { return second_name; }
    int get_age()     { return age;  }
private:
    string first_name, second_name;
    int age;
};
//-------------------------------------------------------------
istream& operator>>(istream& is, Person& p) {
    string f_n, s_n;
    int a;
    cout <<"Enter first name and second name and age:";
    if(!(is >> f_n >> s_n >> a))
        error("Can't read the object");
    Person pp(f_n,s_n,a);
    p = pp;
    return is;
}
//-------------------------------------------------------
ostream& operator<<(ostream& os, Person& p) {
    if(!(os << p.get_first_name() <<' '<< p.get_second_name() << p.get_age()))
        error("Can't write the object");
    return os;
}
//------------------------------------------------------
int main() try
{
    Person p("Goofy","tom",53);
    vector <Person> vp;
    for(int i=0; i<5; i++) {
        cin>>p;
        vp.push_back(p);
    }
    for(int i=0; i<vp.size(); i++)
        cout << vp[i] << endl;
    return 0;
}
catch(exception& e) {
    cerr << e.what();
    return 0;
}
catch(...) {
    return 1;
}

该错误与int main()Person p("Goofy","tom",53);)之后的第 2 行有关,但我不知道为什么那些strings会犯这个错误!

这是错误:http://i59.tinypic.com/v487sw.png

i<f_n.size() || i<s_n.size()

应该是

i<f_n.size() && i<s_n.size()

否则,您将在其中一个字符串上超出界限。例如,如果f_n长度为 3,s_n长度为 5,则3 < 3 || 3 < 5计算结果为 true,您将索引f_n[3]超出界限。

PS:实际上,尽管您可能希望将检查提取到函数中并分别循环两个字符串,否则将无法完全检查两个字符串中最长的字符串。

您需要

分别验证f_ns_n,否则它仅在长度相等时才有效。

这看起来像一个"学习如何将事物分离到函数中"的练习,所以可能是这样的:

bool valid_character(c)
{
   // return true if c is a valid character, false otherwise.
}
bool valid_name(string name)
{
   // true if all the characters in name are valid.
}
Person(string f_n, string s_n, int a)
{
   if (!valid_name(f_n))
   {
      error("Bad first name");
   }
   if (!valid_name(s_name))
   {
      error("Bad second name");
   }
   if(a < 0 || a >=150) 
   {
       error ("Bad age!");
   }
   first_name = f_n;
   second_name = s_n;
   age = a;
}

有些部分作为练习离开了。

在你的构造函数中,你想检查f_n和s_n是否包含任何"坏字符",如果你确定它们具有相同的长度,你不能在一个循环中检查它们。因此,我建议您分别检查f_n和s_n。我


另外if((f_n[i]>'a' && f_n[i]<'z') ||(f_n[i]>'A' &f_n[i]<'Z'))通过这样做,你可以确保f_n[我]是一个'好品格',可能,(我真的不知道你所说的"坏品格"和"好品格"是什么意思。

祝你好运!