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.
Related
Quick question: how can I initialize this? The syntax isn't working.
#include <iostream>
using namespace std;
template<typename TYPE>
class Heap1 {
class Node {
public:
friend Heap1;
private:
TYPE elt;
Node *child;
}; // Node
};
int main() {
Heap1<int>.Node var;
return 0; }
I'm reading this answer but the syntax isn't too clear to me: Creating instance of nested class
Heap1<int>.Node var;
The syntax isn't working
Try
Heap1<int>::Node var;
But actually, you can't. Heap1<>::Node is private and thus inaccessible from the outside world.
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().)
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
Here is my problem.
I have a Linked List class as follows:
#include <iostream>
#include "List.h"
template <class Elem>
class LList : public List<Elem> { //List is a virtual base class
protected:
Node <Elem> *head;
Node <Elem> *fence;
Node <Elem> *tail;
int leftCount;
int rightCount;
void init();
void removeAll();
public:
LList();
~LList();
//Rest of methods overridden from List class
//////
};
Then I have a class called SortedLList which inherits from LList as follows:
#include "LinkedList.h"
#include "Helper.h"
template <class Elem>
class SortedLList : public LList<Elem> {
protected:
Helper *helper;
public:
SortedLList();
~SortedLList();
bool insert(const Elem&); //Override insertion method from LList class
};
In the implementation of SortedLList (SortledLList.cpp):
#include "SortedLList.h"
template <class Elem>
SortedLList<Elem>::~SortedLList() {
removeAll();
}
template <class Elem>
bool SortedLList<Elem>::insert(const Elem &_e) {
fence = head;
//Rest of Code..
}
I am having a compiler error that says : Use of undeclared identifier removeAll(). Same thing for fence and head pointers are not being recognized. What did I do wrong?
Thank You.
Because your class is a template there are certain issues that can happen to confuse the compiler. You may think your code is straight forward and easy to understand, and in this case it is. Older compilers used to do their best to guess and compile this code.
However, newer compilers are more strict and fail on all versions of this type of code, in order to prevent programmers from relying on it.
What you need to do is use the this pointer when calling base class functions. That makes the call unambiguous and clear. That would look like this->removeAll().
Another option would be to use a full name qualification like LList<Elem>::removeAll(). I prefer using this because it is easier to read.
Related to C++ Basics.
I am creating a singly linked list.
class Linked_List
{
public: Linked_List();
~Linked_List();
//Member Functions
struct node* getHead(void);
private:
struct node{
int d;
struct node* next;
}*head;
};
struct node (Linked_List::*getHead)(void)
{
return head;
}
I am getting this error:
"error C2470: 'getHead' : looks like a function definition, but there is no parameter list; skipping apparent body".
I tried to search in google but of no use. Any suggestions plz.
You don't need a pointer to member function, you just want to provide a definition for that function:
Linked_List::node* Linked_List::getHead()
{
return head;
}
Also notice, that the struct keyword is unnecessary in the function definition, while you have to qualify the name of the struct node with the name of the class in the scope of which it is defined.
Also, the void keyword to specify an empty argument list is unnecessary. I therefore suggest you to rewrite the class definition as follows:
class Linked_List
{
private:
struct node
{
int d;
struct node* next;
};
node *head;
public:
Linked_List();
~Linked_List();
node* getHead();
};