0

I know many people have had this problem before. However, the solutions that I come across don't work for me. The problem also arises when using VSCode to build my code through G++, while many of the previous questions are concerning Xcode.

I'm coding an inheritance and polymorphism project for school, and when building main.cpp, I get this error:

duplicate symbol '_main' in:
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-21bf23.o
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-ac12d2.o
duplicate symbol 'checkAntNeighbors(Creature* (*) [10], int, int)' in:
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-21bf23.o
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-ac12d2.o
duplicate symbol 'runTurn(Creature* (*) [10], int)' in:
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-21bf23.o
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-ac12d2.o
duplicate symbol 'antDistances(Creature* (*) [10], int, int, int*)' in:
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-21bf23.o
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-ac12d2.o
duplicate symbol 'beetleDistances(Creature* (*) [10], int, int, int*)' in:
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-21bf23.o
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-ac12d2.o
duplicate symbol 'antMove(Creature* (*) [10], int, int, int*)' in:
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-21bf23.o
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-ac12d2.o
duplicate symbol 'beetleMove(Creature* (*) [10], int, int, int*)' in:
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-21bf23.o
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-ac12d2.o
duplicate symbol 'printGrid(Creature* (*) [10], char, char)' in:
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-21bf23.o
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-ac12d2.o
duplicate symbol 'populateGrid(Creature* (*) [10], std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)' in:
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-21bf23.o
    /var/folders/ql/g3bvqchs5jl8svpmfdfqw2380000gn/T/main-ac12d2.o
ld: 9 duplicate symbols for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I also have other warnings but I don't think that they are root cause:

delete called on non-final 'Creature' that has virtual functions but non-virtual destructor [-Wdelete-non-abstract-non-virtual-dtor]

Anywho, I have a base class Creature with derived classes Beetle and Ant. The Beetle and Ant header files inherit the Creature header file. Main inherits all header files. The only platform that builds my code is onlineGDB, so it has to be an IDE problem.

Any help would be greatly appreciated. Please let me know if I need to provide more information such as code and I will respond in a timely manner. Again, thanks to anyone that helps!

  • Looks like you may have stale obj files, multiple cpp files with the same functions defined in them, or are including a cpp file in another cpp file that is also being compiled as part of your project. Clean and then rebuild, and add a virtual destructor to `Creature`. – Retired Ninja Sep 25 '22 at 05:45
  • @RetiredNinja I added the virtual keyword to the creature destructor. That clears up the other errors. The first question I have is that an override keyword is not required in the derived constructors. Also, if I call the Creature destructor in my derived destructors, does that also solve the problem? Lastly, how am I able to clean and rebuild? Thanks so much for the quick response, I appreciate it. – Daniel Vayman Sep 25 '22 at 07:02
  • Don't have xcode in front of me, but I believe the clean and build options are under the Product menu. The reason for a virtual destructor is for the case where you have a base class pointer to a derived instance and want to delete it. Without virtual only the base class destructor is called versus the one that matches the instance. [When to use virtual destructors?](https://stackoverflow.com/questions/461203/when-to-use-virtual-destructors) – Retired Ninja Sep 25 '22 at 07:20
  • @RetiredNinja I was asking how to rebuilld/clean in VScode. Also, thanks for explaining virtual destructors – Daniel Vayman Sep 25 '22 at 23:47

0 Answers0