I am trying to create a binary search tree of Player objects. I have previously defined the Player class. However, when I go to include the player object in the structure for each node of the BST, I get an error that Player is undefined even though I thought I had set up the includes properly.Is there any way for me to go about this without having to rethink my implementation?
I simplified the code a little to demonstrate:
BST header:
class Player;
class BinarySearchTree{
private:
struct Node {
Player info;
Node* left;
Node* right;
};
Node *root;
void Insert(Node*& tree, Player p);
void PrintTree(Node* tree, std::ostream& out);
};
BST.cpp
#include "Player.h"
#include "BinarySearchTree.h"
//all methods implemented afterwards
Player.h
class Player{
private:
std::string* name = new std::string;
int* score = new int;
public:
//....
};
Player.cpp
#include "Player.h"
//...
Compiler needs to see the definition of class Player in header file (BST.h) which you have provided in BST.cpp ( by including "Player.h").
So, BST header should be:-
#include "Player.h" <<<include this file
class Player; <<<remove this forward declaration
class BinarySearchTree
{
private:
struct Node {
Player info;
Related
I have a Binary Search Tree class that uses templates. So I can create a BST of type Faculty, but is there a way to include a BST within the Faculty class?
I want to hold a tree of integers as a member variable within the Faculty class.
So I would have a Faculty tree, and each node (of type faculty) would have its own Integer tree.
Here's the Faculty class in which I am trying to add a BST member variable. From what I read elsewhere the problem lies in trying to #include the BST.h file, since I already included the Faculty file in the BST file.
#ifndef Faculty_H
#define Faculty_H
#include "Person.h"
#include "BST.h"
using namespace std;
class Faculty : public Person
{
public:
Faculty();
Faculty(int new_ID);
~Faculty();
friend ostream& operator<<(ostream& os, Faculty& f);
private:
BST<int> advisees;
};
#endif //Faculty_H
Here's my error:
./Faculty.h:36:2: error: unknown type name 'BST'
BST advisees;
AND HERE'S THE BST.H
#ifndef BST_H
#define BST_H
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include "tree_node.h"
#include "tree_node.cpp"
#include "Person.h"
#include "Student.h"
#include "Faculty.h"
using namespace std;
template <class T>
class BST
{
public:
BST();
~BST();
void insert(T k);
tree_node<T>* find(T k);
bool contains(T k);
bool delete_node(T k);
tree_node<T> *get_min();
tree_node<T> *get_max();
tree_node<T> *get_root();
bool is_empty();
int get_size();
void print_tree(tree_node<T> *node);
tree_node<T>* get_successor(tree_node<T> *d);
private:
tree_node<T> *root;
unsigned int size;
};
#endif //BST_H
You have an #include loop; BST.h includes Faculty.h, which includes BST.h. This can cause a lot of problems, including what you're seeing now; depending on include ordering, the definition of BST may not be available when Faculty needs to use it.
BST.h doesn't need to know anything about Person, Student, or Faculty. Removing those includes should resolve the current problem.
Still fairly new with C++ and trying to kick it up a notch here. I would like to build a Heap class, with a nested Node class, and add a heap sort aspect to the Heap class. I have done something similar with Java, but I am getting stuck trying to define the nested class in the .cpp file.
#pragma once
#ifndef HEAP_H
#define HEAP_H
template <class T>
class Heap
{
public:
class Node
{
public:
Node(T);
T data;
private:
Node *parent;
Node *left_child;
Node *right_child;
boolean is_root;
};
Heap(T*, int);
sort_it();
private:
T *unsorted_list
Node root;
void build_heap();
void add_node(Node);
void swap_root();
void trickle_down();
void heap_sort();
};
#endif
Now when I go to define my nested class in the .cpp file I cannot simply...
#include "stdafx.h"
#include "Heap.h"
#include <iostream>
//Defining Heap Constructor
Heap::Heap(T* incoming_array, int _size)
{
unsorted_list = incoming_array;
size = _size;
}
//Defining Node Constructor
Heap::Node(T _data)
{
data = _data;
left_child = right_child = parent = Null;
is_root = false;
}
I am not sure if my problem is how I am incorporating the template, or if my syntax for defining the inner class is wrong. Both Generic Programming and Nested Classes are unfamiliar to me in C++
If you use any generic type in nested class you have to specify the template.
template<class T>
class Node
To define the template class constructor outside the class,
template<typename T>
Node<T>::Node(T _data)
Declare the member as follows,
Node<T> root
I have a queue.h file like the following.
Is it possible that I can access the head pointer of the queue from Main?
If yes, what should I do in main?
Since the head pointer is a class pointer, and its type is a protected nested class, I don't think I can access it from main.
Therefore, I try to create a function getHead() as public member. However, another problem comes, it is I am using template class. Please guide me how to solve this problem.
my header file:
#include <iostream>
#include <iomanip>
using namespace std;
class PCB
{
public:
int PID;
string fileName;
};
template<class T>
class myQueue
{
protected:
class Node
{
public:
T info;
Node *next;
Node *prev;
};
Node *head;
Node *tail;
int count;
public:
void getHead(Node **tempHead);
};
template<class T>
void myQueue<T>::getHead(Node **tempHead)
{
*tempHead = head;
}
#endif
my main is:
#include "myQueue.h"
#include <iostream>
int main()
{
myQueue<PCB> queue;
//How can I access the Head pointer of my Queue here?
//queue.getHead(&tempHead);
return 0;
}
To acess myQueue::Node from outside the class you need to rewrite your getter function a bit:
template<class T>
myQueue<T>::Node* myQueue<T>::getHead()
{
return head;
}
Then you can use it in main() like this
auto head = queue.getHead();
Note that the usage of auto is important in this case. You still cannot declare any variable of type myQueue<T>::Node or myQueue<T>::Node** outside of myQueue<T>, but you can use auto variables to hold these types.
EDIT: Created a minimal VS solution to make it easier to reproduce the error: https://www.dropbox.com/s/pk0t8t2xykjmtc5/test%20cereal%20this.zip (add cereal in includes instead of $(LIBSROOT) where I have it).
I get 2 errors stating that I have no default constructor:
error C2139: 'Node' : an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_constructible'
error C2338: Trying to serialize a an object with no default constructor.
<path>\cereal\details\traits.hpp line 1248
But I think the classes' default constructors should be fine. If I comment out the serialization of Node class variables I get the same errors but with Part class variables.
I have the following code structure (some parts like include guards or unrelated code were omitted, I can of course provide the whole thing if needed, but I wanted to keep it as short as possible):
Shape.h:
#include <cereal/types/memory.hpp>
#include <cereal/types/vector.hpp>
#include <string>
class Part;
typedef std::shared_ptr<Part> PartPtr;
class Node;
typedef std::shared_ptr<Node> NodePtr;
class Shape {
private:
std::vector<PartPtr> parts;
NodePtr root;
std::vector<std::vector<NodePtr>> levels;
public:
Shape();
Shape(std::string fileName);
template <class Archive>
void serialize(Archive & ar) {
ar(parts, levels, root);
}
};
Shape.cpp:
#include "Shape.h"
#include "Node.h"
#include "Part.h"
Shape::Shape() {
}
Shape::Shape(std::string fileName) {
// omitted code
}
Node.h:
#include "PointCloud.h"
#include <cereal/types/vector.hpp>
#include <cereal/types/memory.hpp>
class Part;
typedef std::shared_ptr<Part> PartPtr;
class Node;
typedef std::shared_ptr<Node> NodePtr;
class Node : public std::enable_shared_from_this<Node> {
private:
std::vector<PartPtr> parts;
NodePtr parent;
PointCloud pointCloud;
public:
Node();
Node(std::vector<PartPtr> parts, NodePtr parent);
template <class Archive>
void serialize(Archive & ar) {
ar(parts, parent, pointCloud);
}
};
Node.cpp:
#include "Node.h"
#include "Part.h"
Node::Node() {
}
Node::Node(std::vector<PartPtr> parts, NodePtr parent) : parts(parts), parent(parent) {
// code omitted
}
Part.h:
#include "PointCloud.h"
#include <cereal/types/memory.hpp>
#include <cereal/types/vector.hpp>
class Contact;
class Part;
typedef std::shared_ptr<Contact> ContactPtr;
typedef std::shared_ptr<Part> PartPtr;
class Part : public std::enable_shared_from_this<Part> {
private:
PointCloud pointCloud;
std::vector<ContactPtr> contacts;
public:
Part();
Part(double diameter);
template <class Archive>
void serialize(Archive & ar) {
ar(pointCloud, contacts);
}
};
Part.cpp:
#include "Part.h"
#include "Contact.h"
Part::Part() {
}
Part::Part(double diameter) {
// omitted code
}
Contact class contains a PartPtr as a member variable, PointCloud contains just a bunch of Eigen::Matrix data (should probably be a smart pointer too, to speed up the code, but that shouldn't be important for this problem).
Any suggestions how to fix this? Or could it possibly be a bug? I am using VS2013, which could be the cause as well.
You have only declared class Part;and class Node; in Shape.h. To use a class as template parameter you the compiler also needs a definition of that class.
Include Part.h and Node.h in your Shape.h file. Also include Part.hin Node.h and Contact.h in Part.h.
And put some include guards into the headers:
#ifndef SOME_UNIQUE_IDENTIFIER
#define SOME_UNIQUE_IDENTIFIER
// content of your header goes here
#endif
SOME_UNIQUE_IDENTIFIER would usially be something like NODE_H in your Node.h file and PART_H in your Part.h file.
i wana create a bst tree in c++ but i have a syntax error in this code :
#pragma once
#include "BSTNode.h"
using namespace std;
class BST
{
private:
BSTNode* root;
public:
BST(void);
bool insert(int );
int search(int);
~BST(void);
};
and BSTNode is:
#pragma once
#include "BST.h"
class BST;
class BSTNode
{
friend class BST;
private:
int data;
BSTNode * LeftChild, *RightChild;
public:
BSTNode(void);
int getData();
~BSTNode(void);
};
my error is:
Error 1 error C2143: syntax error : missing ';' before '*'
i think dont have error .please help me!
You have a circular include, with 2 files, and because of the #Pragma once, both files are only included once, and therefore BSTNode is parsed first and includes BST, but then BST is not including BSTNode anymore (because it is pragma once).
this leads to BST not knowing what a BSTNode is, solution would be:
Removing the include and forward declaring the class like such:
#pragma once
using namespace std;
class BSTNode; //Forward declare class so that BST knows BSTNode (move include to .cpp file)
class BST
{
private:
BSTNode* root;
public:
BST(void);
bool insert(int );
int search(int);
~BST(void);
};
Example of main function:
int main( int argc, const char* argv[] )
{
printf( "\nHello World\n\n" );
}
declaration of ‘BSTNode’ with no type. You need to declare the class before using it