syntax error in c++ - c++

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

Related

Invalid Use of template-name 'node' without an argument list

node.h
#ifndef node_h
#define node_h
using namespace std;
template<class Type>
struct node {
node (Type v) : next(nullptr),data(v){}
Type data;
node *next;
};
#endif
prioqueueS.cpp
#ifndef prioqueueS
#define prioqueueS
#include "node.h"
using namespace std;
template <class type>
class PrioQueueS {
private:
node *head;
node *tail;
};
#endif
This is of course not all of my prioqueue class, but it is the part that I am having issues with, I have looked around on her for a clear answer but haven't found it. This is my error message:
error: invalid use of template-name 'node' without an argument list
node *head;
If anyone could point me in the right direction for solving this problem that would very appreciated.
EDIT: I realized I forgot the
node<Type> head;
so I will probably just delete this
node is a templated type. To declare a node variable (even just a pointer to one), you need to specify a value for the template parameter. But you are not doing that, so that is what the compiler is complaining about:
#ifndef prioqueueS
#define prioqueueS
#include "node.h"
using namespace std;
template <class type>
class PrioQueueS {
private:
node *head; // <-- here
node *tail; // <-- here
};
#endif
Try this instead:
#ifndef prioqueueS
#define prioqueueS
#include "node.h"
using namespace std;
template <class type>
class PrioQueueS {
private:
node<type> *head; // <-- here
node<type> *tail; // <-- here
};
#endif

Problems with understanding scope

I|m having problems understanding the scope of a class or a function. This program is incomplete but I am not being able to use a function within the same class and then from a different class. For example: I get an error that says
"'selector' was not declared in this scope"
Can you help me figure out what's wrong? Thanks
#include <iostream>
using namespace std;
int main(void){
selector();
}
void selector(){
linkedList test;
/* block of code */
}
class linkedList{
Node *head;
public:
linkedList(){
head = NULL;
}
//other lines
};
class Node{
public:
int data;
Node * next;
}
I don't understand why you're talking about classes, but the scope of a function is from its declaration to the end of the file. Just swap the two functions in your code:
void selector() {
// linkedList test;
/* block of code */
}
int main() {
selector(); // selector is in scope here
}
(I'm not sure why you're doing int main(void) either. That's more of a C thing. A C++ function with no arguments looks like int main().)

Defining a Nested Class From Header File

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

How to access a protected/private nested class pointer from a member

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.

Include a class as member variable in another class (C++)

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;