生成可视C++ 2008 项目时出现链接器错误



我最近接到了工作任务,尝试将一个C++项目从旧的Windws 2003服务器迁移到Windows 2008。 目前,我正在尝试在我的Windows 7PC上本地构建应用程序。 我遵循了收到的安装指南。事情似乎进展顺利。 我遇到的问题是,在编译步骤之后,链接器给了我以下错误:

msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::operator<<<struct std::char_traits<char> >(class std::basic_ostream<char,struct std::char_traits<char> > &,char const *)" (??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z) already defined in CellSortTypeIds.obj

顺便说一下,这是在调试模式下运行的。在发布模式下,我收到相同的错误。 我可以给你看相应的CPP文件:

  1. CellSortTypeIds.h 文件:

    #ifndef CELL_SORT_TYPE_IDS_H
    #define CELL_SORT_TYPE_IDS_H
    #include <iostream>
    #include <QtCore/QString>
    namespace CellSortTypeIds
    {
    enum CellSortTypeEnum 
    {
    NAME=0, LAC, CI, NB_ITEMS
    };
    static const QString mStrings[] = 
    {
    QT_TR_NOOP("Tri par code Nom"), QT_TR_NOOP("Tri par code LAC"), QT_TR_NOOP("Tri par code CI")
    };
    QString getQString(const CellSortTypeIds::CellSortTypeEnum aCellSortType);
    CellSortTypeEnum getCellSortTypeFromQString( QString aCellSortType );
    }
    std::ostream& operator <<(std::ostream &, const CellSortTypeIds::CellSortTypeEnum&); 
    #endif //CELL_SORT_TYPE_IDS_H
    
  2. CellSortTypeIds.cpp 文件

    #include "CellSortTypeIds.h"
    #include <QtCore/QObject>
    using namespace std;
    ostream& operator <<(ostream &out, const CellSortTypeIds::CellSortTypeEnum& aCellSortType)
    {
    out << CellSortTypeIds::getQString(aCellSortType).toAscii().operator const char*();
    return out;
    } 
    QString CellSortTypeIds::getQString(const CellSortTypeIds::CellSortTypeEnum aCellSortType)
    {
    QString result("");
    if( aCellSortType < CellSortTypeIds::NB_ITEMS )
    {
    result = QObject::tr( CellSortTypeIds::mStrings[aCellSortType].toAscii().data() );
    }
    else
    {
    cerr << __FILE__<< "(" <<__LINE__ << "): mStrings[" << (int)aCellSortType << "] not initialised" << endl;
    }
    return result;
    }
    CellSortTypeIds::CellSortTypeEnum CellSortTypeIds::getCellSortTypeFromQString( QString aCellSortTypeString )
    {
    CellSortTypeIds::CellSortTypeEnum theEnum( CellSortTypeIds::NAME );
    bool found( false );
    for( int index( 0) ; index < CellSortTypeIds::NB_ITEMS && !found ; index++ )
    {
    if( QObject::tr(CellSortTypeIds::mStrings[ index ].toAscii().data()) == aCellSortTypeString )
    {
    theEnum = (CellSortTypeIds::CellSortTypeEnum)index;
    found = true;
    }
    }
    return theEnum;
    }
    

我的C++知识不是那么好。我已经阅读了几篇关于 SO 的关于这个问题的文章,一些讲述了配置的运行时,一些讲述了不在头文件中定义运算符,而是将它们放在 cpp 文件中。在这里,我认为情况并非如此。

我怀疑这两个文件中存在我无法弄清楚的问题。 任何想法都是值得赞赏的。如果您需要更多详细信息,请告诉我。 提前感谢您,请不要犹豫,对问题的结构提供任何反馈,因为毕竟这是我的第一个问题。

我要感谢大家发表他们的想法/经验。 卸载Visual C++ 2005及其可分发包后,我不再遇到此问题。我相信此安装可能造成了一些干扰(也可能通过系统变量(。构建运行,古老的应用程序恢复。 周末愉快!

最新更新