调试错误r6010已在C++中调用Abort
Debug Error r6010 Abort Has Been Called in C++
我刚刚第一次收到这个错误,我不太确定它意味着什么。
这是我第一次玩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 );
}
=====垃圾桶.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;
}
=====标头=====
#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
一个问题是:
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 < 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 */ }
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- C++ abort() 在函数内的抽象类对象指针调用上
- C++ abort() 已在 RapidJSON 上调用
- 调试错误R6010-已调用abort()
- 当我调用std::abort()时,cout没有显示
- R6010 abort() 已被调用
- 在调用 abort() 时设置断点
- abort() 的调用方在回溯中是未知的,任何调试它的建议
- 调试错误r6010已在C++中调用Abort
- 使用std::regex调用abort()来验证URL
- 在winsock recv()方法上调用了Abort()
- C++单线程工作,在新线程上它调用"abort()"