Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1997-05-05
6
Medium Priority
?
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 100 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

604 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