Multiple file compilation error

This is a discussion on Multiple file compilation error within the C++ Programming forums, part of the General Programming Boards category; I am starting to create my GUI toolkit project for the X Window System, and I created these classes, each ...

  1. #1
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198

    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:

    Code:
    #include "Dimension.h"
    #include "Application.h"
    #include "TopLevelWindow.h"
    
    int main() {
    	Application app;
    	TopLevelWindow win(app, "Test", Dimension(3, 3));
    	win.pack();
    	win.show();
    	app.eventLoop();
    	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

    Code:
    $ 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 02:06 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    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
    Posts
    3,573
    I think your headers are missing header guards.

    Code:
    #ifndef TOPLEVEL_WINDOW_H
    #define TOPLEVEL_WINDOW_H
    
    //header contents
    
    #endif
    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
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    What is a header guard?

  5. #5
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    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
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    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
    Location
    near a computer
    Posts
    3,856
    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, 09:33 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21