No Such File Or Directory Exists - c++

I'm getting the error no such file or directory exists and it is confusing the hell out of me. I don't want help filling in my methods or anything, that I want to do for myself. I want to fix this error so that I can get started on my project.
This is the error I get:
My Error
Here is my code where the error takes place. If you need anything else, I will gladly update my question, or provide relevant information. Thanks to anyone who helps
#ifndef _DLINKEDLIST_H_
#define _DLINKEDLIST_H_
#include <cstdlib>
#include <stdexcept>
#include <string>
using namespace std;
// template class for doubly-linked list node
template <class T>
class Node
{
public:
T data;
//string data;
Node<T>* prev;
Node<T>* next;
// default constructor
//template <class T>
Node(T value)
{
data = value;
prev = NULL;
next = NULL;
}
};
// DLinkedList class definition
template <class T>
class DLinkedList
{
private:
// DLinkedList private members
int size; // number of items stored in list
Node<T>* front; // references to the front
Node<T>* back; // and back of the list
// helper function for deep copy
// Used by copy constructor and operator=
void CopyList(const DLinkedList& ll);
// helper function for deep delete
// Used by destructor and copy/assignment
void DeleteList();
public:
// default constructor
DLinkedList();
// copy constructor, performs deep copy of list elements
DLinkedList(const DLinkedList& ll);
// destructor
~DLinkedList();
// MUTATORS
// Inserts an item at the front of the list
// POST: List contains item at position 0
// PARAM: item = item to be inserted
void InsertFront(T item);
// Inserts an item at the back of the list
// POST: List contains item at back
// PARAM: item = item to be inserted
void InsertBack(T item);
// Inserts an item in position p (0-indexed)
// Throws exception for invalid index
// PRE: 0 <= p <= size
// POST: List contains item at position p
// PARAM: item = item to be inserted, p = position where item will be inserted
void InsertAt(T item, int p);
// Removes and returns an item from position p (0-indexed)
// Throws exception if list is empty or index invalid
// PRE: 0 <= p < size
// POST: Item is removed from list
// PARAM: p = position from where item will be removed
T RemoveAt(int p);
// Removes duplicates from the list, preserving existing order of remaining items.
// The first occurrence of any duplicate (relative to the front of the list)
// is the one which remains.
// We have not yet learned about efficiency so you may implement this in any way
// as long as the resulting list satisfies the requirement above.
// PRE:
// POST: List contains no duplicates, front and back point to the appropriate nodes
// PARAM:
void RemoveDuplicates();
// ACCESSORS
// Returns size of list
int Size() const;
// Returns whether the list is empty
bool IsEmpty() const;
// Returns existence of item
bool Contains(T item) const;
// Returns item at index (0-indexed)
// Throws exception for invalid index
T ElementAt(int p) const;
// OVERLOADED OPERATORS
// overloaded assignment operator
// must work in the following cases:
// list2 = list1 -> general case
// list2 = list2 -> should do nothing
DLinkedList& operator=(const DLinkedList& ll);
};
#include "dlinkedlist.cpp"
#endif

Answer was a quick and easy #include "..\srcdlinkedlist.cpp"
instead of #include "dlinkedlist.cpp"

Related

Circularly linked queue not enqueing values

I have to implement a Circularly linked queue class as the LinkedQueueType class. For some reason, when I call the enqueue function, it isn't getting added to the queue, even my test prints don't print out. Here is my queue class:
#define LINKED_QUEUE_H
class FullQueue {};
class EmptyQueue{};
typedef int ItemType;
struct NodeType{
ItemType info;
NodeType* next;
};
class LinkedQueueType {
public:
LinkedQueueType ();
// Class constructor.
// Because there is a default constructor, the precondition // that the queue has been initialized is omitted. LinkedQueueType(const LinkedQueueType& qt);
//Copy Constructor
LinkedQueueType operator=(const LinkedQueueType& rhs); //Overloaded assignment operator=
~LinkedQueueType ();
// Class destructor.
void MakeEmpty();
// Function: Initializes the queue to an empty state.
// Post: Queue is empty.
bool IsEmpty() const;
// Function: Determines whether the queue is empty.
// Post: Function value = (queue is empty)
bool IsFull() const;
// Function: Determines whether the queue is full. // Post: Function value = (queue is full)
void Enqueue(ItemType newItem);
// Function: Adds newItem to the rear of the queue. // Post: newItem is at rear of queue.
void Dequeue(ItemType& item);
// Function: Removes front item from the queue and returns it in // item.
// Post: If (queue is empty) EmptyQueue exception is thrown
// and item is undefined
// else front element has been removed from queue and
// item is a copy of removed element.
void Print(); //Print function
private:
NodeType* rear;
int length;
};
#endif
and here is the implementation for the enqueue function:
void LinkedQueueType::Enqueue(ItemType newItem){
NodeType *newNode=nullptr;
NodeType*temp=nullptr;
newNode->info=newItem;
newNode->next=nullptr;
if(rear==nullptr){
rear=newNode;
}else{
temp=rear->next;
rear->next=newNode;
}
rear=newNode;
rear->next=temp;
}
Here is my testing:
cout<<"hi";
LinkedQueueType q;
q.Enqueue(5);
When I run my driver, it doesn't print, could someone show me the way!
This is your code:
NodeType *newNode=nullptr;
NodeType*temp=nullptr;
newNode->info=newItem;
newNode->next=nullptr;
This is what you are saying:
Make a new pointer called newNode of type NodeType, and let it point to nothing.
Do the same for variable 'temp'.
Now we put newItem in nothing -> Because that is what newNode is pointing to.
Your program tries to dereference the variable newNode to access its property info. But that's not possible.
This is what your code should be:
NodeType *newNode = new NodeType();
NodeType*temp=nullptr;
newNode->info=newItem;
newNode->next=nullptr;
This way your program has an object to derefer and get a property from.
The code you posted definitely crashes (segmentation fault on Linux or access violation on Windows™) dereferencing the null pointer. You need to create the node before accessing it, like:
NodeType *newNode = new NodeType();

