“访问违规”运行 C++ 时出错程序

发布于 2024-10-03 10:42:33 字数 966 浏览 7 评论 0原文

我不断收到以下错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧街凉风 2024-10-10 10:42:33

因为数组是从零开始的。数组中的最高索引是 51,但您试图访问 52。此外,在您的实现中,索引 0 处的第一张卡将永远不会被访问。

deck [i*4-1]=Card(i, "clubs");

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.

deck [i*4-1]=Card(i, "clubs");
好多鱼好多余 2024-10-10 10:42:33

在大小为 52 的数组 deck 中,您尝试使用无效的索引 52

您可以将 for 循环更改为:

  for(int i=0; i<52; i+=4){
    deck [i]   = Card(i, "spades");
    deck [i+1] = Card(i, "hearts");
    deck [i+2] = Card(i, "diamonds");
    deck [i+3] = Card(i, "clubs");
  }

In the array deck of size 52 you are trying to use index 52 which is invalid.

You can change your for loop as:

  for(int i=0; i<52; i+=4){
    deck [i]   = Card(i, "spades");
    deck [i+1] = Card(i, "hearts");
    deck [i+2] = Card(i, "diamonds");
    deck [i+3] = Card(i, "clubs");
  }
甜嗑 2024-10-10 10:42:33

这看起来像家庭作业,所以我会给你一些提示:

检查你的 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.

我早已燃尽 2024-10-10 10:42:33

除了数组索引溢出问题。这可能有一个问题:

int main(){
 Deck deck=Deck();
 // ... 
}

没有必要:您可以简单地编写 Deck Deck; 来代替。但这样,如果您的编译器不执行任何优化,您最终可能会得到尝试使用默认赋值运算符复制 Deck 对象的代码,该运算符执行成员复制。因此,将尝试复制 Card 固定大小数组,并将一个固定大小数组复制到另一个固定大小数组将无法正常工作。

Besides the array index overflow issue. There might be a problem with this:

int main(){
 Deck deck=Deck();
 // ... 
}

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 a Deck object using default assingment operator, which performs a memberwise copy. Thus, will try to copy a Card fixed size array, and copying one fixed size array to another will not work properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文