Redefinition of struct error, I only defined it once

c++

Solution

In list.cpp, you are including both "line.h" and "list.h". But "list.h" already includes "line.h" so "list.h" is actually included twice in your code. (the preprocessor is not smart enough to not include something it already has).

There are two solutions:

- Do not include "list.h" directly in your list.cpp file, but it is a practice that does not scale: you have to remember what every of your header file includes and that will be too much very quickly.

- use include guards, as explained by @juanchopanza

Problem

I really don't understand how to fix this redefinition error. COMPILE+ERRORS ``` g++ main.cpp list.cpp line.cpp In file included from list.cpp:5:0: line.h:2:8: error: redefinition of âstruct Lineâ line.h:2:8: error: previous definition of âstruct Lineâ ``` main.cpp ``` #include <iostream> using namespace std; #include "list.h" int main() { int no; // List list; cout << "List Processor\n==============" << endl; cout << "Enter number of items : "; cin >> no; // list.set(no); // list.display(); } ``` list.h ``` #include "line.h" #define MAX_LINES 10 using namespace std; struct List{ private: struct Line line[MAX_LINES]; public: void set(int no); void display() const; }; ``` line.h ``` #define MAX_CHARS 10 struct Line { private: int num; char numOfItem[MAX_CHARS + 1]; // the one is null byte public: bool set(int n, const char* str); void display() const; }; ``` list.cpp ``` #include <iostream> #include <cstring> using namespace std; #include "list.h" #include "line.h" void List::set(int no) {} void List::display() const {} ``` line.cpp ``` #include <iostream> #include <cstring> using namespace std; #include "line.h" bool Line::set(int n, const char* str) {} void Line::display() const {} ```

Original source

Related problems