我的代码中的第一次机会异常[关闭]

Posted

技术标签:

【中文标题】我的代码中的第一次机会异常[关闭]【英文标题】:First-chance exception within my code [closed] 【发布时间】:2014-11-08 22:50:25 【问题描述】:

我正在为我的班级创建一个滑冰程序,并且在运行时遇到错误消息。当编译器尝试在 Judge 函数中添加性能和技术分数时,就会发生这种情况。任何帮助将不胜感激,如果需要任何澄清,请告诉我!

编辑:错误消息是 第一次机会异常位于 IceSkating1.exe 中的 0x00FE4686:0xC0000005:读取位置 0xCDCCDCDD5 的访问冲突。”和“未处理的异常在 IceSkating1.exe 中的 0x00FE4686:0xC0000005:访问冲突读取位置 0xCDCDCDD5。”

#include <iostream>
#include <iomanip>
using namespace std;

typedef int *intptr;

class Judge

private:
    int performance, technical, total = 0;

public:
    void SetPerformance(int a)  performance = a; 
    void SetTechnical(int a)  technical = a; 
    void SetTotal()  total = performance + technical; //EXCEPTION OCCURS HERE
    int GetTotal()  return total; 
;

class Skater

private:
    int ID;
    Judge* judges;
    int *ranks;

public:
    void SetID()
    
        int t;
        cout << "Please enter skater ID: ";
        cin >> t;
        ID = t;
    

    int GetID()
    
        return ID;
     

    void Judges(int qnty)
    
        int p, t;
        judges = new Judge[qnty];


        for (int count = 0; count < qnty; count++)
        
            cout << "Please enter the peformance score from judge " << count + 1 << ":";
            cin >> p;
            while ((p > 6) || (p < 0))
        
            cout << "Invalid score.  Please enter a value between 0 - 6. \n";
            cin >> p;
        

        judges[qnty].SetPerformance(p);


        cout << "Please enter the technical score from judge " << count + 1 << ":";
        cin >> t;

        while ((t > 6) || (t < 0))
        
            cout << "Invalid score.  Please enter a value between 0 - 6. \n";
            cin >> t;
        

        judges[qnty].SetTechnical(t);

    


int GetJudgeTotal(int judgeqnty)

    return judges[judgeqnty].GetTotal();


void SetRank(int judgeqnty, int rank)

    ranks = new int[judgeqnty];
    rank = ranks[judgeqnty];


int GetRank(int judgeqnty)

    return ranks[judgeqnty];

;


int main()

Skater* s;
int skaterqnty, judgeqnty;

cout << "Please enter number of contestants: ";
cin >> skaterqnty;

while ((skaterqnty > 24) || (skaterqnty < 0))

    cout << "Please enter a valid amount. \n";
    cin >> skaterqnty;


s = new Skater[skaterqnty];

for (int count = 0; count < skaterqnty; count++) 
    s[count].SetID();



cout << "Please enter odd number of judges: ";
cin >> judgeqnty;
if ((judgeqnty % 2) == 0 || (judgeqnty > 11))

    cout << "Please enter valid number of judges. \n";
    cin >> judgeqnty;


for (int count = 0; count < skaterqnty; count++)

    cout << "Please enter scores for skater " << s[count].GetID() << ": \n";
    s[skaterqnty].Judges(judgeqnty);


cout << s[0].GetJudgeTotal(0);

/*int jTotals[judgeqnty][skaterqnty];

for (int column = 0; column < skaterqnty; column++)
for (int row = 0; row < judgeqnty; row++)
jTotals[row][column] = s[column].JudgeTotal(judgeqnty);

*/

cout << "---Beginning rank sort---\n";
for (int m = 0; m < judgeqnty; m++) 
int _rank = 1; cout << "---First level of loop\n";
for (int n = 0; n < skaterqnty; n++)  cout << "---Second level \n";
for (int p = 0; p < skaterqnty; p++)  cout << "---Third level \n";
if (s[n].GetJudgeTotal(m) < s[p].GetJudgeTotal(m))

_rank++;
cout << "Control statement completed\n";

 cout << "End third level\n";
s[n].SetRank(m, _rank); cout << "End second level\n";
 cout << "First level completed. \n";


for (int a = 0; a < skaterqnty; a++)
    for (int b = 0; b < judgeqnty; b++)
    
        cout << "Current Ranking:\nSkater " << s[a].GetID() << ":\t" << s[a].GetRank(b) << "\t";
    


cout << "\nDONE.";


【问题讨论】:

而例外是......? 你确定异常就在那里引发吗?你调试过那个代码吗?如果有,你知道崩溃前变量所持有的值是什么吗? 【参考方案1】:

你分配一个大小为qnty的数组:

    judges = new Judge[qnty];

然后尝试访问qnty-th 元素:

    judges[qnty].SetPerformance(p);
    judges[qnty].SetTechnical(t);

这是undefined behaviour(元素编号从0qnty-1)。

你的意思是写judges[count]吗?

【讨论】:

我没有注意到,谢谢!但是,当我更改它并再次运行它时,会出现同样的问题:|【参考方案2】:

您更新了错误裁判的分数。你正在做:

judges[qnty].SetPerformance(p);

但是您是在循环中执行此操作,所以您的意思可能是:

judges[count].SetPerformance(p);

【讨论】:

以上是关于我的代码中的第一次机会异常[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

PresentationFramework.dll 中 System.NullReference 类型异常的第一次机会异常?

如果没有人调用该函数,为啥我会得到第一次机会异常?

VC++ 中的第一次机会异常

MSXML6 中的第一次机会异常

标准 win32 wndproc 中的第一次机会异常

如何避免 ResultSet 是 Java 中的关闭异常?