2015-11-19 8 views
-1

Я только что получил эту ошибку в первый раз, и я не уверен, что это значит.Ошибка отладки r6010 Прерывание было вызвано в 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); 
} 

===== 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 
+0

"Driver"? Я не вижу ничего отдаленно похожего на водителя. А как насчет форматирования кода * сейчас *, а не в следующей программе, потому что это помогает с поиском ошибок? – deviantfan

+1

И вы использовали свой отладчик, чтобы найти, где он сбой? – deviantfan

ответ

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 не обнаруживает ошибку. Таким образом, ваша программа завершается.

Я не знаю намерений этого кода, но если вы просто его запустили, вы увидите, что my_Contents < my_Size каждый раз, когда вы вызываете add_Item, тем самым вызывая исключение.

Почему бы не написать сообщение, чтобы убедиться, что что-то не так?

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 */ } 
+0

Этот код имел в виду, что размер корзины не меньше, чем размер внутри. (так что не переполняйте trashcan) мои исключения, похоже, являются основной проблемой, так как когда я их удаляю, программа работает нормально. Проблема в том, что целью этого задания является использование исключений, поэтому я стараюсь изо всех сил заставить его работать. Спасибо за помощь, кстати! – drakey

 Смежные вопросы

  • Нет связанных вопросов^_^