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.
Related
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.
I read that "if you declare and implement a function in a header file (Header.h) and if this file gets included twice, then you'll most likely will get a function already defined error at some point.". But in my code all the functions that I get errors on are in a .cpp file.
List.h
#pragma once
typedef struct list
{
int lin;
int col;
int val;
struct list* next;
struct list* prev;
}list;
List.cpp
#include "List.h"
bool empty(list*& start)
{
return (start == nullptr);
}
Matrice.h
#pragma once
#include "List.h"
class Matrice
{
private:
list* start;
list* finish;
public:
Matrice() :start(nullptr), finish(nullptr) {}
Matrice(const Matrice& matrice);
};
Matrice.cpp
#include "Matrice.h"
#include "List.cpp"
Matrice::Matrice(const Matrice& matrice)
{
if (empty(start))
{
// Code
}
}
Source.cpp
#include "Matrice.h"
#include <iostream>
int main()
{
Matrice a;
Matrice b;
a = b;
}
I added all the files, maybe there's something I don't see. The error is on the bool empty(list*& start) function ("already defined in List.obj").
You have an #include<List.cpp> in your Matrice.cpp and as you compile and link all cpp files together this will result duplicate definitions of everything defined in List.cpp as they are also defined in Matrice.cpp due to the include.
Replace the #include<List.cpp> with #include<List.h> and add the declaration of empty into the List.h
This is my header file
#ifndef LinkedList_H
#define LinkedList_H
#include <iostream>
#include "Node.h"
class LinkedList {
public:
int length;
// pointer to the first element of LinkedList
Node *head = 0;
// pointer to the last element of LinkedList
Node *tail = 0;
LinkedList();
~LinkedList();
};
#endif
and this is my.cpp file
#include "LinkedList.h"
using namespace std;
LinkedList::LinkedList() {
head=tail;
this->length=0;
}
LinkedList::~LinkedList() {
Node *current = head;
while(current!=NULL){
Node *temp = current;
current=current->next;
delete temp;
}
}
void add(string _name, float _amount){
Node *node = new Node(_name, _amount);
while(head==NULL){ //here, there is an error.
head=node;
head->next=tail;
}
}
int main(){
LinkedList *list = new LinkedList();
add("Adam", 7);
cout<<list->head<<endl;
}
In my .cpp file when I want to try to make an add function, it gives me an error in the while loop condition in add function. It says "head was not declared in this scope". But I declared in .h file. I couldn't see what is wrong.
You should use the resolution scope operator, just like you did for the constructor and the destructor.
So, you in your source file do this:
void LinkedList::add(string _name, float _amount) {
And then, of course, declare that function inside your class, in the header file.
Very likely the problem is circular includes. You probably have Node.h including LinkedList.h and vice-versa. This leads to (essentially) a paradox: If both class definitions require the other, which of the two is defined first in any given compilation unit?
In practice, you include Node.h, which then tries to include LinkedList.h again (note that #include literally means "copy-paste this file here" to the compiler), but at this point LinkedList_H is already defined (because that's where you came from) and the include has no effect. So now you are in the middle of Node.h but with no prior definition of LinkedList and get "not declared" errors.
The solution is to remove the #include Node.h in LinkedList.h and replace it with a forward declaration, since the LinkedList definition in the header doesn't need to know anything more than "the class Node exists" (because it only uses pointers).
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) {
// ...
}
For a school project I am trying to make a binary search tree at the same time we are supposed to learn how to use 'friendship' in classes. The errors I get while compiling are: [I put comments in code where the errors originate from for clarity]
$ make -f makefile.txt
g++ -Wall -W -Werror -pedantic -g -c BST.cpp
BST.cpp: In member function `void BST::insert(std::string, std::string)':
BST.cpp:13: error: invalid use of undefined type `struct Node'
BST.h:19: error: forward declaration of `struct Node'
makefile.txt:9: recipe for target `BST.o' failed
make: *** [BST.o] Error 1
Basically I want to be able to access the Node class as if the class was nested (I am not allowed to nest it for the sake of this programming assignment however). Obviously simply using 'ptr->m_data' would not work, but what could I do to make it work?
Node.h
#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class BST;
class Node
{
public:
Node(string key, string data)
{n_key = key; n_data = data;}
~Node();
private:
string m_key;
string m_data;
Node *m_left;
Node *m_right;
//Node *m_parent;
};
#endif // NODE_H_INCLUDED
BST.h
#ifndef BST_H_INCLUDED
#define BST_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class BST
{
public:
BST()
{m_root = NULL;}
~BST();
void insert(string key, string data);
void find(string key);
void remove(string key, string data);
void print();
friend class Node; //Error: forward declaration of 'struct Node'
private:
Node* m_root;
};
#endif // BST_H_INCLUDED
Why is it that when I call the below line of code it reads out the above error messages? (Note: the below code is from BST.cpp)
#include "BST.h"
void BST::insert(string key, string data)
{
Node* yPtr = NULL;
Node* xPtr = m_root;
while(xPtr != NULL)
{
yPtr = xPtr;
if(key < xPtr->m_key) //Error: invalid use of undefined type 'struct Node'
{
}
}
}
The compiler has not seen the definition of Node when it gets to that line in BST.cpp. Note that that is the first line where the compiler needs to see the structure of Node. You need to #include "Node.h" in BST.cpp.