Function already defined Error, Visual C++

Posted on 2009-02-23
Last Modified: 2013-12-14

I'm trying to create a GUI in Visual Studio, but when i run through that piece of code but i get the errors:

Error      1      error LNK2005: "public: void __clrcall Input::Score0(void)" (?Score0@Input@@$$FQAMXXZ) already defined in cricket_i1_v00.obj      Input.obj      cricket_i1_v00

Error      3      fatal error LNK1169: one or more multiply defined symbols found      C:\Documents and Settings\hlepretre\MyC++Codes\cricket_i1_v00\Debug\cricket_i1_v00.exe      1      cricket_i1_v00

My code is:


class Input
      int RunsScored[11];
      void Score0();
void Input::Score0()

in the main function i reference it this way:

#include "Input.cpp"

and call it this way

Input ins;
this->txtB->Text = ins.RunsScored[0].ToString();

Do you have an idea of where the error comes from?

Question by:alzzz

    Author Comment

    thanking you
    LVL 53

    Accepted Solution

    >> #include "Input.cpp"

    Don't include the .cpp file. Instead, create a .h file, and include that :
    ---- input.h ----
    #ifndef INPUT_H
    #define INPUT_H
    class Input {
      public :
        int RunsScored[11];
        void Score0();
    #endif /* INPUT_H */
    ---- input.cpp ----
    #include "input.h"
    void Input::Score0() {
      RunsScored[0] = 1;
    ---- main.cpp ----
    #include "input.h"
    int main(void) {
      Input in;
      return 0;

    Open in new window

    LVL 8

    Assisted Solution

    Your are getting the error because you are linking in your cricket_i1_v00.cpp (I am guessing this is where your main is located), you are including the input.cpp and in your project you probably have input.cpp as one of source file to your project. So, you are compiling the source "input.cpp" twice in essence. This is why Infinity08 is suggesting that you create an .h file. The basic convention is that declarations get placed in a .h file and object definitions get placed in the .cpp file.

    There is no magic to the #include statement. You might as cut and pasted the code at the point where the #include is.

    Does help answer your question?

    Author Closing Comment

    thanks to you both

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now