How can solve C++ Redefinition headers error in CLION?

  c++, c++14, clion, cmake, oop

Can anybody help me with this error?
I was given a Redefinition error, I check the internet but I can’t find any good solution. if you have any solution help me, please.

Array.h

#ifndef DATA_STRUCTURES_ARRAY_H
#define DATA_STRUCTURES_ARRAY_H


#include <iostream>

template <typename T>
class Array
{
    template <typename U>
    friend std::ostream &operator<<(std::ostream&, const Array<U>&);
public:
    explicit Array(unsigned int);
    ~Array();
    T &operator[](int);
    Array<T> &operator=(Array<T>&);
    unsigned int len() const { return length; };

private:
    T *a;
    unsigned int length;
};



#endif //DATA_STRUCTURES_ARRAY_H

Array.cpp

#include "Array.h"
#include <cassert>

template <typename T>
Array<T>::Array(unsigned int l)
{
    length = l;
    a = new T[length];
}

template <typename T>
Array<T>::~Array()
{
    delete[] a;
}

template <typename T>
T &Array<T>::operator[](int subscript)
{
    assert(subscript >= 0 && subscript < length);
    return a[subscript];
}

template <typename T>
Array<T> &Array<T>::operator=(Array<T> &b)
{
    if(a != NULL)
        delete[] a;
    a = b.a;
    b.a = NULL; // This statement don't delete allocated memory, so its assign NULL value to local b.a variable
    length = b.length;
    return *this;
}

template <typename T>
std::ostream &operator<<(std::ostream &output, const Array<T> &b)
{
    for(int i = 0; i < b.length; ++i)
        output << b.a[i] << 't';
    return output;
}

ArrayStack.h

#ifndef DATA_STRUCTURES_ARRAYSTACK_H
#define DATA_STRUCTURES_ARRAYSTACK_H

#include <iostream>
#include "Array.cpp"

template <typename T>
class ArrayStack
{
    template <typename U>
    friend std::ostream &operator<<(std::ostream&, const ArrayStack<U>&);
public:
    explicit ArrayStack(unsigned int = 1);
    unsigned int size() const;
    const T& get(unsigned int);
    T set(unsigned int, const T&);
    void add(unsigned int, const T&);
    T remove(unsigned int);
    void resize();

private:
    Array<T> a;
    unsigned int n;
};

#endif //DATA_STRUCTURES_ARRAYSTACK_H

ArrayStack.cpp

#include "ArrayStack.h"

template <typename T>
ArrayStack<T>::ArrayStack(unsigned int len)
        :a(len)
{
    n = len;
}

template <typename T>
unsigned int ArrayStack<T>::size() const
{
    return n;
}

template <typename T>
const T &ArrayStack<T>::get(unsigned int i)
{
    return a[i];
}

template <typename T>
T ArrayStack<T>::set(unsigned int i, const T &x)
{
    T y = a[i];
    a[i] = x;
    return y;
}

template <typename T>
void ArrayStack<T>::add(unsigned int i, const T &x)
{
    if(n + 1 > a.len())
        resize();
    for(int j = n; j > i; --j)
        a[j] = a[j - 1];
    a[i] = x;
    ++n;
}

template <typename T>
T ArrayStack<T>::remove(unsigned int i)
{
    T x = a[i];
    for(unsigned int j = i; j < n; ++j)
        a[j] = a[j + 1];
    --n;
    if(a.len() >= 3 * n)
        resize();
    return x;
}

template <typename T>
void ArrayStack<T>::resize()
{
    Array<T> b((n * 2 > 1 ? n * 2 : 1));
    for(int i = 0; i < n; ++i)
        b[i] = a[i];
    a = b;
}

template <typename T>
std::ostream &operator<<(std::ostream &output, const ArrayStack<T> &b)
{
    output << b.a;
    return output;
}

ArrayQueue.h

#ifndef DATA_STRUCTURES_ARRAYQUEUE_H
#define DATA_STRUCTURES_ARRAYQUEUE_H

#include <iostream>
#include "Array.cpp"

