a dll export potential error?

I exported an dll of a class inherited from std::string.
I used it in another project, but there is a compilation error
"
Error 5 error LNK2005: "public: __cdecl std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QEAA@XZ) already defined in tps_frame.obj J:\ciplan_workstation\build\app\tpsUtilsd.lib(tpsUtilsd.dll)
"
I am not sure the problem was caused by the dll or lib. Until now, I have not found out the reason.

The code as following:
//line_string.h
#include <string>
#include <iostream>
class __declspec(dllexport) line_string : public std::string
{
friend std::istream& operator>>(std::istream& is, line_string& str);
public:
inline operator std::string&() { return *this; }
};
//line_string.cpp
#include "line_string.h"
std::istream& operator>>(std::istream& is, line_string& str)
{
std::getline(is, static_cast<std::string&>(str));
return is;
}
The problem is that you need to use dllimport in your other project. See:

https://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx

You can detect whether it is actually the dll or not from the predefined macro _DLL. See:

https://msdn.microsoft.com/en-us/library/b0084kay.aspx

You may write it like so:
1
2
3
4
5
#if defined(_DLL)
class __declspec(dllexport) line_string : public std::string
#else
class __declspec(dllimport) line_string : public std::string
#endif 
Or you create another macro for this.
Topic archived. No new replies allowed.