【问题标题】:Debug Error r6010 Abort Has Been Called in C++在 C++ 中调用了调试错误 r6010 中止
【发布时间】:2015-11-19 01:53:42
【问题描述】:

我第一次收到这个错误,我不太确定它是什么意思。

这是我第一次玩 try/catch。我知道我的格式很乱,下次记得整理一下!

=======驱动程序=========

 #include "trashcan.h"
 #include <iostream> 
 using namespace std;


int main( ) {
cout << "Welcome to TrashCan Program!" << endl;
TrashCan myCan;
TrashCan yourCan;
TrashCan nothing;

yourCan.setSize( 12 );
myCan.setSize( 12 );
nothing.setSize ( 0 );

yourCan.addItem( );
yourCan.addItem( );


myCan.addItem( );








myCan.printCan();
yourCan.printCan();

TrashCan combined = yourCan + myCan;
  cout << "this can's filled to " << combined.getContents( ) << endl;

TrashCan other = combined - myCan;
  cout << "the other can's filled to " << other.getContents( ) << endl;

if (combined > other) {
cout << "looks like combined is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (myCan > other) {
cout << "looks like myCan is bigger..." << endl;
}
else {
cout << "looks like other is bigger..." << endl;
}
if (yourCan < myCan) {
cout << "looks like yourCan is smaller..." << endl;
}
else {
cout << "looks like myCan is smaller..." << endl;
}

// let's throw some exceptions...

try {
TrashCan empty = empty - combined;
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown... 
// so the lines of code here should
// be run, not the cout statement above...
cout << "exception was caught.  moving on..." << endl;
}

try {
nothing.addItem( );
cout << "something not right here..." << endl;
} catch( std::logic_error ) {
// an exception should get thrown... 
// so the lines of code here should
// be run, not the cout statement above...
cout << "exception was caught.  moving on..." << endl;

}


return( 0 );
}

=====trashcan.cpp========

#include "trashcan.h"
#include <iostream>
#include <cstdlib>

using namespace std;



TrashCan::TrashCan( ) {
myIsCovered = false;
my_Size = 0;
my_Contents = 0;
my_Empty = 0;
}

TrashCan::TrashCan( int size ) {
myIsCovered = false;
my_Size = size;
my_Contents = 0;
my_Empty = 0;
}

TrashCan::TrashCan( int size, int contents, int empty ) {
myIsCovered = false;
my_Size = size;
my_Contents = contents;
my_Empty = empty;
}

void TrashCan::setSize( int size ) {
if (size < 0 ){
    throw logic_error("exception was caught.  moving on...");
}
else{
my_Size = size;
}
}

int TrashCan::getSize( ) {
return( my_Size );

}

int TrashCan::getContents( ) {
return( my_Contents );
}

void TrashCan::addItem( ) {
my_Contents = my_Contents + 1;
if (my_Contents > my_Size || my_Contents < my_Size){
    throw logic_error("exception was caught.  moving on...");
}
}

void TrashCan::empties( ) {
my_Contents = 0;
}

void TrashCan::cover( ) {
myIsCovered = true;
}

void TrashCan::uncover( ) {
myIsCovered = false;
}

void TrashCan::printCan( ) {
cout << "A TrashCan with a size=" << my_Size << " and containing " <<                     my_Contents << " piece";
if (my_Contents != 1) {
    cout << "s";
}
cout << " of trash" << endl;
}

TrashCan operator+ ( const TrashCan& yourCan, const TrashCan& myCan ) {             /* This is where I override the + and - operators... I put in the error messages     for over filling and under filling the trashcan */

    TrashCan combined;
combined.my_Contents = yourCan.my_Contents + myCan.my_Contents ;


    return( combined );

}

TrashCan operator- ( const TrashCan& combined, const TrashCan& myCan ){
TrashCan other;
TrashCan empty;
empty.my_Contents = -1;

if ( empty.my_Contents < 0 ){
    throw logic_error("exception was caught.  moving on...");
}
else
{
other.my_Contents = combined.my_Contents - myCan.my_Contents;
return (other );
}
}

