Static Member Initialization and Singleton Design, error -> multiple definition of member

389 Views Asked by At

I'm trying to implement singielton design, but unfortunately this error of multiple definiton occurrs. Everything seems to be alright I cannot find multiple definiton nowhere. I tried to compile it with gcc on WSL and mingw but the problem stays. I'm following this design https://refactoring.guru/pl/design-patterns/singleton/cpp/example

And member static member initialization from https://www.tutorialspoint.com/how-to-initialize-private-static-members-in-cplusplus

fsc.hpp

class FileSystemController {
    private:
        const static std::vector<std::pair<uint32_t, uint32_t>> testProfileData;

    protected:
        FileSystemController {};
        static FileSystemController* fsc_;
    public:
        FileSystemController(FileSystemController& other) = delete;    
        void operator=(const FileSystemController&) = delete;    
        static FileSystemController* GetInstance();        
    };

    FileSystemController* FileSystemController::fsc_ = nullptr;
    const std::vector<std::pair<uint32_t, uint32_t>> FileSystemController::testProfileData = {{1001, 2001},
                                                                                              {1002, 2002},
                                                                                              {1003, 2003},
                                                                                              {1004, 2004},
                                                                                              {1005, 2005},
                                                                                              {1006, 2006},
                                                                                              {1007, 2007}};

fsc.cpp

FileSystemController* FileSystemController::GetInstance() {
    if (fsc_ == nullptr) {
        fsc_ = new FileSystemController();
    }
    return fsc_;
}

logs

 ====================[ Build | FileSystemController | Debug ]====================
    /usr/bin/cmake --build /mnt/e/Dokumenty/AiR_rok_4/S7/EngineeringThesis/Profilometr/FileSystemController/cmake-build-debug --target FileSystemController -- -j 8
    Scanning dependencies of target FileSystemController
    [ 25%] Building CXX object CMakeFiles/FileSystemController.dir/FileSystem/FileSystemController.cpp.o
    [ 50%] Linking CXX executable FileSystemController
    /usr/bin/ld: CMakeFiles/FileSystemController.dir/FileSystem/FileSystemController.cpp.o:(.bss+0x0): multiple definition of `controllers::FileSystemController::fsc_'; CMakeFiles/FileSystemController.dir/main.cpp.o:(.bss+0x0): first defined here
    /usr/bin/ld: CMakeFiles/FileSystemController.dir/FileSystem/FileSystemController.cpp.o:(.bss+0x10): multiple definition of `controllers::FileSystemController::testProfileData'; CMakeFiles/FileSystemController.dir/main.cpp.o:(.bss+0x10): first defined here
    collect2: error: ld returned 1 exit status
    make[3]: *** [CMakeFiles/FileSystemController.dir/build.make:114: FileSystemController] Error 1
    make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/FileSystemController.dir/all] Error 2
    make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/FileSystemController.dir/rule] Error 2
    make: *** [Makefile:118: FileSystemController] Error 2
1

There are 1 best solutions below

0
Giel On

The declaration of that static variable should be in the header, the definition in the .cpp.

otherwise if you include the file multiple time, the compiler will find multiple definitions.