Creating instances for a linked list class in C++

I am currently learning C++ linked list and I hit this piece of code from the textbook.I have trouble understanding this:
const string& e
I am trying to write some instances for this class in my main function to see how things works but don't know how.
For example, I want to add 3, 5, 7 to the list and add 1 to the front of the list, than delete 7 from the list.
#include <cstdlib>
#include <iostream>
#include <string>
using std::string;
using namespace std;
class StringNode { // a node in a list of strings
private:
string elem; // element value
StringNode* next; // next item in the list
friend class StringLinkedList; // provide StringLinkedList
// access
};
class StringLinkedList { // a linked list of strings
public:
StringLinkedList(); // empty list constructor
~StringLinkedList(); // destructor
bool empty() const; // is list empty?
const string& front() const; // get front element
void addFront(const string& e); // add to front of list
void removeFront(); // remove front item list
private:
StringNode* head; // pointer to the head of list
};
StringLinkedList::StringLinkedList() // constructor
: head(NULL) { }
StringLinkedList::~StringLinkedList() // destructor
{ while (!empty()) removeFront(); }
bool StringLinkedList::empty() const // is list empty?
{ return head == NULL; }
const string& StringLinkedList::front() const // get front element
{ return head->elem; }
void StringLinkedList::addFront(const string& e) { // add to front of list
StringNode* v = new StringNode; // create new node
v->elem = e; // store data
v->next = head; // head now follows v
head = v; // v is now the head
}
void StringLinkedList::removeFront() { // remove front item
StringNode* old = head; // save current head
head = old->next; // skip over old head
delete old; // delete the old head
}
I tried to look for a duplicate which explains how C++ uses call-by-value.
In C++ a function parameter of type T will make a copy of the object before the function is called.
int myFunction( int value )
{
value = value + 1;
}
int main( int argc, char * argv[] )
{
int elem = 6;
myFunction( elem );
printf( "%d\n", elem ); // elem = 6;
}
In the above example a copy is made of the int value, which is sent to myFunction, and the copy is incremented.
This may not be what is wanted, and we can change the result to 7, by modifying myFunction to take a reference to the value. This is done by using the '&' to describe a reference value.
int myFunction( int & value )
{
value = value + 1;
}
int main( int argc, char * argv[] )
{
int elem = 6;
myFunction( elem );
printf( "%d\n", elem ); // elem = 7;
}
In the above case, no copy is made, and elem gets updated.
There are 2 main reasons you pass references to a function
To allow the value (or object) to be updated.
To avoid the cost of copying the value.
In your quoted example the second case is why the const string & is used (reference to string object). std::string, has costs to construct and destroy, so by sending a reference to avoid this, is more efficient.
To compliment this usage, the reference, is usually made const to convince the compiler that the value should not be changed.

Pointing a base pointer at a derived instance

