[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1049
  • Last Modified:

Function already defined Error, Visual C++

hey,

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:

Input.cpp


class Input
{
public:
      int RunsScored[11];
      void Score0();
};
void Input::Score0()
{
      RunsScored[0]=1;
}


in the main function i reference it this way:

#include "Input.cpp"

and call it this way

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


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

Thanks
0
alzzz
Asked:
alzzz
  • 2
2 Solutions
 
alzzzAuthor Commented:
thanking you
0
 
Infinity08Commented:
>> #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;
  in.Score0();
  return 0;
}

Open in new window

0
 
Anthony2000Commented:
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?
0
 
alzzzAuthor Commented:
thanks to you both
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now