473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

how to deal with duplicate class names

i'm somewhat of a c++ newbie. i'm linking some static libraries into
my app. it turns out that two of the libraries (from different
suppliers) share a class name; e.g. they both have a class named
'SomeClass'. the compiler/linker is complaining. is there an easy
way deal with this so my app can link? thanks.
May 8 '06 #1
10 3460
use namespaces, this is exactly what they are for.

May 8 '06 #2
4MLA1FN wrote:
i'm somewhat of a c++ newbie. i'm linking some static libraries into
my app. it turns out that two of the libraries (from different
suppliers) share a class name; e.g. they both have a class named
'SomeClass'. the compiler/linker is complaining. is there an easy
way deal with this so my app can link? thanks.


Create proxy class. That is, one that implements all of the same
methods as one of the classes with which you are having problems, and
have it simply forward all method calls to an instance of the class for
which it is proxying. If needed, use the PIMPL idiom (see Google).

--
Alan Johnson
May 8 '06 #3
* 4MLA1FN:
i'm somewhat of a c++ newbie. i'm linking some static libraries into
my app. it turns out that two of the libraries (from different
suppliers) share a class name; e.g. they both have a class named
'SomeClass'. the compiler/linker is complaining. is there an easy
way deal with this so my app can link?


No, not as far as I know, sorry.

But you can pursue solutions outside the language.

For example, in Windows you can isolate the usage of one of the classes
to a dynamically linked library.

Or, for example, you can check whether the same functionality is
available from some higher quality libraries, which you should probably
do anyway -- using low-quality libraries will yield more and more
problems, and can force you to use less than good design.

A quality C++ library will use a namespace to greatly reduce the chance
of name collisions, so that's one thing to look for.

--
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?
May 8 '06 #4

Alan Johnson wrote:
4MLA1FN wrote:
i'm somewhat of a c++ newbie. i'm linking some static libraries into
my app. it turns out that two of the libraries (from different
suppliers) share a class name; e.g. they both have a class named
'SomeClass'. the compiler/linker is complaining. is there an easy
way deal with this so my app can link? thanks.


Create proxy class. That is, one that implements all of the same
methods as one of the classes with which you are having problems, and
have it simply forward all method calls to an instance of the class for
which it is proxying. If needed, use the PIMPL idiom (see Google).


This does not solve the problem. The problem is that One Definition
Rule is violated and you have several different external linkage
entities with the same name in your application. Adding a proxy to the
mix does not make the entity being proxied go away.

May 8 '06 #5
Maxim Yegorushkin wrote:

Alan Johnson wrote:
4MLA1FN wrote:
> i'm somewhat of a c++ newbie. i'm linking some static libraries into
> my app. it turns out that two of the libraries (from different
> suppliers) share a class name; e.g. they both have a class named
> 'SomeClass'. the compiler/linker is complaining. is there an easy
> way deal with this so my app can link? thanks.


Create proxy class. That is, one that implements all of the same
methods as one of the classes with which you are having problems, and
have it simply forward all method calls to an instance of the class for
which it is proxying. If needed, use the PIMPL idiom (see Google).


This does not solve the problem. The problem is that One Definition
Rule is violated and you have several different external linkage
entities with the same name in your application. Adding a proxy to the
mix does not make the entity being proxied go away.


Well, with standard C++, you're right. But the linker might offer some way
to only export symbols that are explicitly marked for that (e.g. in a
linker script). That can be used to make only the proxy class visible for
the main program.

May 8 '06 #6
yu*****@gmail.com wrote:
use namespaces, this is exactly what they are for.


