Declaring Class function error?

This is a discussion on Declaring Class function error? within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am experiencing a strange problem when declaring classes outside the main source file in VC++. I have ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Unhappy Declaring Class function error?

    Hi all,

    I am experiencing a strange problem when declaring classes outside the main source file in VC++. I have made an experiment that demonstrates my problem. There are two files: “main.cpp” and “class.cpp”.

    “main.cpp” content:
    Code:
    #include "class.cpp"
    
    int main() {
    	return 0;
    }
    “class.cpp” content:
    Code:
    class Class {
    public:
    	void Test();
    };
    
    void Class::Test() {
    	return;
    }
    When I try to compile I get following error:

    --------------------Configuration: main - Win32 Debug--------------------
    Compiling...
    main.cpp
    class.cpp
    Linking...
    class.obj : error LNK2005: "public: void __thiscall Class::Test(void)" (?Test@Class@@QAEXXZ) already defined in main.obj
    Debug/main.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.

    main.exe - 2 error(s), 0 warning(s)

    It says that the function void Test() is already declared, but how? Please help me, am I doing something wrong?
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    You have to make class.cpp a header file (class.h)...

  3. #3
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    and in the second part, I think(I'm not sure) it should be

    Code:
    void test::class()
    {
         stuff;
    }

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Use inclusion guards around your class declaration too.

    Code:
    #ifndef MY_CLASS_H
    #define MY_CLASS_H
    
    class my_class { ...  };
    
    #endif
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Hey thanks that helpt (renaming to class.h), thanks
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Thanks for the tip, Zach. I will keep that in mind
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Usually the bodies of the functions of a class are not placed in a headerfile, but in a sourcefile. So you should have your class definition in a file class.h and its implementation in a file class.cpp. Include the headerfile in both sourcefiles.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    18
    You'll also need constructors because you don't have any.

    Then changing the file class.h doesn't really change any thing hell it could be a .c file and you r code wouldn't work
    I need MONEY more than help with My C++ so yeah you get the idea

    C notes preferably LOL

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    In the absence of any explicitly defined constructors, the compiler will provide a do-nothing default constructor (which takes no arguments), and a copy constructor (which makes a shallow copy of your class).

    Also, there is one exception to what Shiro said. Templated classes often have to be defined in the same file they are declared (usually a header file). The reason is that many compilers (not all, but many) will choke if they are in separate files.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 01:17 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 10:09 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM

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