奇怪的超出范围错误
Strange out of range error
在下面的代码中,当我运行它时,我遇到了一个奇怪的超出范围的错误。这段代码是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_n
和s_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[我]是一个'好品格',可能,(我真的不知道你所说的"坏品格"和"好品格"是什么意思。
祝你好运!
相关文章:
- "myClock"未在此范围错误中声明
- 包含来自另一个文件的函数会导致范围错误(openFoam)
- Visual C:命名空间范围错误或晦涩功能?
- 在遍历字符串时,为什么我没有超出范围错误,即使我已经超出了它的长度?
- 此范围错误中未声明如何修复
- 尝试声明函数的局部变量,但得到范围错误
- c ++ to_string未在此范围错误 [Windows + Devcpp 环境] 中声明
- 将文件读取到向量的向量,超出范围错误
- 向量下标出的范围错误.即使向量的索引大于访问数据的索引,也会发生误差
- Eratosthenes C 的筛子 - 内存处的范围错误
- 向量:内存处的范围错误
- 测试完美哈希函数时超出范围错误
- 为什么此代码会给出 Vector 超出范围错误
- 在填充 Arduino 草图时出现范围错误
- 如何修复C++中矢量的"下标超出范围"错误?
- CPP-模板范围错误
- 在此范围错误中未声明函数错误
- C 表达式:向量下标出范围错误线:1733
- 无法在好友函数中实例化类?我没有得到在范围错误中声明
- 带有类的嵌套结构.范围错误