I think the OP doesn't have that choice. Read the post again and you'll
find it is the library code (not OP's code) that caused the problem.

Putting library code in your own namespace is not only a substantial
work but is also unsupported, tedious, and error-prone.

Regards,
Ben
May 8 '06 #7

Rolf Magnus wrote:
Maxim Yegorushkin wrote:

Alan Johnson wrote:
4MLA1FN wrote:
> i'm somewhat of a c++ newbie. i'm linking some static libraries into
> my app. it turns out that two of the libraries (from different
> suppliers) share a class name; e.g. they both have a class named
> 'SomeClass'. the compiler/linker is complaining. is there an easy
> way deal with this so my app can link? thanks.

Create proxy class. That is, one that implements all of the same
methods as one of the classes with which you are having problems, and
have it simply forward all method calls to an instance of the class for
which it is proxying. If needed, use the PIMPL idiom (see Google).


This does not solve the problem. The problem is that One Definition
Rule is violated and you have several different external linkage
entities with the same name in your application. Adding a proxy to the
mix does not make the entity being proxied go away.


Well, with standard C++, you're right. But the linker might offer some way
to only export symbols that are explicitly marked for that (e.g. in a
linker script). That can be used to make only the proxy class visible for
the main program.


In standard C++ one had best be using namespaces to solve the problem.

May 8 '06 #8
Maxim Yegorushkin wrote:
>> Create proxy class. That is, one that implements all of the same
>> methods as one of the classes with which you are having problems, and
>> have it simply forward all method calls to an instance of the class
>> for which it is proxying. If needed, use the PIMPL idiom (see
>> Google).
>
> This does not solve the problem. The problem is that One Definition
> Rule is violated and you have several different external linkage
> entities with the same name in your application. Adding a proxy to the
> mix does not make the entity being proxied go away.


Well, with standard C++, you're right. But the linker might offer some
way to only export symbols that are explicitly marked for that (e.g. in a
linker script). That can be used to make only the proxy class visible for
the main program.


In standard C++ one had best be using namespaces to solve the problem.


With "one" in this case being those that wrote the libraries that the OP is
using. Noting that namespaces are there to avoid that problem won't help
the OP solving it.

May 8 '06 #9
4MLA1FN posted:
i'm somewhat of a c++ newbie. i'm linking some static libraries into
my app. it turns out that two of the libraries (from different
suppliers) share a class name; e.g. they both have a class named
'SomeClass'. the compiler/linker is complaining. is there an easy
way deal with this so my app can link? thanks.

Maybe you could find some tool for altering the static library file? (Even
a text editor?). If you find such a tool, the handiest solution would be:

1) Put everything in a namespace.
Failing that, you could:

2) Rename the classes.
You'd probably need a fancy tool to achieve the first choice, but I'd say
that something as trivial as a HexEditor could achieve the second choice.
-Tomás
May 8 '06 #10
On Mon, 08 May 2006 08:38:16 +0200, "Alf P. Steinbach"
<al***@start.no> wrote:

No, not as far as I know, sorry.
thank you very much alf and all others that posted. i was
hoping/fantasizing that there was a way to prefix symbols in a library
at link time. guess not. i suspect i can get the source for the
smaller of the two libraries, so i'll try to do the namespace
solution. (not exactly sure how just yet; i think i need to put the
class in a namespace x, then in each .h/.cpp where the class is used,
i need to add a 'using x' declaration?? ). alternatively, i'll make a
little perl script to search-and-replace the class name to something
unique. (ugly solution, i know, but it's quick.)
Or, for example, you can check whether the same functionality is
available from some higher quality libraries...


fortunately, these are libraries from pretty escoteric academic
research, which increases the likelihood i can get the source.
May 8 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
7
by: Voetleuce en fênsievry | last post by:
Hello everyone. I'm not a JavaScript author myself, but I'm looking for a method to remove duplicate words from a piece of text. This text would presumably be pasted into a text box. I have,...
3
by: shine | last post by:
I am trying to add an integer array to a hashtable. While adding to hashtable I want to check the duplicates in array, the way I want to do is add the key(the integer stored in array) if a...
23
by: Steve Jorgensen | last post by:
Hi all, I'm working on a project through a consulting company, and I'm writing some database code for use in another programmer's project in Excel/VBA. The other programmer is working through...
8
by: Iona | last post by:
Hi Allan, I'm using a nifty piece of code you put on here some time back to do a duplicate entry check as below. I'm using to check for duplicate names. However I am getting an error message on...
4
by: whatnameisnottaken | last post by:
right now i have gotten all the classes to print out using helpdbg. Now i want to remove the duplicate class names out of an array so i can just print one of each. This is the output i have. ...
4
by: | last post by:
Given an XML file (dataset.writexml), here is my output (simplified for this posting): <?xml version="1.0" standalone="yes"?> <NewDataSet> <Category> <CategoryId>80</CategoryId>...
5
by: Brett Barry: Go Get Geek! | last post by:
Hello, Can someone please paste VBA code and how I would go about doing this?: I have a customer table with over 6000 duplicates in the field called "Customer". While the customer names may...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.