template <typename T>
class ArrayQueue
{
    template <typename U>
    friend std::ostream &operator<<(std::ostream&, ArrayQueue<U>&);
public:
    explicit ArrayQueue(unsigned int = 1);
    unsigned int size() const;
    bool add(const T&);
    T remove();
    const T &front();
    void resize();

private:
    Array<T> a;
    unsigned int n;
    int j;
};

#endif //DATA_STRUCTURES_ARRAYQUEUE_H

ArrayQueue.cpp

#include "ArrayQueue.h"

template <typename T>
ArrayQueue<T>::ArrayQueue(unsigned int len)
        :a(len)
{
    n = 0;
}

template <typename T>
unsigned int ArrayQueue<T>::size() const
{
    return n;
}

template <typename T>
bool ArrayQueue<T>::add(const T &x)
{
    if(n + 1 > a.len())
        resize();
    a[(j + n) % a.len()] = x;
    ++n;
    return true;
}

template <typename T>
T ArrayQueue<T>::remove()
{
    T x = a[j];
    j = (j + 1) % a.len();
    --n;
    if(a.len() >= 3 * n)
        resize();
    return x;
}

template <typename T>
const T &ArrayQueue<T>::front()
{
    return a[j % a.len()];
}

template <typename T>
void ArrayQueue<T>::resize()
{
    Array<T> b((n * 2 > 1 ? n * 2 : 1));
    for(int i = 0; i < n; ++i)
        b[i] = a[(j + i) % a.len()];
    j = 0;
    a = b;
}

template <typename T>
std::ostream &operator<<(std::ostream &output, ArrayQueue<T> &b)
{
    int t = b.n;
    while(t)
        output << b.a[(b.j + b.n - t--) % b.a.len()] << ' ';
    return output;
}

main.cpp

#include <iostream>
#include "ArrayBased/ArrayStack.cpp"
#include "ArrayBased/ArrayQueue.cpp"

using namespace std;

