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.
Related
I have a simple c++ app:
node.h:
#include<iostream>
using namespace::std;
class Node
{
private:
int data;
Node *next;
public:
Node(int nodeData,Node *nextNode);
};
node.cpp:
#include "node.h"
Node::Node(int nodeData, Node *nextNode) {
data = nodeData;
next = nextNode;
}
linked_list.h
#include "node.h"
class LinkedList
{
private:
Node *head;
Node *tail;
int size;
public:
LinkedList();
int getSize();
};
linked_list.cpp:
#include "linked_list.h"
LinkedList::LinkedList()
{
size = 0;
}
int LinkedList::getSize() {
return size;
}
main.cpp:
#include <iostream>
#include "node.h"
#include "linked_list.h"
using namespace ::std;
int main()
{
cout << "This is main!\n";
return 0;
}
I am on linux, inside the projcet's directory, I open a terminal there and try to compile them by this command:
g++ *.cpp *.h -o app
but I get this error:
In file included from linked_list.h:1:0,
from main.cpp:3:
node.h:1:7: error: redefinition of ‘class Node’
class Node
^~~~
In file included from main.cpp:2:0:
node.h:1:7: note: previous definition of ‘class Node’
class Node
^~~~
I looked at some posts here on stackoverlfow but had no luck in solving my problem. I am new to c++, I know that the compiler thinks I am redefining class Node somewhere, but where is this somewhere so I can remove the definition?
Your linked_list.h includes node.h, so the compiler will see the definition in node.h twice while compiling main.cpp.
To avoid this problem, you should add "include guard" to your header files.
It should be like this:
node.h:
#ifndef NODE_H_GUARD // add this
#define NODE_H_GUARD // add this
#include<iostream>
using namespace::std;
class Node
{
private:
int data;
Node *next;
public:
Node(int nodeData,Node *nextNode);
};
#endif // add this
The macro name to define and check should be different for each headers.
Another way to avoid this problem is to adding #pragma once as the first lines of your headers if your compiler supports this.
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;
};
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.
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) {
// ...
}
I've created 2 header files. ListA.h and ListN.h
They both make their own use their own unique class List. When I compile my program (even though they have no way of knowing the other exists, it says the following error)
Im pretty sure it shouldnt be a redefinition, but it obviously is. Any help is appreciated.
ListA.h
#ifndef __LISTA_H_
#define __LISTA_H_
#include <iostream>
using namespace std;
class List{
public:
List(int = 0);
List(const List&);
~List();
};
#endif
ListN.h
#ifndef __LISTN_H_
#define __LISTN_H_
#include <iostream>
using namespace std;
class List{
public:
List(int = 10);
List(const List&);
~List();
};
#endif
ListA.cpp
#include "ListA.h"
using namespace std;
List::List(int mySize)
{
//...
}
ListN.cpp
#include "ListN.h"
#include <iostream>
using namespace std;
List::List(int size)
{
//...
}
Main
#include <iostream>
#include "ListN.h"
using namespace std;
int main()
{
List myList;
return 0;
}
Both cpp files are being compiled by the compiler. Thus, when the linker goes to link the files together, it gets confused, since there are multiple List classes.
To fix this, you could use namespaces, or you cold not expose at least one of the List classes.
Alternatively, if the idea was to be able to include ListN.h vs ListA.h for configuration purposes, this is the wrong way to do so. Either you should have a #define parameter for the header, or you should find some other way, such as through #ifdef. For example (I'm not 100% sure this would compile, but you get the idea):
List.h
#ifndef __LIST_H_
#define __LIST_H_
#ifndef LIST_PARAM
#define LIST_PARAM 0
#endif
#include <iostream>
using namespace std;
class List{
public:
List(int = LIST_PARAM);
List(const List&);
~List();
};
#endif
main.cpp
#include <iostream>
#define LIST_PARAM 10
#include "List.h"
using namespace std;
int main()
{
List myList;
return 0;
}
I personally don't like this method; it is much better to just pass the value in to the constructor:
int main()
{
List myList{ 10 };
return 0;
}
When linker trying to link find the definition / symbol for List, it does found in two different obj file and hence linker givers error. In visual studio error number : LNK2005
To solve this error, either:
To fix, add /FORCE:MULTIPLE to the linker command line options
Add the classes in two different namespaces which will avoid this error.
ListN.h
#ifndef __LIST_H_
#define __LIST_H_
#include <iostream>
using namespace std;
namespace ListN
{
class List{
public:
List(int = 10);
List(const List&);
};
}
#endif
ListN.cpp
#include "ListN.h"
#include <iostream>
using namespace std;
namespace ListN
{
List::List(int size)
{
//...
}
}
Main.cpp
#include <iostream>
#include "ListN.h"
int main()
{
ListN::List myList;
return 0;
}