By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

faq error LNK2005: ...already defined

P: n/a

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.
Mar 12 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 12 '07 #2

P: n/a
fcvcnet wrote:
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
Mar 12 '07 #3

P: n/a

"fcvcnet" <fc*****@163.comwrote in message
news:et**********@news.cn99.com...
>
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?
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.

Mar 12 '07 #4

P: n/a
On Mar 13, 12:45 am, "Alf P. Steinbach" <a...@start.nowrote:
* fcvcnet:
enum TLSC {PARALLEL, INTERSECT, COINSIDE,INTERSECTATDIASTOLE}
twolinesolutioncases;

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?
Mar 13 '07 #5

P: n/a
* Old Wolf:
On Mar 13, 12:45 am, "Alf P. Steinbach" <a...@start.nowrote:
>* fcvcnet:
>>enum TLSC {PARALLEL, INTERSECT, COINSIDE,INTERSECTATDIASTOLE}
twolinesolutioncases;
Java'ism: don't use all uppercase names except for macros.

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/.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 13 '07 #6

P: n/a
Thanks you so much.
Mar 13 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.