int main() {

     cout << "------------------------ ArrayStack ------------------------" << endl;
    ArrayStack<int> arrayStack;
    arrayStack.add(0, 1);
    cout << arrayStack << endl;
    arrayStack.add(1, 2);
    cout << arrayStack << endl;
    arrayStack.add(2, 3);
    cout << arrayStack << endl;
    arrayStack.add(3, 4);
    cout << arrayStack << endl;
    cout << "Set element in index of 2 to 5" << endl;
    arrayStack.set(2, 5);
    cout << arrayStack << endl;
    cout << "Element in index of 1: " << arrayStack.get(1) << endl;
    cout << "Removing element in index of 1..." << endl;
    arrayStack.remove(1);
    cout << arrayStack << endl;

    cout << "------------------------ ArrayStack_Queue ------------------------" << endl;
    ArrayQueue<int> arrayQueue;
    arrayQueue.add(5);
    cout << arrayQueue << endl;
    arrayQueue.add(6);
    cout << arrayQueue << endl;
    arrayQueue.add(7);
    cout << arrayQueue << endl;
    arrayQueue.add(8);
    cout << arrayQueue << endl;
    arrayQueue.add(9);
    cout << arrayQueue << endl;
    arrayQueue.remove();
    cout << arrayQueue << endl;

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(Data-Structures)

set(CMAKE_CXX_STANDARD 14)

#Set your directories.  The dot representes the root application folder.
#Thus my the path to my domain folder:
set(ArrayBased ./ArrayBased)

#Include the directories and now your cpp files will recognize your headers
include_directories(${ArrayBased})

set(Headers ${ArrayBased}/Array.h ${ArrayBased}/ArrayStack.h ${ArrayBased}/ArrayQueue.h)
set(Sources ${ArrayBased}/Array.cpp ${ArrayBased}/ArrayStack.cpp ${ArrayBased}/ArrayQueue.cpp)

add_executable(Data-Structures main.cpp ${Sources} ${Headers})

And these are the errors:

====================[ Build | Data-Structures | Debug ]=========================
/snap/clion/164/bin/cmake/linux/bin/cmake --build /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/cmake-build-debug --target Data-Structures -- -j 6
Scanning dependencies of target Data-Structures
[ 20%] Building CXX object CMakeFiles/Data-Structures.dir/main.cpp.o
In file included from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayQueue.h:5,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayQueue.cpp:1,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/main.cpp:3:
/home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/Array.cpp:5:1: error: redefinition of ‘Array<T>::Array(unsigned int)’
    5 | Array<T>::Array(unsigned int l)
      | ^~~~~~~~
In file included from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayStack.h:5,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayStack.cpp:1,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/main.cpp:2:
/home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/Array.cpp:5:1: note: ‘Array<T>::Array(unsigned int)’ previously declared here
    5 | Array<T>::Array(unsigned int l)
      | ^~~~~~~~
In file included from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayQueue.h:5,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayQueue.cpp:1,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/main.cpp:3:
/home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/Array.cpp:12:1: error: redefinition of ‘Array<T>::~Array()’
   12 | Array<T>::~Array()
      | ^~~~~~~~
In file included from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayStack.h:5,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayStack.cpp:1,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/main.cpp:2:
/home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/Array.cpp:12:1: note: ‘Array<T>::~Array()’ previously declared here
   12 | Array<T>::~Array()
      | ^~~~~~~~
In file included from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayQueue.h:5,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayQueue.cpp:1,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/main.cpp:3:
/home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/Array.cpp:18:4: error: redefinition of ‘T& Array<T>::operator[](int)’
   18 | T &Array<T>::operator[](int subscript)
      |    ^~~~~~~~
In file included from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayStack.h:5,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayStack.cpp:1,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/main.cpp:2:
/home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/Array.cpp:18:4: note: ‘T& Array<T>::operator[](int)’ previously declared here
   18 | T &Array<T>::operator[](int subscript)
      |    ^~~~~~~~
In file included from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayQueue.h:5,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayQueue.cpp:1,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/main.cpp:3:
/home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/Array.cpp:25:11: error: redefinition of ‘Array<T>& Array<T>::operator=(Array<T>&)’
   25 | Array<T> &Array<T>::operator=(Array<T> &b)
      |           ^~~~~~~~
In file included from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayStack.h:5,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayStack.cpp:1,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/main.cpp:2:
/home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/Array.cpp:25:11: note: ‘Array<T>& Array<T>::operator=(Array<T>&)’ previously declared here
   25 | Array<T> &Array<T>::operator=(Array<T> &b)
      |           ^~~~~~~~
In file included from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayQueue.h:5,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayQueue.cpp:1,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/main.cpp:3:
/home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/Array.cpp:36:15: error: redefinition of ‘template<class T> std::ostream& operator<<(std::ostream&, const Array<T>&)’
   36 | std::ostream &operator<<(std::ostream &output, const Array<T> &b)
      |               ^~~~~~~~
In file included from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayStack.h:5,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/ArrayStack.cpp:1,
                 from /home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/main.cpp:2:
/home/amin/Documents/ProgrammingProjects/CppProjects/Data-Structures/ArrayBased/Array.cpp:36:15: note: ‘template<class T> std::ostream& operator<<(std::ostream&, const Array<T>&)’ previously declared here
   36 | std::ostream &operator<<(std::ostream &output, const Array<T> &b)
      |               ^~~~~~~~
make[3]: *** [CMakeFiles/Data-Structures.dir/build.make:72: CMakeFiles/Data-Structures.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Data-Structures.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/Data-Structures.dir/rule] Error 2
make: *** [Makefile:124: Data-Structures] Error 2

Compiler (or Linker) gives me an error of why you include Array.cpp two times? (Because I include Array.cpp in ArrayQueue.h and ArrayStack.h) and include ArrayQueue.cpp, ArrayStack.cpp in main.cpp and this is why this error happened.
I can’t remove #include "Array.cpp" this line because when I do it another runtime error comes with it.
so I’m confused.

Source: Windows Questions C++

LEAVE A COMMENT