wxWidgets, private classes and header files Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
albinoblacksheep
Earned a small fee
Earned a small fee
Posts: 11
Joined: Tue Mar 28, 2017 4:59 pm

wxWidgets, private classes and header files

Post by albinoblacksheep »

Hi all,

since I am searching now days for a solution I take the risk and ask you for help.

I am having an issue with implementing Serial.h in my project. The Serial.h file can be found here
https://github.com/yan9a/serial/blob/master/Serial.h

I included the header file in my SCdeckmain.h file as I have to declare 'Serial com;' as follows

Code: Select all


#include "Serial.h"

...

class SCdeckFrame: public wxFrame
{
    public:
        Serial com; // Inherit from Serial.h
        SCdeckFrame(wxWindow* parent,wxWindowID id = -1);
        virtual ~SCdeckFrame();


    private:

        //(*Handlers(SCdeckFrame)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void OnConnect(wxCommandEvent& event);
...

When I try to complile I am getting the errors that there are multiple definitions

Code: Select all

obj\Debug\SCdeckMain.o||In function `ZSt19__iterator_categoryIN9__gnu_cxx17__normal_iteratorIPcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEENSt15iterator_traitsIT_E17iterator_categoryERKSB_':|
D:\Documents\Coding\SCdeck\Serial.h|96|multiple definition of `Serial::Serial()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|96|first defined here|
obj\Debug\SCdeckMain.o||In function `ZSt19__iterator_categoryIN9__gnu_cxx17__normal_iteratorIPcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEENSt15iterator_traitsIT_E17iterator_categoryERKSB_':|
D:\Documents\Coding\SCdeck\Serial.h|96|multiple definition of `Serial::Serial()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|96|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN14wxFormatStringC1EPKw':|
D:\Documents\Coding\SCdeck\Serial.h|111|multiple definition of `Serial::Serial(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, long, long, char, float)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|111|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN14wxFormatStringC1EPKw':|
D:\Documents\Coding\SCdeck\Serial.h|111|multiple definition of `Serial::Serial(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, long, long, char, float)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|111|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6SerialD2Ev':|
D:\Documents\Coding\SCdeck\Serial.h|125|multiple definition of `Serial::~Serial()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|125|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6SerialD2Ev':|
D:\Documents\Coding\SCdeck\Serial.h|125|multiple definition of `Serial::~Serial()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|125|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial7SetPortENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE':|
D:\Documents\Coding\SCdeck\Serial.h|130|multiple definition of `Serial::SetPort(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|130|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial7GetPortB5cxx11Ev':|
D:\Documents\Coding\SCdeck\Serial.h|134|multiple definition of `Serial::GetPort[abi:cxx11]()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|134|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial11SetDataSizeEl':|
D:\Documents\Coding\SCdeck\Serial.h|138|multiple definition of `Serial::SetDataSize(long)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|138|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial11GetDataSizeEv':|
D:\Documents\Coding\SCdeck\Serial.h|143|multiple definition of `Serial::GetDataSize()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|143|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial9SetParityEc':|
D:\Documents\Coding\SCdeck\Serial.h|147|multiple definition of `Serial::SetParity(char)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|147|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial9GetParityEv':|
D:\Documents\Coding\SCdeck\Serial.h|158|multiple definition of `Serial::GetParity()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|158|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial11SetStopBitsEf':|
D:\Documents\Coding\SCdeck\Serial.h|162|multiple definition of `Serial::SetStopBits(float)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|162|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial11GetStopBitsEv':|
D:\Documents\Coding\SCdeck\Serial.h|170|multiple definition of `Serial::GetStopBits()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|170|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial11SetBaudRateEl':|
D:\Documents\Coding\SCdeck\Serial.h|178|multiple definition of `Serial::SetBaudRate(long)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|178|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial11GetBaudRateEv':|
D:\Documents\Coding\SCdeck\Serial.h|195|multiple definition of `Serial::GetBaudRate()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|195|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial4OpenEv':|
D:\Documents\Coding\SCdeck\Serial.h|200|multiple definition of `Serial::Open()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|200|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial5CloseEv':|
D:\Documents\Coding\SCdeck\Serial.h|274|multiple definition of `Serial::Close()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|274|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial8IsOpenedEv':|
D:\Documents\Coding\SCdeck\Serial.h|286|multiple definition of `Serial::IsOpened()'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|286|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial5WriteEPc':|
D:\Documents\Coding\SCdeck\Serial.h|294|multiple definition of `Serial::Write(char*)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|294|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial9WriteCharEc':|
D:\Documents\Coding\SCdeck\Serial.h|317|multiple definition of `Serial::WriteChar(char)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|317|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial8ReadCharERb':|
D:\Documents\Coding\SCdeck\Serial.h|325|multiple definition of `Serial::ReadChar(bool&)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|325|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial6SetRTSEb':|
D:\Documents\Coding\SCdeck\Serial.h|371|multiple definition of `Serial::SetRTS(bool)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|371|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial6SetDTREb':|
D:\Documents\Coding\SCdeck\Serial.h|385|multiple definition of `Serial::SetDTR(bool)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|385|first defined here|
obj\Debug\SCdeckMain.o||In function `ZN6Serial6GetCTSERb':|
D:\Documents\Coding\SCdeck\Serial.h|399|multiple definition of `Serial::GetCTS(bool&)'|
obj\Debug\SCdeckApp.o:D:\Documents\Coding\SCdeck\Serial.h|399|first defined here|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build failed: 50 error(s), 35 warning(s) (0 minute(s), 2 second(s)) ===|

But I can't see any multiple definitions in the header file. There are just constructors, deconstructors and normal definitions.
I can compile the example wxwidgets file without any issues.

I neither have included Serial.h twice nor declared the definitons a second time anywhere. So I don't understand why the linker has the opinion that there are multiple definitions.

Small repro (codeblocks) is attached
repro.zip
(231.39 KiB) Downloaded 71 times
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: wxWidgets, private classes and header files

Post by T-Rex »

Move the implementation of all methods of Serial class into separate CPP file.
Or, just make them inline (inside the class). You get these errors because you have the implementation of methods and their declaration in the same hader file, which is visible by multiple other source files.
albinoblacksheep
Earned a small fee
Earned a small fee
Posts: 11
Joined: Tue Mar 28, 2017 4:59 pm

Re: wxWidgets, private classes and header files

Post by albinoblacksheep »

Thanks a lot!
Post Reply