Solved

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

Posted on 1997-05-05
6
419 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

749 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