WHAT redefinition?

This is a discussion on WHAT redefinition? within the C++ Programming forums, part of the General Programming Boards category; I have a program with a class in it (how interesting). I have the definitions of this class in a ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891

    WHAT redefinition?

    I have a program with a class in it (how interesting). I have the definitions of this class in a .h file, with the actual finction code in a .cpp file. The .cpp file #includes the .h file. Then, I included the cpp file in my main cpp file (with main in it). The result is twenty errors about redefinition. What can this be?
    If someone would like the code, I'll post it when prompted. It's a bit lengthy if it isn't necessary to post it.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    don't #include source files

    and make sure you have #ifndef guards on your headers so they aren't included multiple times
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Might you give an example? I was never fully educated in header commands.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    okay:

    Code:
    #ifndef _THIS_HEADER_NAME_  // if not defined...
    #define _THIS_HEADER_NAME_ // define it
    
    /* ... all the code of the header ... */
    
    #endif
    So, if you've included this header before, this will prevent the code from being accidentally included again.

    This may not be your problem though.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    #ifndef CLASS_H
    #define CLASS_H
    
    class Example {
        public:
            Example();
            ~Example();
        private:
            int fred;
    };
    
    #endif
    edit: rmullen beat me
    Last edited by alpha; 02-07-2003 at 10:10 PM.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Ouch, I'm really in a tangle.

    I have:

    -class in balance.h
    -class functions in account.cpp, which includes balance.h
    -balance.cpp (main file) what should this include? I'm thinking account.cpp, but you said no
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    try including balance.h;
    from what i understood, you have the prototypes in balance.h, and the code in account.cpp. if that is so, #include balance.h in balance.cpp (where main is); but this may not solve your problem.

    if you still have errors, you may want to post code and the error messages.

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Code:
    Compiling...
    Account.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\Account.cpp(27) : error C2556: 'int __thiscall ACCNT::GetLog(void)' : overloaded function differs only by return type from 'char *__thiscall ACCNT::GetLog(void)'
            C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\balance.h(70) : see declaration of 'GetLog'
    C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\Account.cpp(27) : error C2040: 'GetLog' : 'int (void)' differs in levels of indirection from 'char *(void)'
    C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\Account.cpp(34) : error C2556: 'int __thiscall ACCNT::deposit(double,char *,bool)' : overloaded function differs only by return type from 'double __thiscall ACCNT::deposit(double,char *,boo
    l)'
            C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\balance.h(54) : see declaration of 'deposit'
    C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\Account.cpp(34) : error C2371: 'deposit' : redefinition; different basic types
            C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\balance.h(54) : see declaration of 'deposit'
    C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\Account.cpp(105) : error C2556: 'int __thiscall ACCNT::withdraw(double,const char *,bool)' : overloaded function differs only by return type from 'double __thiscall ACCNT::withdraw(double,c
    onst char *,bool)'
            C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\balance.h(56) : see declaration of 'withdraw'
    C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\Account.cpp(105) : error C2371: 'withdraw' : redefinition; different basic types
            C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\balance.h(56) : see declaration of 'withdraw'
    C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\Account.cpp(209) : error C2556: 'int __thiscall ACCNT::GetName(void)' : overloaded function differs only by return type from 'char *__thiscall ACCNT::GetName(void)'
            C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\balance.h(60) : see declaration of 'GetName'
    C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\Account.cpp(209) : error C2040: 'GetName' : 'int (void)' differs in levels of indirection from 'char *(void)'
    C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\Account.cpp(214) : error C2556: 'int __thiscall ACCNT::GetBalance(void)' : overloaded function differs only by return type from 'double __thiscall ACCNT::GetBalance(void)'
            C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\balance.h(62) : see declaration of 'GetBalance'
    C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\Account.cpp(214) : error C2371: 'GetBalance' : redefinition; different basic types
            C:\Program Files\Microsoft Visual Studio\MyProjects\Balance\balance.h(62) : see declaration of 'GetBalance'
    Balance.cpp
    Error executing cl.exe.
    
    Balance.exe - 10 error(s), 0 warning(s)
    and back at square 1. I hate to run you in circles, but this is really peeving me. (my god if I could only spell)
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    The general idea is:

    prototypes/definitions in header
    "code" in source

    so account.cpp should include something like acount.h, and balance.cpp include balance.h.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    ////////////
    ///account.h
    ////////////
    #ifndef ACCOUNT_H
    #define ACCOUNT_H
    //protoypes
    #endif

    ////////////////
    //account.cpp
    ////////////////
    #include "account.h"
    //definitions for stuff in account h

    //////////////
    //balance.h //frequently not explicitly named
    //////////////
    //prototypes


    //////////////
    //balance.cpp ---the file where main() is found
    //////////////
    #include <iostream>
    #include "account.h"

    //alternate location for function prototypes declared in balance.h

    int main()
    {
    //whatever
    }

    //location for definitions of functions in balance.h or above main() and below list of #includes

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    overloaded fxns need different parameters, not different return types, that fixes a few errors, but can't tell the other errors w/out code.

    edit: a lot of errors are overloaded fxns with only diff in return type. fixing those may fix all your errors, will fix some, not sure if all though.

  12. #12
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Here: Iv'e consolidated and organized the code as you suggested, but I'm still having big, ugly yet simple linking issues. I'll include the source for anyone who wants to help further. Thank you all for your help already.
    Attached Files Attached Files
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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