Thread: Multiple file compilation error

  1. #1
    Registered User
    Join Date
    Aug 2009

    Multiple file compilation error

    I am starting to create my GUI toolkit project for the X Window System, and I created these classes, each of which has a .h and .cpp file:

    Dimension (actually contains 3 classes, Dimension, FloatDimension, and BoolDimension): An object to contain an x, y pair.
    Application: Initializes the resources used by the widgets, manages the event loop.
    TopLevelWindow: A window.

    Each of these individually compile into a .o file just fine, but my test program that uses them gives an error:

    #include "Dimension.h"
    #include "Application.h"
    #include "TopLevelWindow.h"
    int main() {
    	Application app;
    	TopLevelWindow win(app, "Test", Dimension(3, 3));
    	return 0;
    In case you don't know the g++ command, this is an explanation in english:

    g++ compile-only-no-linker include-library-X11 file:src/main.cpp

    $ g++ -c -lX11 src/main.cpp
    In file included from src/TopLevelWindow.h:5,
                     from src/main.cpp:4:
    src/Application.h:3: error: redefinition of ‘class Application’
    src/Application.h:3: error: previous definition of ‘class Application’
    In file included from src/TopLevelWindow.h:7,
                     from src/main.cpp:4:
    src/Dimension.h:1: error: redefinition of ‘class Dimension’
    src/Dimension.h:1: error: previous definition of ‘class Dimension’
    src/Dimension.h:10: error: redefinition of ‘class FloatDimension’
    src/Dimension.h:10: error: previous definition of ‘class FloatDimension’
    src/Dimension.h:18: error: redefinition of ‘class BoolDimension’
    src/Dimension.h:18: error: previous definition of ‘class BoolDimension’
    src/main.cpp: In function ‘int main()’:
    src/main.cpp:7: error: no matching function for call to ‘TopLevelWindow::TopLevelWindow(Application&, const char [10], Dimension)’
    src/TopLevelWindow.h:16: note: candidates are: TopLevelWindow::TopLevelWindow(Application*, std::string, Dimension)
    src/TopLevelWindow.h:8: note:                 TopLevelWindow::TopLevelWindow(const TopLevelWindow&)
    I have no idea what's going on. Can anyone help me fix this?
    Last edited by MTK; 12-01-2009 at 03:06 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Your headers probably include the other headers yes? Do you have appropriate header inclusion guards? What do the header files look like?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    The larch
    Join Date
    May 2006
    I think your headers are missing header guards.

    //header contents
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Aug 2009
    What is a header guard?

  5. #5
    Registered User
    Join Date
    Aug 2009
    I figured out the header guard thing, it compiled fine, and gave a segfault. Probably that big mess of pointers that lets all the objects refer to each other.

  6. #6
    Registered User
    Join Date
    Aug 2009
    I figured out the problem using the GDB debugger and it works, displaying a blank window with the correct title!

  7. #7
    train spotter
    Join Date
    Aug 2001
    near a computer
    Quote Originally Posted by MTK View Post
    What is a header guard?
    the 'technical' term is 'Conditional Compilation'.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM