Solved

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

Posted on 1997-05-05
6
412 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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.…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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 is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

26 Experts available now in Live!

Get 1:1 Help Now