Setting pointer to null creates runtime error - c++

I've been looking around and haven't seen a question with a problem as specific as this one.
I'm trying to create a linked list in this program, but I get a runtime error and no build errors when I run it.
Main:
#include <iostream>
#include "LinkedListInterface.h"
#include "LinkedList.h"
#include <fstream>
int main(int argc, char * argv[])
{
ifstream in(argv[1]);
LinkedList<int> myIntList;
}
LinkedList class:
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include <string>
#include <sstream>
using namespace std;
template<typename T>
class LinkedList : public LinkedListInterface<T>
{
public:
LinkedList()
{
head->next = NULL;
}
private:
struct Node
{
T data;
struct Node *next;
};
Node *head;
};
I have assured that the problem is not with an out-of-bounds error on argv[1], and removing any of the statements in LinkedList() or main() makes the program run smoothly.

You have to construct head before invoking head->next = NULL. But this would mean that there is an empty node in the list when you create it.
template<typename T>
class LinkedList : public LinkedListInterface<T>
{
public:
LinkedList()
{
// At least do this
head = new Node();
head->next = NULL;
// The best idea is to do below:
// head = null;
}
private:
struct Node
{
T data;
struct Node *next;
};
Node *head;
};

Related

C2011 'Node':'class' type redefinition

I am implementing bptree using c++. I am am stuck in the initial step of node creation. Keep getting "C2011 'Node':'class' type redefinition" error. I found some suggestions online to remove class key word from cpp file. But when I remove class keyword I get lots of other errors. here is my code for Node.cpp:
#include "Node.h"
#include <cstdio>
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Node {
bool leaf;
Node** kids;
map<int, string> value;
int keyCount;//number of current keys in the node
//constructor;
Node::Node(int order) {
this->value = {};
this->kids = new Node *[order + 1];
this->leaf = true;
this->keyCount = 0;
for (int i = 0; i < (order + 1); i++) {
this->kids[i] = NULL;
}
}
};
and Node.h file is as following:
#pragma once
#ifndef NODE_HEADER
#define NODE_HEADER
class Node {
public:
Node(int order) {};
};
#endif
How can I fix this?
Problem
In C++, headers are simply pasted into the body when you #include. So now the compiler sees:
class Node {
public:
Node(int order) {};
};
// stuff from system headers omitted for brevity
using namespace std;
class Node {
bool leaf;
//...
};
There are two problems here:
compiler sees class Node twice with different bodies.
Node::Node is defined twice (first time empty {}).
Solution
The header should include class declaration:
#include <map>
using namespace std;
class Node {
bool leaf;
Node** kids;
map<int, string> value;
int keyCount;//number of current keys in the node
//constructor;
Node(int order);
};
Note that the constructor has no body here. It's just a declaration. Because it uses map you need to include <map> and add using namespace before the declaration.
After that don't put class Node again in the .cpp or .cc file. Only put the method implementations at the top level:
Node::Node(int order) {
// ...
}

Error: does not name a type (c++)

I'm having a bit of trouble trying to create a linked list that uses a LinkedList and Node class. In the List class, how do I create the head, curr and temp objects? I thought I could just initialise them as objects, which would then call the default Node() constructor and assign them a data and pointer variable. But I am getting the error: ‘Node’ does not name a type and ‘head’ was not declared in this scope for the objects head, curr and temp.
Here's my code:
LinkedList.cpp:
#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
#include "Node.h"
using namespace std;
LinkedList::LinkedList() {
head = NULL;
curr = NULL;
temp = NULL;
cout << "Blank list created." << endl;
}
LinkedList::LinkedList(value_type addData) {
Node n(addData);
}
LinkedList.h:
class LinkedList {
public:
typedef std::string value_type;
LinkedList();
LinkedList(value_type addData);
private:
Node head;
Node curr;
Node temp;
};
Node.cpp:
#include <iostream>
#include <cstdlib>
#include "Node.h"
using namespace std;
Node::Node() {
nodePtr n = new node;
n->next = NULL;
n->data = NULL;
}
Node::Node(value_type addData) {
nodePtr n = new node;
n->next = NULL;
n->data = addData;
}
Node.h:
class Node {
public:
typedef std::string value_type;
Node();
Node(value_type addData);
private:
struct node {
value_type data;
node* next;
};
typedef struct node* nodePtr;
};
Any help would be greatly appreciated, thanks guys!
Look at what the compiler does with LinkedList.c. It starts with this:
...
#include "LinkedList.h"
#include "Node.h"
...
Then it evaluates the #include directives, and pulls LinkedList.h and Node.h into the source:
...
class LinkedList {
...
Node head;
Node curr;
Node temp;
};
class Node {
...
};
Then it tries to compile this, gets as far as "Node head;" (when it has not yet reached the declaration of the class Node), and complains that it doesn't know what you're talking about.
The file that declares class LinkedList needs to know what a Node is. Put #include "Node.h" near the top of LinkedList.h (and add header guards to Node.h) and you won't have this problem.

Error: 'object' was not declared in this scope (C++)

