Problems with understanding scope - c++

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().)

Related

Syntax for creating instance of templated nested class?

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.

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.

class pointer error: does not have a type

one of my class variables that is a pointer to another class is giving a error of no type. can figure it out after a few days thinking about it, anyone see a mistake? also for some reason the line
r->setright(NULL)
crashes the program
#include "gridnode.h"
#include "grid.h"
#include "basenode.h"
class grid
{
public:
grid();
void print();
protected:
gridnode *head;//error of no type here
};
#endif // GRID_H
and my node class
class gridnode:public basenode
{
public:
char mark;
gridnode *l, *r, *u, *d;
gridnode(){
l->setleft(
r->setright(NULL);//crashes
u->setup(NULL);
d->setdown(NULL);
}
//set and get functions
protected:
};
any help greatly apreciated.
one of my class variables that is a pointer to another class is giving a error of no type.
gridnode has not been defined yet when you try to use it. That can happen if you have circular references in your .h files, for instance. Since you are just declaring a pointer, you can use a forward declaration instead:
#ifndef GRID_H
#define GRID_H
// there should be no include of grid.h here
// move the gridnode.h and basenode.h includes to grid.cpp instead
class gridnode; // forward declaration
class grid
{
public:
grid();
void print();
protected:
gridnode *head;
};
#endif // GRID_H
also for some reason the line
r->setright(NULL)
crashes the program
Inside the gridnode constructor, you are calling methods on l, r, u, and d pointers that have not been initialized to point at anything. You are invoking undefined behavior by calling methods on them. You probably meant to set the pointers themselves to NULL instead, eg:
gridnode(){
l = NULL;
r = NULL;
u = NULL;
d = NULL;
}
Or:
gridnode() :
l(NULL),
r(NULL),
u(NULL),
d(NULL)
{
}

How to define a pointer to a class member function

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();
};

syntax error in 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