When trying to run my codebase, I am having an error that says:
'Node': no appropriate default constructor available BST
I have an idea that this could be caused by BST.h:
#ifndef BST_H
#define BST_H
// using namespace std;
template <typename T>
class Node {
public:
Node(T n) : data(n), rlink(nullptr), llink(nullptr) {}
~Node() {}
private:
T data;
Node *rlink, *llink;
};
template <typename T>
class BST {
public:
BST();
void insert(T &p);
private:
Node<T> * root;
};
#endif
Or if not that then caused by this function which is part of BST.cpp:
template <typename T>
void BST<T>::insert(T &p) {
if (root != nullptr) {
}
else {
Node<T> *newNode = new Node<T>;
cout << "UPD" << endl;
}
}
The cout is there for test purposes. I am aware of the usage of namespace std; that is just there for quick-testing-purposes. That's not my priority, but this is. Please:
- Help me fix this very problem
- May I get your assistance and guidance as an answer on how I can fix this problem? I'm not sure how to fix it!
My intention is to do something like this:
Node *newNode = new Node;
newNode->data = new Packet(p);
but in a template version. How can I achieve this? I used to have the error "No default constructor for Packet" but that was fixed w/an edition of this:
Node(T n) : data(n), rlink(nullptr), llink(nullptr) {}
in BST.h. It used to be:
Node() : rlink(nullptr), llink(nullptr) {}
But then it introduced this error.
For reference purposes:
BST.cpp
#include "BST.h"
#include <iostream>
template <typename T>
BST<T>::BST() : root(nullptr) {}
template <typename T>
void BST<T>::insert(T &p) {
if (root != nullptr) {
}
else {
Node<T> *newNode = new Node<T>;
cout << "UPD" << endl;
}
}
Packet.h
#ifndef PACKET_H
#define PACKET_H
#include <string>
// using namespace std;
class Packet {
public:
Packet(int partId, string description, double price, int partCount) :
partId(partId), description(description), price(price), partCount(partCount) {}
int getPartId() const { return partId; }
string getDescription() const { return description; }
double getPrice() const { return price; }
int getPartCount() const { return partCount; }
private:
int partId;
string description;
double price;
int partCount;
};
#endif
Main.cpp
// using namespace std;
#include <iostream>
#include "BST.h"
#include "BST.cpp"
#include "Packet.h"
int main()
{
BST<Packet> test;
Packet one(1, "testPacket", 1, 1);
test.insert(one);
system("Pause");
}
Note: Commented out namespace std; but if you want to run the program, just uncomment. I just did it for testing purposes...