C++ struct was not declared this scope - c++

Using struct like linked list creates problem.
When I declare 2 values (my struct),
it returns error:
this VALUE_NAME was not declared this scope
This is part of the source.
struct _node
{
int val;
_node* prev;
_node* next;
};
typedef struct _node node;
node* HEAD;
void deleteALLNode()
{
node* node = HEAD;
node* _tmp;
//
// here _tmp value return error
//
}

You cannot name your variable with same name, as type (you can one time maybe, but than you cannot use the type without compilation error anymore). Just change name of node* node = HEAD; variable to something else, like node* pNode = HEAD;

If it's C++, you can use _node directly, no need to typedef. typedef a struct is C style.
struct _node
{
int val;
_node* prev;
_node* next;
};
_node* HEAD;
void deleteALLNode()
{
_node* node = HEAD;
_node* _tmp;
}

Related

How to use of a self reference type and using alias inside C++ class

Here is a simple code that I tried to use a self-reference type and using alias at the same time.
#include <iostream>
class List {
private:
struct node {
int data;
struct node* next;
node(const int& d=0, struct node* n=nullptr) {
data = d; next = n;
}
~node() {};
};
using pNode = struct node*;
pNode head;
public:
List();
~List();
void print() const { std::cout << head->data; }
};
List::List() {
head = new node{55};
}
int main() {
List *a = new List;
a->print();
}
This above works fine. However, I'd rather start the code as shown below:
class List {
private:
using pNode = struct node*;
struct node {
int data;
pNode next;
...
I'd like to place using pNode = struct node* before the struct node definition such that I can use it inside struct node definition as well. I believe that this style of code works fine if I don't use class.
Don't hide pointer semantics in an alias. It's the one "never" advice I always get behind.
And if you agree to only ever use node* in your code, then you can just write
struct node {
int data;
node* next;
// ..
};
C++ introduces a type named node with struct node, unlike C. So we can use natural syntax.
To use the latter you need to forward declare the struct node like so:
struct node;
using pNode = node*;
struct node {
int data;
pNode next;
};

Linked List in C

Heres a snippet from the code I am trying to complete for building a linked list. for some reason I keep getting the error "error: expected ‘;’, identifier or ‘(’ before ‘struct’ " when trying to compile the code. Can someone help me out.
struct node;
struct node* buildList(int x);
void push(struct node** headRef, int data);
int findLen(struct node** headRef);
struct node{
int data;
struct node* next;
}
struct node* buildList(int x){
struct node* head = NULL;
head = malloc(sizeof(struct node));
head->data = x;
head->next = NULL;
return head;
}
Try putting a semicolon after the struct declaration
struct node{
int data;
struct node* next;
};

Creating Struct node on c++

I having some doubt with struct.
struct node
{
int data;
node* next;
}node; <---- what does this actually do?
Thanks.
add on::
Hi, trying to fix this error..
Line 11: error: expected constructor, destructor, or type conversion before '*' token
compilation terminated due to -Wfatal-errors.
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
}node;
node* nodeNew(int newData, node* newNext) // line 11
{
node* n= new node;
n->data= newData;
n->next= newNext;
return n;
}
void listPrint(node* p)
{
while( p!=NULL )
{
cout << p->data << " "; p= p->next;
}
}
int main()
{
}
Is happens when i add that "node" in the struct.
The final line:
}node;
creates a variable with the type struct node, named node. It's equivalent to:
struct node {
int data;
node* next;
};
struct node node;
EDIT: In response to your edit, the line:
node* nodeNew(int newData, node* newNext)
is erroring because node isn't a type. Either change it to:
struct node* nodeNew(int newData, struct node* newNext)
or change the structure declaration to:
typedef struct node node;
struct node {
int data;
node* next;
};
To be exact, it creates an object from given struct in given scope. Word 'variable' is a too generic term.

Linked List not compiling

This code is not compiling in my system; I'm using Eclipse.
// Linked list head
template<class T>
struct Node
{
// constructor
Node(const T& a) : pNext(NULL), data(a) {}
Node* pNext; //link
T data;
}; // end of header
// List code
#include <iostream>
#include "LinkedList.h"
template<class T>
class linkedList
{
public:
typedef Node<T> Node;
//constructor creates empty list
linkedList() : pHead(NULL), size(0) {}
~linkedList()
{
Node* pIter = pHead;
while(pIter != NULL)
{
Node* pNext = pIter->pNext;
delete pIter;
pIter = pNext;
}
}
void insert(const T& data)
{
Node* pInsert = new Node(data);
if(pHead == NULL)
{
pHead = pInsert;
}
else
{
pInsert->pNext = pHead;
pHead = pInsert;
}
}
private:
Node* pHead; // always points to head of list
unsigned int size; // stores number of elements in list
};
Here is the error message:
./LinkedList.cpp:14:18: error: declaration of 'typedef struct Node<T> linkedList<T>::Node'
../LinkedList.h:4:1: error: changes meaning of 'Node' from 'struct Node<T>'
make: *** [LinkedList.o] Error 1
The error is fairly clear: Don't reuse the name Node. Instead you can write something like this:
typedef Node<T> node_type;
Template names and type names share the same namespace in C++, so you cannot use the same name for two distinct entities, even though one is a template and the other a type.
(Somewhat tangentially, there is a fair amount of subtlety surrounding tag names both in C and C++; this article may be worth a read, and this and this.)

Linkedlist Node in C+

I am learning a book on data structures, and complied their node in linked list example, and I receive this error:
and Everything.cpp|7|error: expected unqualified-id before "int"|
and Everything.cpp|7|error: expected `)' before "int"|
||=== Build finished: 2 errors, 0 warnings ===|
The code for the node is:
typedef struct Node
{
struct Node(int data) //Compile suggest problem is here
{
this-> data = data;
previous = NULL;
next = NULL;
}
int data;
struct Node* previous;
struct Node* next;
} NODE;
I am not familiar with structs and I am using Code::blocks to compile. Does anyone know whats wrong?
The code sample is wrong. There should not be the keyword struct in front of the constructor declaration. It should be:
typedef struct Node
{
Node(int data) // No 'struct' here
{
this-> data = data;
previous = NULL;
next = NULL;
}
int data;
struct Node* previous;
struct Node* next;
} NODE;