list.hpp
template <typename Data>
class List{
private:
struct Node* Head;
protected:
using LinearContainer<Data>::size;
struct Node
{
Data Elements;
Node* Nxt;
// Specific constructors
Node(Data);
};
public:
List() = default;
}
list.cpp
template<typename Data>
List<Data>::Node::Node(Data Elemento) {
List<Data>::Head.Elements = Elemento;
size = 1;
}
ERROR
error: invalid use of non-static data member 'Head'
List<Data>::Head.Elements = Elemento;
~~~~~~~~~~~~^~~~
1 error generated.
What’s my mistake, can you please tell me? I have not put the "main" as I know for certain that it has nothing to do while "size" is inherited from a higher class
Related
This question already has answers here:
How to use a member variable as a default argument in C++?
(4 answers)
Closed 2 years ago.
i have two classes Node and AvlTree , i will make other methods of AvlTree later but i can't access root data member of AvlTree in it's own class , how can i access root in AvlTree class inOrder method.
My code is following here
class Node {
public:
int key;
Node* left;
Node* right;
int height;
Node(int key) : key(key) , left(nullptr) , right(nullptr) , height(1) {};
};
class AvlTree {
private:
Node* root;
public:
AvlTree() : root(nullptr) {};
int height(Node* ptr) {
}
int getBalanceFactor(Node* ptr) {
}
void inOrder(Node* itr = root) { // <--- i get an error here
}
};
I tried with this->root but that also does't work , what i am do wrong here , also can i not access like this in it's own class.
I got an error like
09_avl_tree.cpp:36:34: error: invalid use of non-static data member ‘AvlTree::root’
36 | void inOrder(Node* itr = root) {
| ^~~~
09_avl_tree.cpp:15:15: note: declared here
15 | Node* root;
| ^~~~
I don't want to make root as static data member. because i want multiple instances of AvlTree.
The short answer, as the compiler is telling you, is that you can't do that as a default value of an argument.
The simplest approach would be to overload the inOrder() function, for example (within the definition of AvlTree)
void inOrder(Node *itr)
{
// whatever
}
void inOrder()
{
inOrder(root);
}
Also, unrelated to your question, the shadowing of member names in Nodes constructor (e.g. an argument named key used to initialise a member named key) is not a good idea, since it is easy to mislead human readers about what the code does. It is therefore often considered preferable to name the argument differently from the member.
I am implementing a binary tree class that is almost identical to this one. However, in my task, the node struct must be a templated structure. Therefore I changed struct node to:
template <typename T>
class node {
public:
T data;
node<T> *left, *right;
}
so far so good, until I added a node instance to btree as a member variable:
class btree {
// ......
private:
template <typename T>
node<T> *root = NULL; // error
}
error message says
C3857: multiple template parameter lists are not allowed.
I tried to move root = NULL to btree's default constructor, does not work either.
You cannot have a templated variable declaration. There would be no way to specify the type to use for the variable. You can either make btree a template and use that type for the node
template<typename T>
class btree {
// ......
private:
node<T> *root = NULL; // error
}
or specify what type of node you want in btree
class btree {
// ......
private:
node<some_type> *root = NULL; // error
}
The template declaration is:
template <typename DataType>
class BST
and the error that I keep getting is
bst.h(101) : see reference to class template instantiation 'BST::BinNode' being compiled
bst.h(183) : see reference to class template instantiation 'BST' being compiled
I think my syntax may be wrong, but I'm not sure what about it is. Could somebody push me into the right direction. I just cannot get it to compile. Class BinNode is a private class of the main class BST. The line the error is referring too is DataType BinNode::treeheight(BinNode * p)
private:
/***** Node class *****/
class BinNode
{
public:
DataType data;
BinNode * left;
BinNode * right;
DataType treeheight(BinNode * p);
template <typename DataType>
DataType BinNode::treeheight(BinNode * p)
{
if(p != 0)
{
int heightl = treeheight(p->left);
int heightr = treeheight(p->right);
}
if(heightl > heightr)
return heightl;
else
return height r;
}
When defining the member function (outside the class, that is), you need to qualify the member function definition as Node<DataType>::BinNode::treeheight(…) {…} since BinNode is a nested class of Node.
I am not very clear about this code
outer is a class and the inner a struct, can anyone help me explain it?
class Stack {
struct Link {
void* data;
Link* next;
Link(void* dat, Link* nxt):
data(dat),next(nxt) {}
}* head;
public:
Stack():head(0) {}
~Stack() {
require(head==0,"Stack not empty");
}
void push(void* dat) {
head = new Link( dat, head );
}
void peek() const {
return head ? head->data : 0;
}
void* pop() {
if(head == 0) return 0;
void* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;
}
};
my question is focus on the first few lines
class Stack {
struct Link {
void* data;
Link* next;
Link(void* dat, Link* nxt):
data(dat),next(nxt) {}
}* head;
what the relation between class Stack and struct Link?
Link is declared inside Stack, and since it's private by default, it can't be used outside the class.
Also, Stack has a member head which is of type Link*.
The only difference between class and struct is the default access level - public for struct and private for class, so don't let "struct declared inside a class" confuse you. Other than the access level, it's the same as "class declared inside a class" or "struct declared inside a struct".
Class Stack and struct Link are nested.
Note that nested classes have certain limitations regarding accessing of elements of nested and the enclosing class.
Since, Link is declared under private access specifier in class Struct it cannot be accessed outside the class Struct.
Struct Link is inner-declared in the class (the correct term is nested). It can be accessed only in the class (due to its default private accessor level). The struct could have been referred with Stack::Link if it was declared public.
The reason for such declaration in this case is that only the inner methods of the class need to use the links and struct is providing better code organization. However as long as no other class needs to access the struct such declaration provides better encapsulation.
I'm new to C++ and I keep getting this error message in the following class:
class LinkedList {
class Node *head;
class Node {
Student *student;
Node *next;
Node *prev;
public:
Node(Student *n_student, Node *n_next, Node *n_prev);
~Node();
Student *getStudent() const;
Node *getNext() const;
Node *getPrev() const;
};
public:
LinkedList();
~LinkedList();
void printList();
};
The method that causes the error:
void LinkedList::printList() {
using namespace std;
class Node *p_n;
p_n = head; // ERROR!
while (p_n) {
cout << '[' << (*(*p_n).getStudent()).getId() << ']' << endl;
p_n = (*p_n).getNext();
}
}
The error message I'm getting is
error: cannot convert 'Node*' to
'LinkedList::Node*' in assignment
I've tried casting Node to LinkedList::Node but I keep getting the same message. I'm compiling it in Xcode, not sure if that causes the problem.
Any idea how to fix this?
Change this:
class Node *head;
Into this:
Node *head;
When you declare a field of a certain class inside a class, you don't need the class keyword. Just the type name and its corresponding identifier. Like this:
Node *n;
LinkedList *l;
No class keyword. class keyword is only used when you actually declare/define a class.