Using a struct from one header, within a struct of another - c++

I have a homework assignment where I need to create a binary tree, and within each node point to a linked list.
I have my linkedList program working from a previous assignment. However within my binary tree struct, I would like to access the struct from linked list.
Here's an example of what I am talking about.
BinaryTree.h
#ifndef BinaryTree_h
#define BinaryTree_h
#include <iostream>
using namespace std;
struct bnode {
bnode * lChild;
bnode * rChild;
string word;
lnode * lineList; // <--------- This is what I would like to accomplish
};
LinkedList.h
#ifndef LinkedList_h
#define LinkedList_h
#include <iostream>
using namespace std;
struct lnode {
lnode * prev;
int data;
void *pointerData;
lnode * next;
};

You have two choices:
add #include "LinkedList.h" to BinaryTree.h:
#ifndef BinaryTree_h
#define BinaryTree_h
#include <iostream>
#include "LinkedList.h" // <-- here
struct bnode {
bnode * lChild;
bnode * rChild;
std::string word;
lnode * lineList;
};
#endif
since the lineList member is just a pointer, you can (and should) forward declare the lnode type without having to fully define it:
#ifndef BinaryTree_h
#define BinaryTree_h
#include <iostream>
struct lnode; // <-- here
struct bnode {
bnode * lChild;
bnode * rChild;
std::string word;
lnode * lineList;
};
#endif
In the latter case, you would still need to use #include "LinkedList.h" in any source files that need to access the content of the lineList member.

Related

C++ a class using other class

I'm trying to define Node into NodeList class, And store it.
Whay I've tried is:
In Try() function, I defined a node like Node *node = malloc... This works fine. But if I use the node that I defined in class like node = malloc... this line gives runtime error. I don't understand what is the difference between these two.
Here are classes:
Node.hpp
#ifndef NODE_HPP
#define NODE_HPP
class Node{
public:
int data;
};
#endif
Node.cpp
#include <iostream>
#include "Node.hpp"
using namespace std;
NodeList.hpp
#ifndef NODELIST_HPP
#define NODELIST_HPP
#include "Node.hpp"
class NodeList{
public:
Node *node;
void Try();
};
#endif
NodeList.cpp
#include <iostream>
#include "NodeList.hpp"
#include "Node.hpp"
using namespace std;
void NodeList::Try(){
//This works (doesn't give error):
//Node *node = (Node*)malloc(sizeof(Node));
//But I use the node I defined in class here and this line gives runtime error:
node = (Node*)malloc(sizeof(Node));
}
Main.cpp
#include <iostream>
#include "NodeList.hpp"
using namespace std;
int main()
{
NodeList *node = NULL;
node->Try();
system("PAUSE");
return 0;
}
You code has many problems:
In Main.cpp you are dereferencing a NULL pointer: node->DosyaOku();, but node is NULL. This is an undefined behavior.
You have the same problem in NodeList.cpp
You are using a malloc in Node.cpp and you should probably want to use a new instead (read here), and you should think how to free/delete that pointer.
The Create in Node.cpp has a parameter that is overwritten immediately, that looks like an error.

Setting pointer to null creates runtime error

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

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) {
// ...
}

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.

What is making my constructor protected?

#ifndef SLIST_H
#define SLIST_H
#include "llist.h"
using namespace std;
class slist:public llist{
public:
slist();
int search(el_t Key);
void replace(el_t Elem, int I);
};
#endif
That is my new class I just made that gives me the search and replace function, on top of all the inherited functions contained in llist.h
In my main...
#include "slist.h"
#include <iostream>
using namespace std;
int main(){
slist list;
list.addFront(4);
cout<<list.search(4);
}
I'm trying to call addfront() which is a public function in the llist class. Then I want to call search() which is an inherited public function of the slist class. g++ gives me a few errors that I don't understand.
slist.h: In function âint main()â:
slist.h:10: error: âslist::slist()â is protected
main.cpp:7: error: within this context
slist() is protected? Why's that? I put it under public:
Also whats up with the this context, I'm guessing I'm just doing the whole inheritance thing totally wrong. Any help would be appreciated!
Edit: Here's the llist class, if it helps
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
class llist{
protected:
typedef int el_t;
el_t total;
struct Node{
int Elem;
Node *Next;
};
Node *Front;
Node *Rear;
Node * Curr;
public:
class Overflow{};
class Underflow{};
class Range{};
llist();
~llist();
bool isEmpty();
void displayAll();
void addRear(el_t NewNum);
void deleteFront(el_t& OldNum);
void addFront(el_t NewNum);
void deleteRear(el_t& OldNum);
void deleteIth(int I, el_t& OldNum);
void addbeforeIth(int I, el_t newNum);
class Overflow;
};
#endif
This is llist.cpp with only the relevant functions pasted
#include "llist.h"
#include <iostream>
using namespace std;
int total=0;
llist::llist(){
Front=NULL;
Rear=NULL;
total=0;
}
llist::~llist(){
while(Front!=NULL){
int z;
deleteFront(z);
}
}
bool llist::isEmpty(){
if(Front==NULL){
return true;
}
return false;
}
void llist::displayAll(){
Curr=Front;
if(isEmpty()){
cout<<"[ empty ]"<<endl;
}else{
while(Curr!=NULL){\
cout<<"curr != NuL"<<endl;
cout<<Curr->Elem<<endl;
Curr=Curr->Next;
}
}
}
void llist::addFront(el_t NewNum){
if(isEmpty()){
Node *x=new Node;
x->Next=Front;
Rear=Front;
Front=x;
Front->Elem=NewNum;
}else{
Node *x=new Node;
x->Next=Front;
Front=x;
Front->Elem=NewNum;
++total;
}
}
I honestly can't see the problem but not every compiler is standard-compliant, so I would try the following:
1) Rename your class - if it works, that means it's a because of a naming conflict.
2) Remove the using directives.
3) Remove the inheritance. If it works after this... you really need to change compilers.
4) Try #undef public before your class declaration. If it works after this... well, someone's in for a talk with the manager.
5) Pray...