I'm trying to write a linked list of Node objects, where each Node contains: a string - data, and a pointer - next. To manage the list (e.g: add/remove nodes), I have Node objects: head, curr and temp. I'm trying to work out how to link up each node to the previous one when new data is added to the list, but I am having trouble doing so. The error is occuring in Node.cpp when I am trying to link the new node n to the curr nodes next pointer.
LinkedList.cpp:
#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
#include "Node.h"
using namespace std;
LinkedList::LinkedList(value_type setData) {
head.setData(setData);
cout << head.getData() << endl;
}
void LinkedList::addNode(value_type addData) {
Node n;
n.setData(addData);
if (head.getData() != "") {
curr = head;
while(curr.getNext() != NULL) {
//Still writing this..
}
curr.setNext(n); //Is this correct?
}
else {
head = n;
}
}
LinkedList.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
class LinkedList {
public:
typedef std::string value_type;
LinkedList();
LinkedList(value_type setData);
void addNode(value_type addData);
private:
Node head;
Node curr;
Node temp;
};
#endif
Node.cpp:
#include <iostream>
#include <cstdlib>
#include "Node.h"
using namespace std;
Node::Node() {
data = "";
next = NULL;
}
void Node::setData(value_type setData) {
data = setData;
}
string Node::getData() {
return data;
}
void setNext(Node n) {
next = n; //'next' was not declared in this scope error.
//Does this assignment statement even work in this scenario?
}
Node * Node::getNext() {
return next;
}
Node.h:
#ifndef NODE_H
#define NODE_H
class Node {
private:
typedef std::string value_type;
value_type data;
Node* next;
public:
Node();
void setData(value_type setData);
value_type getData();
void setNext(Node n);
Node * getNext();
};
#endif
Any help would be greatly appreciated, thanks guys.
You've got this line:
void setNext(Node n) {
You probably want this instead:
void Node::setNext(Node n) {
The function void setNext(Node n); is declared in class Node. So in Node.cpp, you must define the function with void Node::setNext(Node n);. The symbol :: will show the function's scope.

Link list with a vector pointer

I'm trying to build a tree for my data structure course using struct of vectors to connect the nodes.
here is my code.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct node
{
vector<node> *next;
string val;
int tagid;
};
int main()
{
string operation;
node *head=new node;
head->next->resize(1);
return 0;
}
Now I tried to modify the first element's pointer with this code
head->next[0]=NULL;
The compiler gives me the error no match for ‘operator=’. How can I write it correct to be able to modify it's elements?
Following #Zaiborg comments, this works for me :
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct node {
vector<node*> next;
string val;
int tagid;
};
int main()
{
string operation;
node *head = new node;
head->next.resize(1);
head->next[0] = NULL;
return 0;
}
Compile with : g++ -Wall gives you no warning and no errors at the compilation.

Double Linked List undefined exception error [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
So, i am trying to do a program to generalize adding,deleting, showing a double linked list. but i have encountered a problem at the addition part. When compiling it i encounter " undefined reference to insertNodeBeggining(List*, void*)". What is the problem?
main.cpp
#include <iostream>
#include <cstring>
#include "methods.h"
using namespace std;
int main()
{
List *head=createList();
void *p=NULL;
insertNodeBeggining(head,p);
return 0;
}
methods.cpp
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstring>
#include <math.h>
using namespace std;
typedef struct nodeType{
nodeType *next;
nodeType *prev;
void* data;
} NodeT;
typedef struct Lists{
NodeT *first;
NodeT *last;
} List;
List *createList()
{
return (List*)malloc(sizeof(List));
}
void insertNodeBeggining(List *head, void *value)
{
NodeT *nou=(NodeT*)malloc(sizeof(NodeT));
nou->data=value;
if(head->first==NULL)
{
head->first=nou;
head->last=nou;
nou->next=NULL;
nou->prev=NULL;
}
else
{
nou->next=head->first;
head->first->prev=nou;
nou->prev=NULL;
head->first=nou;
}
}
methods.h
#ifndef METHODS_H_INCLUDED
#define METHODS_H_INCLUDED
typedef struct NodeT;
typedef struct List;
List *createList();
void insertNodeBeggining(List *head, void *value);
#endif // METHODS_H_INCLUDED
It seems that the problem is that the compiler considers two names List, one in the header and other in methods.cpp, as two different names.
The typedef in the header is invalid though the compiler may not issue an error
typedef struct List;
You should exclude these definitions
typedef struct nodeType{
nodeType *next;
nodeType *prev;
void* data;
} NodeT;
typedef struct Lists{
NodeT *first;
NodeT *last;
}List;
from metods.cpp and include them in the header instead of your typedef(s).
methods.cpp has to contain this new header included.
Basically, an incomplete type can't be completed via a typedef.
Hence your typedef-ed List in the implementation file is not considered to be a definition of the earlied forward-declared incomplete List.
To fix that, replace the C style
typedef struct Lists{
NodeT *first;
NodeT *last;
} List;
with C++ style
struct List
{
NodeT* first;
NodeT* last;
};
Disclaimer: I haven't run this through a compiler.