473,698 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

7.1: Compiler/linker issue with operators

Hi,

I've found some strange behaviour with some smart pointers. Consider this
little sample:

// *************** *************** *************** *************
struct MyStruct {};

template <typename T>
struct Wrapper
{
explicit Wrapper(T* p) : m_ptr(p) {}
private:
T *m_ptr;
friend bool operator < (const Wrapper <T>& left, const Wrapper <T>&
right);
};

template <class T>
inline bool operator < (const Wrapper <T>& left, const Wrapper <T>& right)
{
return *(left.m_ptr) < *(right.m_ptr); // forward!
}

inline bool operator < (const MyStruct& left, const MyStruct &right)
{ return true; }

int main()
{
MyStruct a, b;
Wrapper<MyStruc t> wa(&a), wb(&b);

wa < wb;
}
// *************** *************** *************** *************
There is a wrapper class (that could be a smart pointer) which forwards
operator < to its wrapped class.
This code is compiled without problems but the linker complains about an
unresolved external, which is very strange:

error LNK2019: unresolved external symbol "bool __cdecl operator<(struc t
Wrapper<struct MyStruct> const &,struct Wrapper<struct MyStruct> const &)"
(??M@YA_NABU?$W rapper@UMyStruc t@@@@0@Z) referenced in function _main

Regards, Stefan
Nov 16 '05 #1
1 1326
Stefan Slapeta wrote:
Hi,

I've found some strange behaviour with some smart pointers. Consider
this little sample:


This should be an FAQ for VC7.1 :)

The problem is, your code is not conforming, and VC7 accepted it. You need
to make a slight change to make it standard conforming. As posted, your
code declares a non-template operator < as a friend, then provides a
templated operator < that has the same signature. Unfortunately, VC7
accepted this, but it's not valid according to the standard.

-cd

// *************** *************** *************** *************
struct MyStruct {};

/* +++ You need these forwards +++ */
template <class T> struct Wrapper;
template <class T> inline bool operator < (
const Wrapper<T>& left,
const Wrapper<T>& right
);

template <typename T>
struct Wrapper
{
explicit Wrapper(T* p) : m_ptr(p) {}
private:
T *m_ptr;

/* +++ You need to delcare an instantiation of the
operator template as a friend +++ */
friend bool operator < <T> (
const Wrapper& left,
const Wrapper& right
);
};

template <class T>
inline bool operator < (
const Wrapper <T>& left,
const Wrapper <T>& right
)
{
return *(left.m_ptr) < *(right.m_ptr); // forward!
}

inline bool operator < (
const MyStruct& left,
const MyStruct &right
)
{
return true;
}

int main()
{
MyStruct a, b;
Wrapper<MyStruc t> wa(&a), wb(&b);

wa < wb;
}
// *************** *************** *************** *************

-cd
Nov 16 '05 #2

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

Similar topics

4
2198
by: Jeremy Watts | last post by:
Hi, I'm completely new to C++ and wondered if anyone has a recommendation for a C++ compiler Thanks
7
3105
by: Tao Wang | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I saw cuj's conformance roundup, but the result is quite old. I think many people like me want to know newer c++ standard conformance test result. Especially, I want to know the compiler conformance of vc7.1, vc8.0, gcc 4.x and intel c++, and also each implementation of STL. The world is moving on, right? :)
5
1946
by: Ondrej Spanel | last post by:
I though that inline functions should be always visible only in the compilation unit where they are defined - meaning if compiler cannot inline them, they should be handled as if declared static. However sample attached shows VC compiler does not work this way (tested in .NET 2003). When you compile the sample with inlining enabled (like in default Release config), the output is A1 = 1, A2 = 2. When run with inlining disabled (Debug),...
4
2874
by: wenmang | last post by:
Hi all, I got following error when compiling a rogram: prelink: ERROR: operator <<<T1, T2, T3>(ostream &, const basic_string<T1, T2, T3&) assigned to a.o and b.o prelink: ERROR: bad instantiation request file -- instantiation assigned to more than one file UX:CC: ERROR: Pre-linker failed In both places, I use cout as ostream, not sure why it happen. I build
3
9365
by: NewsGroup | last post by:
Hello, I am trying to write a code to interface with ILOG OPL development studio using their C++ libraries and header files. But I am encountering linker issues, an example being - error LNK2019: unresolved external symbol "public: void __thiscall IloOplModelI::printSolution(class std::basic_ostream<char,struct std::char_traits<char &,long)" (?printSolution@IloOplModelI@@QAEXAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@J@Z)
7
3181
by: Nena | last post by:
Hi there, I'm trying to include the Numerical Recipes for C++ for the Dev-C++ Editor Version. But so far I've failed. Therefore I've copied all head-files (for example nr.h) into the folder where my program is. When compiling my program two windows are poping up. The first one is saying "Total errors 0" and "Size of
42
3438
by: jacob navia | last post by:
http://slashdot.org/ "The leaner, lighter, faster, and most importantly, BSD Licensed, Compiler PCC has been imported into OpenBSD's CVS and NetBSD's pkgsrc. The compiler is based on the original Portable C Compiler by S. C. Johnson, written in the late 70's. Even though much of the compiler has been rewritten, some of the basics still remain. It is currently not bug-free, but it compiles on x86 platform, and work is being done on it to...
4
248
by: Comcast | last post by:
Can anyone help: When I compile this code, I'm getting undefined reference to 'getData(WeatherStats*,int)' I'm not sure what is causing the error. Thanks Art
20
4291
by: Nickolai Leschov | last post by:
Hello all, I am programming an embedded controller that has a 'C' library for using its system functions (I/O, timers, all the specific devices). The supplied library has .LIB and .H files. How can I dynamically load a LIB file and access all its functions? Surely someone has solved similar task? My intention is to use a Forth system for programming the controller,
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8862
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.