Thread: Compilation Error. Need help

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Compilation Error. Need help

    Ok. I'm doing a Towers of Hanoi project and it's supposed to be a multi file program. I keep getting this error and I can't figure out why.
    hanoi.c:4: error: redefinition of ‘hanoi’
    hanoi.h:5: error: previous definition of ‘hanoi’ was here
    Here's the code for hanoi.h
    Code:
    void makeMove(int, int);
    void hanoi(int, int, int, int);
    
    
    extern in n;
    extern int noOfMoves;
    enum stacks{A = 1, B, C};
    and for hanoi.c
    Code:
    #include "hanoi.h"
    
    void hanoi(int n, int src, int dest, int by)
    {
            if(n == 1)
            {
                    makeMove(src, dest);
            }
            else
            {
                    hanoi(n - 1, src, by, dest);
                    makeMove(src, dest);
                    hanoi(n-1, by, dest, src);
            }
    }

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    What does the rest of it look like?

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    do you inlcude hanoi.h in another .c file? If so, you're loading the definitions within hanoi.h twice... in hanoi.h add the following to the TOP of the file

    Code:
    #ifndef __HANOI_H     // or some other unique identifier
    #define __HANOI_H
    then at the VERY BOTTOM of hanoi.h add
    Code:
    #endif
    This will prevent the error you're experiencing

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bladactania
    in hanoi.h add the following to the TOP of the file
    Names that contain consecutive underscores, or that begin with an underscore followed by an uppercase letter, are reserved to the implementation for any use. As such, even though it is unlikely to matter, I would suggest removing those two leading underscores from the header inclusion guard macro name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I wasn't aware of that. Thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PC RAM in Compilation
    By SlyMaelstrom in forum Tech Board
    Replies: 5
    Last Post: 05-21-2006, 06:03 AM
  2. Inconsistent Compilation
    By GlassEyeSlim in forum Linux Programming
    Replies: 2
    Last Post: 02-23-2006, 06:43 PM
  3. Compilation units
    By filler_bunny in forum C++ Programming
    Replies: 0
    Last Post: 10-21-2003, 03:57 AM
  4. MS VC++ Crash on compilation
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-23-2003, 07:06 PM
  5. malloc problem in SUN in 64-bit compilation
    By ylzhang in forum C Programming
    Replies: 6
    Last Post: 05-31-2003, 11:48 AM