Multiple definition of class member function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brixton
    New Member
    • Nov 2006
    • 35

    #1

    Multiple definition of class member function

    Hello,

    this is probably an extremely silly question but I've been trying to find the problem for a while now so here it goes. Given the following code....

    MainFrame.h
    Code:
    #include "wxTextWrapper.h"
    wxTextWrapper.h
    Code:
    class wxTextWrapper
    {
    public:
        void Wrap(wxWindow *win, const wxString& text, int widthMax);
    
       // there are a bunch of other methods here
    
    };
    wxTextWrapper.c pp
    Code:
    #include "wxTextWrapper.h"
    
    void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax)
    {
         // lots of code...
    }
    I've left out a lot of stuff but these should be the important ones... the compiler is complaining about "multiple definition of `wxTextWrapper: :Wrap(wxWindow* , wxString const&, int)'".

    It should probably be noted that the overall structure of my classes at the moment is:

    1. DSVApp: includes LoginFrame and MainFrame
    2a. LoginFrame
    2b. MainFrame: includes wxTextWrapper

    I'm having a hard time figuring out why I get this error, so any help is appreciated.

    Rgds
    // Joakim
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Do you have macro guards on your header file to prevent it from being included more than once? Not having them could cause a multiple definition error, since you include the header file in multiple source files. Macro guards are
    Code:
    #ifndef WXTEXTWRAPPER_H
    #define WXTEXTWRAPPER_H
    at the top of your header file, and
    Code:
    #endif
    at the bottom. That way, once the header file is included once, it won't be included again. Come to think of it though, this might not be your problem, because I would think the compiler would be complaining about the class being defined more than once, not the function. But you should put macro guards on your header file if you haven't already.

    Hope this helps.
    Last edited by boxfish; Sep 30 '08, 08:46 PM. Reason: More Stuff

    Comment

    • brixton
      New Member
      • Nov 2006
      • 35

      #3
      Don't know how but this morning when I started it up I got it to compile.
      Learned about macro guards, though :-)

      Thanks!

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Great! Maybe you clicked "Rebuild All" this morning? I find that I always have to do that if I make a change to a header file.

        Comment

        Working...