bool operator> ( const TrashCan& myCan, const TrashCan& yourCan) {                      /* MY bool override for the '< and > '  operators. */
TrashCan combined;
combined.my_Contents = yourCan.my_Contents + myCan.my_Contents ;

TrashCan empty;
empty.my_Empty = 0;


TrashCan other;
other.my_Contents = combined.my_Contents - myCan.my_Contents;


 return combined.my_Contents > other.my_Contents && myCan.my_Contents >     other.my_Contents; 



}

bool operator< (const TrashCan& myCan, const TrashCan& yourCan ) {          
TrashCan combined;
combined.my_Contents = yourCan.my_Contents + myCan.my_Contents ;

TrashCan other;
other.my_Contents = combined.my_Contents - myCan.my_Contents;

if (yourCan.my_Contents < myCan.my_Contents ){
    return yourCan.my_Contents > myCan.my_Contents ; }
else {
    return yourCan.my_Contents < myCan.my_Contents;
}





}


ostream& operator<< (ostream &out, TrashCan& myCan)
{
out <<myCan.my_Contents << " Contents in this trashcan";
return out;
}

======HEADER=========

#ifndef TRASHCAN_H
#define TRASHCAN_H
#include <iostream>
using namespace std;


class TrashCan {
public:
TrashCan( );
TrashCan( int size );
TrashCan( int size, int contents, int empty );

void setSize( int size );
int  getSize( );
int  getContents( );
void addItem( );
void empties( ); 
void cover( );
void uncover( );

friend TrashCan operator + ( const TrashCan& yourCan, const TrashCan& myCan );              /*as the assignment asks... I override the +,-, and <,> operators. */
friend TrashCan operator - ( const TrashCan& combined, const TrashCan& myCan);             /*I have used the bool statements for the <,> to compare sizes */
friend bool operator > ( const TrashCan& myCan,  const TrashCan& yourCan);
friend bool operator < ( const TrashCan& myCan, const TrashCan& yourCan );

friend ostream& operator<< (ostream &out, TrashCan& myCan);


void printCan( );

private:
bool myIsCovered;
int my_Size;
int my_Contents;
int my_Empty;
};



#endif

【问题讨论】:

  • “驱动程序”?我没有看到远程看起来像司机的东西。那么现在而不是在下一个程序中格式化你的代码怎么样,因为它有助于发现错误?
  • 而且,您是否使用调试器查找崩溃的位置?

标签: c++ debugging abort


【解决方案1】:

一个问题是这样的:

void TrashCan::addItem() {
    my_Contents = my_Contents + 1;
    if (my_Contents > my_Size || my_Contents < my_Size) {
        throw logic_error("exception was caught.  moving on...");
    }
}

您正在抛出 logic_error 异常,但您的 main 函数未捕获该错误。这样你的程序就终止了。

我不知道这段代码的意图,但如果你只是简单地运行它,你会在每次调用add_Item 时看到my_Contents &lt; my_Size,从而导致抛出异常。

为什么不放一条信息来说服自己有问题呢?

void TrashCan::addItem() {
    my_Contents = my_Contents + 1;
    if (my_Contents > my_Size || my_Contents < my_Size) 
    {
        std::cout << "oh no, I'm about to die..." << std::endl;
        throw logic_error("exception was caught.  moving on...");
    }
}

然后你的main函数,如果你不想在一个未处理的异常上结束它,必须做一个try/catch

try { 
   yourCan.addItem();
}
catch(logic_error& e) { /* whatever */ }

【讨论】:

  • 代码的意思是垃圾桶的大小不小于里面的大小。 (所以不会溢出垃圾桶)我的异常似乎是主要问题,因为当我删除它们时,程序运行正常。问题是这个作业的目的是练习异常,所以我正在尽我最大的努力让它发挥作用。顺便谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
  • 1970-01-01
相关资源
最近更新 更多