Solved

Overloaded << and >> in DLL cause "unresolved external symbol" error

Posted on 1997-05-05
6
415 Views
Last Modified: 2013-12-04
I am writing a DLL for Windows NT and I am having problems overriding the extractors for my classes.  I have recreated a simple example of the problem here (see code below).
When I compile and link I get the following linker error...


--------------------Configuration: Test - Win32 Debug--------------------
Compiling...
main.cpp
Linking...
   Creating library Debug/Test.lib and object Debug/Test.exp
main.obj : error LNK2001: unresolved external symbol "class basic_ostream<char,struct char_traits<char>> & __cdecl operator<<(class basic_ostream<char,struct char_traits<char>> &,class SmallClass const &)"(??6@YAAAV?$basic_ostream@DU?$char_traits@D@@@@AAV0@ABVSmallClass@@@Z)
Debug/Test.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Test.exe - 2 error(s), 0 warning(s)


Any suggestions?
Ed Peddycoart

//---------------------------------------------------------------------------------------------------
// main.cpp
#include <iostream>
#include <afx.h>

#include "TheDLL\SmallClass.h"


void main(void)
{

      SmallClass smcl;
      smcl.SetString(CString("This is a small string"));
      cout << smcl;
}


//---------------------------------------------------------------------------------------------------
//SmallClass.h

#ifndef __SmallClass
#define __SmallClass 1

#include <iostream>
#include <afx.h>

class _declspec (dllexport) SmallClass
{
      public:
            SmallClass();
            friend ostream &operator<<(ostream& stream, const SmallClass& obj);
            void SetString(CString newString);
            CString GetString(){ return string; }

      private:
            CString string;
};

#endif
//---------------------------------------------------------------------------------------------------
// SmallClass.cpp
#include "SmallClass.h"

SmallClass::SmallClass()
{
}


void SmallClass::SetString(CString newString)
{
      string = newString;
}

ostream &operator<<(ostream& stream, const SmallClass& obj)
{
      stream << obj.string;
      return stream;
}
0
Comment
Question by:epeddyco
  • 4
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 50 total points
ID: 1397112
You should export "ostream &operator<<(ostream& stream, const SmallClass& obj)" function. Declaring it inside exported class do not exports this function.

0
 

Author Comment

by:epeddyco
ID: 1397113
How do I do this?  As follows?

_declspec (dllexport) ostream &operator<<(ostream& stream, const SmallClass& obj)
{
stream << obj.string;
return stream;
}
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397114
If it works...I'm using Borland C++ :ostream & _export operator<<(...)In your case I suppose "ostream& _declspec(dllexport)operator<<"
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:epeddyco
ID: 1397115
I changed my operator definition to include the export

Here is the new .cpp

//-----------------------------------------------------------
#include "stdafx.h"

#include "SmallClass.h"

SmallClass::SmallClass()
{
}


void SmallClass::SetString(CString newString)
{
      string = newString;
}

ostream __declspec(dllexport) &operator<<(ostream& stream, SmallClass& obj)
{
      stream << obj.GetString();
      return stream;
}

and my SmallClass declaration is still
//----------------------------------------------
#ifndef __SmallClass
#define __SmallClass 1

#include <iostream>
#include <afx.h>

class _declspec (dllexport) SmallClass
{
      public:
            SmallClass();            
            void SetString(CString newString);
            const CString GetString(){ return string; }
            friend ostream &operator<<(ostream& stream, SmallClass& obj);
      private:
            CString string;
};
#endif

When I try to compile I get the following error...

C:\USERS\Peddycoart\Test\TheDLL\SmallClass.cpp(16) : error C2375: '<<' : redefinition; different linkage

What have I done wrong?

0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397116
1.You must insert _declspec after &:  ostream& _declspec operator<< ...2.Specify _declspec in .h-file, it's not necessary in cpp:class _declspec (dllexport) SmallClass
{
friend ostream& _declspec(dllexport) operator<<(...)};

0
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397117
Also you can place _declspec after/before 'friend' keyword.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question