首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

重写boost:any让其支持输出时候出现了链接的重定义异常

2013-08-09 
重写boost::any让其支持输出时候出现了链接的重定义错误//这是我的anny类#pragma once#include map#incl

重写boost::any让其支持输出时候出现了链接的重定义错误
//这是我的anny类
#pragma once
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <fstream>
#include <boost/any.hpp>

class CBaseImplementaion
{
public:
virtual void printAny(std::ostream& vOutStream, const boost::any& vValue) = 0;
virtual void printAny(std::ofstream& vOutStream, const boost::any& vValue) = 0;
virtual CBaseImplementaion* clone() = 0;
virtual const std::type_info&  type() = 0;

};

template<class ValueType>
class CDerivedImplementation : public CBaseImplementaion
{
public:
virtual void printAny(std::ostream& vOutStream, const boost::any& vValue)
{
std::cout<<typeid(ValueType).name()<<std::endl;
vOutStream<<std::endl<<boost::any_cast<ValueType>(vValue)<<std::endl;
}
virtual void printAny(std::ofstream& vOutStream, const boost::any& vValue)
{
std::cout<<typeid(ValueType).name()<<std::endl;
vOutStream<<std::endl<<boost::any_cast<ValueType>(vValue)<<std::endl;
}

virtual CBaseImplementaion* clone(){return new CDerivedImplementation<ValueType>();}
virtual const std::type_info&  type()
{
return typeid(ValueType);
}
};

class Anny
{
public:
Anny() : m_Any(boost::any()), m_StreamImplementation(0){}
//这里需要保证的都得到了保证;

Anny(const Anny& vValue)
: m_StreamImplementation(0 == vValue.m_StreamImplementation ? 0 : vValue.m_StreamImplementation->clone()), m_Any(vValue.m_Any){}

template<class ValueType>
Anny(ValueType& vValue) :  m_StreamImplementation(new CDerivedImplementation<ValueType>()), m_Any(vValue){}

template<class ValueType>
Anny& operator =(const ValueType& vValue)
{
//this->m_Any = vValue;
//this->m_StreamImplementation = new CDerivedImplementation<ValueType>();
Anny(vValue).swap(*this);
return *this;
}

template<class ValueType>
Anny& operator =(const Anny& vValue)
{
//this->m_Any = vValue; 
//this->m_StreamImplementation = vValue.m_StreamImplementation == 0 ? new CDerivedImplementation<ValueType>() : vValue.m_StreamImplementation;


Anny(vValue).swap(*this);
return *this;
}

void swap(Anny& vValue){std::swap(m_Any, vValue.m_Any); std::swap(m_StreamImplementation, vValue.m_StreamImplementation);}

friend std::ostream& operator<<(std::ostream& vOutStream, const Anny& vValue);
friend std::ostream& operator<<(std::ofstream& vOutStream, const Anny& vValue);

private:
boost::any m_Any;
CBaseImplementaion* m_StreamImplementation;
};

std::ostream& operator<<(std::ostream& vOutStream, const Anny& vValue)
{
if(0 != vValue.m_StreamImplementation)
vValue.m_StreamImplementation->printAny(vOutStream, vValue.m_Any);
return vOutStream;
}

std::ostream& operator<<(std::ofstream& vOutStream, const Anny& vValue)
{
if(0 != vValue.m_StreamImplementation)
vValue.m_StreamImplementation->printAny(vOutStream, vValue.m_Any);
return vOutStream;
}

//这是我的输出类
#pragma  once
#include "Anny.h"
class COutputToConsoleTarget
{
public:
void outputToConsoleTarget(const Anny& vValue)
{
std::cout<<vValue;
}
};

//main函数是这样的:
#pragma once
#include "Anny.h"
#include "OutputToConsoleTarget.h"
#include <string>
void main()
{
COutputToConsoleTarget OutputToConsoleTarget;
Anny AnyTemp1 = std::string("yuanzan");
OutputToConsoleTarget.outputToConsoleTarget(AnyTemp1);
}

但是总是出现这样的错误!!!
1>OutputToConsoleTarget.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Anny const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVAnny@@@Z) already defined in main.obj
1>OutputToConsoleTarget.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ofstream<char,struct std::char_traits<char> > &,class Anny const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV?$basic_ofstream@DU?$char_traits@D@std@@@1@ABVAnny@@@Z) already defined in main.obj
1>E:\学习资料1\C++\MyBoostPractice\Debug\LogSystemWithAny.exe : fatal error LNK1169: one or more multiply defined symbols found



求大神指导。 boost::any
[解决办法]
头文件里不要直接写函数的实现,如果非要写,加上inline限定符
[解决办法]
顺便,没有必要给ofstream再加一个重载
[解决办法]

std::ostream& operator<<(std::ofstream& vOutStream, const Anny& vValue)
 {
 if(0 != vValue.m_StreamImplementation)
 vValue.m_StreamImplementation->printAny(vOutStream, vValue.m_Any);
 return vOutStream;
 }


在头文件中申明,定义放在源码文件

热点排行