I'm working on my final project for C++ and my professor specified that we had to have three class files for a linked list.
The first named LinkedList holds the head and the tail, as well as two overloaded operators in which we have to use the list as an array, and add an element to the end of the array.
The second named Node holds the two generic values Seat and Row.
The third and final named RNode holds the values of the next, previous spots in the list, as well as reservation status.
My problem is when using my LinkedList.cpp, defining all of the functions, I cannot figure out how to set node equal to the head, because the types are different. I can set the next node in the list with tempNode.setNext(Head);. But when I try to do tempNode = tempNode.getNext() it says the types are not the same. What is an easy way for me to make this work?
Here is my code.
This is supposed to use the Linked List as an array and return the pointer to the Node correlating with the integer passed in.
int& LinkedList::operator[] (const int &middle) {
RNode *tempNode;
tempNode->setNext(Head);
tempNode = tempNode->getNext(); // Error here
for (int i = 0; i < middle; i++) {
}
}
Here are the three class files I have currently made.
Linked List Class
#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED
#include "Node.h"
class LinkedList {
private:
Node* Head; // Head of linked list
Node* Tail; // Tail of linked list
public:
// Constructors
LinkedList(); // Set default values
LinkedList(Node*, Node*); // Set values passed in to head and tail
int& operator [] (const int &); // Overloaded [] operator PAGE 854 HOW TO USE THIS
// Treat list like an array.
// First node will be [0]
// Return pointer to node indicated inside of brackets
Node& operator += (const Node &); // Overloaded += operator
// Adds a node to the end of the linked list
// Head
void setHead(Node*); // Sets head of list
Node* getHead(); // Returns the head of list
// Tail
void setTail(Node*); // Sets tail of list
Node* getTail(); // Returns tail of list
};
#endif // LINKEDLIST_H_INCLUDED
Reservation Node Class
#ifndef RNODE_H_INCLUDED
#define RNODE_H_INCLUDED
#include "Node.h"
using namespace std;
class RNode : public Node {
private:
Node* Next; // Next node pointer
Node* Prev; // Previous node pointer
bool reservationStatus(); // Reservation status
public:
// Constructors
RNode(); // Sets default values
RNode(Node*, Node*, bool); // Takes values passed in and sets them
// Overloaded operators
friend ostream &operator << (ostream &, Node*); // Prints out correct symbol based on reservation status
friend istream &operator >> (istream &, Node*); // Prints correct symbol based on reservation status .
// Next
void setNext(Node*); // Sets next node in list
Node* getNext(); // Returns next node in list
// Prev
void setPrev(Node*); // Sets previous node in list
Node* getPrev(); // Returns previous node in list
// Reservation Status
void setReservationStatus(bool); // Sets reservation status of a current node
bool getReservationStatus(); // Returns reservation status
};
#endif // RNODE_H_INCLUDED
Node Class
#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
class Node {
protected:
int row;
int seat;
public:
// Constructors
Node(); // Sets default values
Node(int, int); // Sets row and seat to values passed in
// Row
void setRow(int); // Sets row for current node
int getRow(); // Gets row for current node
// Seats
void setSeat(int); // Sets seat for current node
int getSeat(); // Gets seat for current node
};
#endif // NODE_H_INCLUDED
In summary, how can I match the types so that I can set RNode tempNode equal to a Node? This is very confusing and I can't really find a good explanation on how to solve this.
Keep in mind, according to my instructions I have to have the classes created this way. If it were up to me, I would have combined the RNode and Node class.

Binary Tree (insert and search within radius)

I am writing a binary tree search program but I'm not sure how to add nodes and search through them. The nodes come from a .txt file that is being read with a different file so just assume that already works.
The text file looks like:
Name Location
Old Building 31.2222
New Building 21.2111
Like I said, the program already reads in the file so that's not an issue. However, I have to insert the name and location into the nodes of the binary tree. Then I have to search everything within a range which is where the plus minus comes from.
Side note: my copy constructor may be incorrect as well though it complies properly.
Thanks for the help!
#ifndef BINTREE_HPP
#define BINTREE_HPP
#include <utility>
#include <string>
#include <vector>
class bintree {
// A binary search tree for locations in Lineland.
// Notes:
// - Assume a flat, one-dimensional world with locations from -180 to 180.
// - All locations and distances are measured in the same units (degrees).
public:
// Default constructor
bintree() {
this->root = NULL;
}
// Copy constructor
bintree(const bintree &t) {
this -> root = NULL;
*this = t;
}
// Destructor
~bintree() {
}
// Copy assignment is implemented using the copy-swap idiom
friend void swap(bintree &t1, bintree &t2) {
using std::swap;
// Swap all data members here, e.g.,
// swap(t1.foo, t2.foo);
// Pointers should be swapped -- but not the things they point to.
}
bintree &operator= (bintree other) {
// You don't need to modify this function.
swap(*this, other);
return *this;
}
void insert(const std::string& name, double p) {
// insert node with name and location (p)
}
void within_radius(double p, double r, std::vector<std::string> &result) const {
// Search for elements within the range `p` plus or minus `r`.
// Clears `result` and puts the elements in `result`.
// Postcondition: `result` contains all (and only) elements of the
// tree, in any order, that lie within the range `p` plus or minus
// `r`.
}
private:
struct node
{
node *left;
node *right;
};
node* root;
};
#endif
First, your nodes need to hold the data:
struct node
{
node *left;
node *right;
std::string name; // This is the key for your reasearch
double p; // followed by other data
};
Then you can think to browsing through your tree to insert a new node.
In this example, I assume that you can insert several nodes with the same name.
void insert(const std::string& name, double p) {
node *n = new node; // create a new node
n->name=name; n->p=p; // intialise the data payload
n->left=n->right=nullptr; // and make it a leaf.
if (root==nullptr) // if tree is empty,
root = n; // add the new node.
else { // else find where to insert it
node* t=root;
while (true) {
if (t->name > n->name) { // go to left
if (t->left==nullptr) {
t->left = n;
break;
}
else t=t->left;
}
else if (t->name == n->name) { // insert between current and next
n->right = t->right;
t->right = n;
break;
}
else { // go to right
if (t->right==nullptr) {
t->right = n;
break;
}
else t=t->right;
}
}
}
}
Here a live demo.
Note that I have only answered your insertion question, you still have to do a lot on your own (operator= and copy constructor need review, a destructor needs to be created, etc...)

