I\'m getting errors that say \"no suitable constructor exists to convert from \"
ID: 3751121 • Letter: I
Question
I'm getting errors that say "no suitable constructor exists to convert from "const char [21]" to "DequeEmpty""
THIS IS THE CPP FILE
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include "LinkedDeque.hpp"
#include "DLinkedList.hpp"
#include "DLinkedList.cpp"
LinkedDeque::LinkedDeque()// constructor
: D(), n(0) { }
int LinkedDeque::size() const// number of items in the queue
{ return n; }
bool LinkedDeque::empty() const// is the queue empty?
{ return n == 0; }
// get the front element
const Elem& LinkedDeque::front() const throw(DequeEmpty) {
if (empty())
throw DequeEmpty("front of empty Deque");
return D.front();// linkedlist front is queue front
}
// insert new first element
void LinkedDeque::insertFront(const Elem& e) {
D.addFront(e);
n++;
}
// insert new last element
void LinkedDeque::insertBack(const Elem& e) {
D.addBack(e);
n++;
}
// remove first element
void LinkedDeque::removeFront() throw(DequeEmpty) {
if (empty())
throw DequeEmpty("removeFront of empty deque");
D.removeFront();
n--;
}
// remove last element
void LinkedDeque::removeBack() throw(DequeEmpty) {
if (empty())
throw DequeEmpty("removeBack of empty deque");
D.removeBack();
n--;
}
THIS IS THE HEADER FILE
#ifndef LINKEDDEQUE_H
#define LINKEDDEQUE_H
#include <string>
#include "DLinkedList.hpp"
#include "DLinkedList.cpp"
class DequeEmpty
{
public:
std::string what()
{
return "MyException happened";
}
};
typedef std::string Elem; // deque element type
class LinkedDeque
{ // deque as doubly linked list
public:
LinkedDeque(); // constructor
int size() const; // number of items in the deque
bool empty() const; // is the deque empty?
const Elem& front() const throw(DequeEmpty); // first element
const Elem& back() const throw(DequeEmpty); // last element
void insertFront(const Elem& e); // insert new first element
void insertBack(const Elem& e); // insert new last element
void removeFront() throw(DequeEmpty); // remove first element
void removeBack() throw(DequeEmpty); // remove last element
private:
DLinkedList D; // doubly linked list of elements
int n; // number of elements
};
#endif
Explanation / Answer
Since you have not provided the code for class DLinkedList, it becomes difficult for me to answer your question with 100% correctness. Hence, I would request you to provide the full code.
However, i will still try to answer your query.
I think you are getting error in the line:
throw DequeEmpty("front of empty Deque");
I think the error has something to do with your custom Exception "DequeEmpty".
A custome exception class in C++ looks like:
class myexception: public exception
{
virtual const char* what() const throw()
{
return "My exception happened";
}
};
Since I don't know how your class DequeEmpty looks like, your exception should look like.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.