“访问违规”运行 C++ 时出错程序
我不断收到以下错误:
0x5a6fca58 处未处理的异常 Gofish.exe 中的(msvcr100d.dll): 0xC0000005:写入访问冲突 位置0x0ff3b113。
我尝试运行的代码是:
#include <iostream>
#include <string>
#include<Array>
using namespace std;
class Card{
string suit;
int rank;
public:
Card(int a, string b){
rank=a;
suit=b;
}
Card(){}
string getSuit(){
return suit;
}
int getRank(){
return rank;
}
};
class Deck{
Card deck [52];
public:
Deck(){
for(int i=1; i<=13; i++){
deck [i]=Card(i, "spades");
deck [i*2]=Card(i, "hearts");
deck [i*3]=Card(i, "diamonds");
deck [i*4]=Card(i, "clubs");
}
}
void list(){
for(int i=1; i<=52; i++){
cout << deck [i].getRank() << " of " << deck [i].getSuit() << endl;
}
}
};
int main(){
Deck deck=Deck();
deck.list();
system("pause");
return 0;
}
我正在使用的编译器是 Microsoft Visual C++ 2010 Express(如果这可能会产生任何影响)。
I keep getting an error of:
Unhandled exception at 0x5a6fca58
(msvcr100d.dll) in Gofish.exe:
0xC0000005: Access violation writing
location 0x0ff3b113.
The code that I'm trying to run is:
#include <iostream>
#include <string>
#include<Array>
using namespace std;
class Card{
string suit;
int rank;
public:
Card(int a, string b){
rank=a;
suit=b;
}
Card(){}
string getSuit(){
return suit;
}
int getRank(){
return rank;
}
};
class Deck{
Card deck [52];
public:
Deck(){
for(int i=1; i<=13; i++){
deck [i]=Card(i, "spades");
deck [i*2]=Card(i, "hearts");
deck [i*3]=Card(i, "diamonds");
deck [i*4]=Card(i, "clubs");
}
}
void list(){
for(int i=1; i<=52; i++){
cout << deck [i].getRank() << " of " << deck [i].getSuit() << endl;
}
}
};
int main(){
Deck deck=Deck();
deck.list();
system("pause");
return 0;
}
The compiler I'm using is Microsoft Visual C++ 2010 Express if that might effect anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为数组是从零开始的。数组中的最高索引是 51,但您试图访问 52。此外,在您的实现中,索引 0 处的第一张卡将永远不会被访问。
Because arrays are zero based. The highest index in your array is 51 but you are trying to access 52. Also, in your implementation, the first card, at index 0, will never be accessed.
在大小为
52
的数组deck
中,您尝试使用无效的索引52
。您可以将
for
循环更改为:In the array
deck
of size52
you are trying to use index52
which is invalid.You can change your
for
loop as:这看起来像家庭作业,所以我会给你一些提示:
检查你的 for 循环逻辑。
请记住,数组中的第一个条目是 0 而不是 1。
This looks like homework so I'll give you some hints:
Check your for loop logic.
Remember that the first entry in an array is 0 not 1.
除了数组索引溢出问题。这可能有一个问题:
没有必要:您可以简单地编写
Deck Deck;
来代替。但这样,如果您的编译器不执行任何优化,您最终可能会得到尝试使用默认赋值运算符复制Deck
对象的代码,该运算符执行成员复制。因此,将尝试复制Card
固定大小数组,并将一个固定大小数组复制到另一个固定大小数组将无法正常工作。Besides the array index overflow issue. There might be a problem with this:
There is no need for that: you could simply write
Deck deck;
instead. But this way, if your compiler is performing no optimizations, you might end up with code that tries to copy aDeck
object using default assingment operator, which performs a memberwise copy. Thus, will try to copy aCard
fixed size array, and copying one fixed size array to another will not work properly.