faq error LNK2005: ...already defined

F

fcvcnet

Hi all,
I defined a class, as fellows:
// Segment.h
#pragma once

#include "MyPoint.h"

enum TLSC {PARALLEL, INTERSECT, COINSIDE,INTERSECTATDIASTOLE}
twolinesolutioncases;

class CSegment
{
public:
CSegment(void);
void Clear();
public:
~CSegment(void);
private:
....
....
....
friend TLSC InterSect(const CSegment &sgmt1, const CSegment &sgmt2,
CMyPoint &intersectpoint);
....
};

And at the file segment.cpp below I defined a function to be a global
function
// Segment.cpp
#include "StdAfx.h"
#include "Segment.h"

CSegment::CSegment(void)
: m_pointlist(0)
, m_index(0)
, m_createdirection(true)
{
}

CSegment::~CSegment(void)
{
}
....

TLSC InterSect(const CSegment &sgmt1, const CSegment &sgmt2, CMyPoint
&intersectpoint)
{
double ua,ub;//ub can be delete
double a,b,c;
....
if (0==c)
{
if (a==b==0)
{
return COINSIDE;
}
return PARALLEL;
}
....
if ( ua < 0 || ua > 1 || ub < 0 || ub > 1)
{
return INTERSECTATDIASTOLE;
}
....
return INTERSECT;

}

when I compile ,

Segment.cpp
Linking...
borderView.obj : error LNK2005: "enum TLSC twolinesolutioncases"
(?twolinesolutioncases@@3W4TLSC@@A) already defined in border.obj
Segment.obj : error LNK2005: "enum TLSC twolinesolutioncases"
(?twolinesolutioncases@@3W4TLSC@@A) already defined in border.obj
E:\border\0312\border\Debug\border.exe : fatal error LNK1169: one or more
multiply defined symbols found
Build log was saved at
"file://e:\border\0312\border\border\Debug\BuildLog.htm"
border - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

How to solove this errors?
Thanks.
 
A

Alf P. Steinbach

* fcvcnet:
Hi all,
I defined a class, as fellows:
// Segment.h
#pragma once

This effect of this #pragma, if any, depends on the compiler.

#include "MyPoint.h"

enum TLSC {PARALLEL, INTERSECT, COINSIDE,INTERSECTATDIASTOLE}
twolinesolutioncases;

Java'ism: don't use all uppercase names except for macros.


class CSegment
{
public:
CSegment(void);
void Clear();
public:
~CSegment(void);
private:
...
...
...
friend TLSC InterSect(const CSegment &sgmt1, const CSegment &sgmt2,
CMyPoint &intersectpoint);
...
};

And at the file segment.cpp below I defined a function to be a global
function
// Segment.cpp
#include "StdAfx.h"

This is not a standard header. In addition it indicates you're using a
feature of Visual C++ called "precompiled headers", where the compiler
will not abide by standard C++ rules. Nothing more can be said until
you turn off that feature and remove this header.

Post a small, standard C++ program that exhibits the problem, if it's
still there when you remove the compiler-specific stuff.
 
S

Stuart Redmann

fcvcnet said:
Hi all,
I defined a class, as fellows:
// Segment.h
#pragma once

#include "MyPoint.h"

enum TLSC {PARALLEL, INTERSECT, COINSIDE,INTERSECTATDIASTOLE}
twolinesolutioncases;

You declare a global variable called "twolinesolutioncases" of type TLSC
here. That is not a good thing to do in a header file. I guess you
forgot to make this a typedef.
class CSegment

[snipped rest of code]
when I compile ,

Segment.cpp
Linking...
borderView.obj : error LNK2005: "enum TLSC twolinesolutioncases"
(?twolinesolutioncases@@3W4TLSC@@A) already defined in border.obj
Segment.obj : error LNK2005: "enum TLSC twolinesolutioncases"
(?twolinesolutioncases@@3W4TLSC@@A) already defined in border.obj

I think the compiler messages are pretty descriptive.

Regards,
Stuart
 
J

Jim Langston

fcvcnet said:
Hi all,
I defined a class, as fellows:
// Segment.h
#pragma once

#include "MyPoint.h"

enum TLSC {PARALLEL, INTERSECT, COINSIDE,INTERSECTATDIASTOLE}
twolinesolutioncases;

The line above declares an instance of TLSC called twolinesolutioncases.
Any seperation compilation unit (.obj) which has a .cpp which includes this
header will have an instance. So when you go to link, the linker says, hey,
wait, you got a bunch of twolinessolutionscases, which one am I supped to
use?

There are a few solutions to this depending on what you are trying to do.
One solutions is to make this

extern TLSC twolinesolutioncases;
and then in one, and only one of your compiliatin units (pick a .cpp) you
have
TLSC twolinesolutioncases;

so there is only one instance, and the extern says, it exists in one of the
compiliation units, find it linker.

That is, if you really need twolinesolutioncases to be global. Do you?
 
O

Old Wolf

* fcvcnet:


Java'ism: don't use all uppercase names except for macros.

I was using this convention before Java even existed.. (and still do)

Why do you think using upper case for enumerated constants is bad?
 
A

Alf P. Steinbach

* Old Wolf:
I was using this convention before Java even existed.. (and still do)

For that matter, Dennis Ritchie also did. He also used all uppercase
for typedef'ed names. Very useful.[1]

Why do you think using upper case for enumerated constants is bad?

You mean, why is it bad.

It's bad because (1) it increases the chance of name collisions with
macros, and (2) because all caps is visually distracting.

Why it's a good idea to keep the macro "namespace" as separate as
possible: macros don't respect namespaces or scopes.



Notes:
[1] Nowadays many people are so insanely stupid, especially in the
country infamous for "Warning: the coffee is hot!", that it's dangerous
to use obvious irony lest one be misunderstood, so in the manner of CNN
stating that their story of wilful, bored robots on Mars was irony, here
goes: the statement about the usefulness of all caps was /irony/.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top