Re: Easy Question - LINK2005 error
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 05 Jul 2007 17:26:32 -0400
See below...
On Thu, 05 Jul 2007 12:43:00 -0700, bones288 <b_t_k@xxxxxxxxxxx> wrote:
////////// main.cpp //////////****
#include <iostream>
#include "try.cpp"
Wouldn't you have meant
#include "try.h"
since it seems strange to include a .cpp file
*****
using namespace std;*****
int main()
{
Num Try;
return 0;
}
//////// try.cpp //////////
#include <iostream>
I'm curious why this includes iostream since there is nothing here that would use it.
*****
*****
class Num{
private:
int number;
public:
void SetNumber(int);
};
void SetNumber(int a)
{
}
This makes no sense for several reasons. First, it is an implementation in a header file
that is included elsewhere. If it is included in two files, it would create duplicate
symbol definitions.
I think you intended to write
try.h:
class Num {
private:
int number;
public:
void SetNumber(int);
};
try.cpp
#include "stdafx.h"
#include "try.h"
void Num::SetNumber(int a)
{
number = a;
}
Note that the name is NOT "SetNumber" but "Num::SetNumber". You defined a function which
by the merest coincidence had the same name as a member function of a class, but in fact
has absolutely no relationship to the class; it is a global function. It would be
declared in a .cpp file that was NOT included in some other compilation by doing #include.
In the correct implementation, the try.cpp file would contain the definition of the class
member, as shown above.
joe
*****
Joseph M. Newcomer [MVP]
///////////OUTPUT GENERATED//////////
------ Rebuild All started: Project: try_examples, Configuration:
Debug Win32 ------
Deleting intermediate and output files for project 'try_examples',
configuration 'Debug|Win32'
Compiling...
try.cpp
try_example.cpp
.\main.cpp(9) : warning C4101: 'Try' : unreferenced local variable
Generating Code...
Linking...
try.obj : error LNK2005: "void __cdecl SetNumber(int)" (?
SetNumber@@YAXH@Z) already defined in try_example.obj
try.obj : error LNK2005: "void __cdecl SetNumber(int)" (?SetNumber@@$
$FYAXH@Z) already defined in try_example.obj
try_examples - 3 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
This is a seriously simplified version of my problem so I'm hoping
that someone can give me an explanation as to what's going on here?
In particular the LNK2005 errors.
Thanks in advance!
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- Easy Question - LINK2005 error
- From: bones288
- Easy Question - LINK2005 error
- Prev by Date: Re: Easy Question - LINK2005 error
- Next by Date: Problem with Base64 Decoding for a CBitmap
- Previous by thread: Re: Easy Question - LINK2005 error
- Next by thread: Re: Easy Question - LINK2005 error
- Index(es):
Relevant Pages
|