Constructor and Destructor Declaration Syntax with Template Class

I am trying to make a queue implementing a linked list but am running into a compiler error. The error is coming from the overloaded assignment operator function on the line where I call the destructor (marked with an all-caps comment). I have a hunch it is a simple fix that has something to do with the syntax of my constructor/destructor declarations.
The error I am getting states the following code: error C2512: 'Queue<char>::Queue' : no appropriate default constructor available
It mentions no constructor, but the line it refers to is the one below where I am trying to call the destructor.
Thanks in advance for your help.
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using namespace std;
template <class Type>
class Queue // Create a Queue data structure implementing a linked list
{
private: // The private members
struct Cell // The Cell class will be the blueprints for each link in the list
{
Type data; // The information held by the cell
Cell* next; // The link to the next cell
};
Cell* first = NULL;
Cell* last = NULL;
public: // The public members
Queue(Type);
bool isEmpty();
void push(Type);
Type pop();
Queue<Type>& operator=(Queue<Type>&);
friend ostream& operator<<(ostream&, const Queue<Type>&);
~Queue();
};
template<class Type>
Queue<Type>::Queue(Type inputData) // Constructor that initializes the queue with a new cell that last and first point to
{
first = new Cell;
first->data = inputData;
first->next = NULL;
last = first;
}
template<class Type>
Queue<Type>& Queue<Type>::operator=(Queue<Type>& queue) // Overload "=" so that it performs a deep copy of a Queue object
{
if (!queue.isEmpty())
{
~Queue(); // HERE IS THE ERROR LINE
Cell* rhs = queue.first;
while (rhs != NULL)
{
push(rhs->data);
rhs = rhs->next;
}
}
return *this;
}
template<class Type>
Queue<Type>::~Queue() // Destructor that deallocates all of the memory used by the queue.
{
if (!isEmpty()) // We only need to deallocate the queue if it is non-empty
{
Cell *link = last;
while (link != NULL) // Until we reach the end of the queue, keep deleting each link
{
pop();
}
first = NULL;
last = NULL;
}
else // If the queue is already empty, let the user know
{
cout << "Cannot call destructor. The list is already empty.\n";
}
}
#endif
Check out this thread: Can i call destructor from its class method?. An easy way around this is to make a function to empty the queue, then call it from the destructor and assignment operator.
template<class Type>
void Queue<Type> empty(){
if (!isEmpty()) // We only need to deallocate the queue if it is non-empty
{
Cell *link = last;
while (link != NULL) // Until we reach the end of the queue, keep deleting each link
{
pop();
}
first = NULL;
last = NULL;
}
else // If the queue is already empty, let the user know
{
cout << "Cannot call empty. The list is already empty.\n";
}
}
template<class Type>
Queue<Type>& Queue<Type>::operator=(Queue<Type>& queue) // Overload "=" so that it performs a deep copy of a Queue object
{
if (!queue.isEmpty())
{
empty(); // Tada, no more error
Cell* rhs = queue.first;
while (rhs != NULL)
{
push(rhs->data);
rhs = rhs->next;
}
}
return *this;
}
template<class Type>
Queue<Type>::~Queue() // Deconstructor that deallocates all of the memory used by the queue.
{
empty();
}
This has nothing to do with template.
If you declare any constructor for your class, the compiler synthesized default constructor(i.e. the one that takes no arg) is deleted.
You have to define Queue() yourself.
BTW, a using directive in the global scope is not a good idea.
I guess you define a queue without parameter, like
Queue<char> quCh;
If you want to do this, you must define a constructor without parameter.
Queue();
or you must define your queue like this:
Queue<char> quCh('a');