Solved

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

Posted on 1997-05-05
6
411 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now