【问题标题】:Conflict between includes: stdafx.h and <vector> VisualStudio 2012 C++ (error C2953: class template has already been defined)包括之间的冲突:stdafx.h 和 <vector> VisualStudio 2012 C++(错误 C2953:已定义类模板)
【发布时间】:2012-12-03 19:24:57
【问题描述】:

我在这个问题上卡了太久,必须问全能的堆栈。请原谅问题的简单性!

我正在使用 VisualStudio 2012 中的 BowlingGame kata 来练习他们的单元测试版本。我在测试 #2 的重构部分的中间,我在我的 Game 类中添加了一个向量,以保存滚动的引脚数。

但是,当我将向量包含添加到我的头文件时,我遇到了编译器错误!事实上,是我的单元测试类抛出了编译器错误(不是我的源代码)。

我认为问题是单元测试类中的 stdafx.h 包含与源代码标头中的向量包含之间的冲突,但我不确定。它也可能是托管/本机 C++ 问题?在这个练习中,我必须完成其中的一些。

下面是编译器错误(缩写,因为它一直在继续)和到目前为止我的三个文件的代码。请让我知道我能做些什么来解决这个问题。我正在尝试在此练习中使用“最佳实践”,因此如果您发现任何其他看起来可疑的内容,请告诉我!

1>------ Build started: Project: BowlingGameUnitTests, Configuration: Debug Win32 ------
1>  BullseyeCoverage Compile C++ 8.7.21 Windows License 7978 
1>  Copyright (c) Bullseye Testing Technology 1990-2012
1>CL : Command line warning d9999: changing /clr:safe to -clr
1>  BowlingGameTest.cpp
1>c:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/include/xrefwrap(156): warning C4561: '__fastcall' incompatible with the '/clr' option: converting to '__stdcall'
1>c:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/include/xrefwrap(156): error C2953: 'std::_Result_of<_Ret(__stdcall &)(void),std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>' : class template has already been defined
1>          c:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/include/xrefwrap(156) : see declaration of 'std::_Result_of<_Ret(__stdcall &)(void),std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>'
1>c:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/include/xrefwrap(156): error C2953: 'std::_Result_of<_Ret(__stdcall *)(void),std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>' : class template has already been defined
1>          c:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/include/xrefwrap(156) : see declaration of 'std::_Result_of<_Ret(__stdcall *)(void),std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>'

游戏.h:

#pragma once
#include <vector>
using namespace std;

class Game
{
public:
    Game(void);
    ~Game(void);
    void roll( int pins );
    int score();

private:
    int totalScore;
    vector<int> rolls;
    int currentRoll;
};

游戏.cpp:

#include "stdafx.h"
#include "Game.h"

Game::Game(void)
{
    totalScore = 0;
    currentRoll = 0;
}

Game::~Game( void )
{
}

void Game::roll( int pins )
{
    totalScore += pins;
    rolls.push_back(pins);
}

int Game::score()
{
    return totalScore;
}

BowlingGameTest.cpp:

#include "stdafx.h"
#include "../BowlingGame/Game.h"

using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;

namespace BowlingGameUnitTests
{
    [TestClass]
    public ref class BowlingGameTest
    {
    private:
        Game* game;

    public: 
        [TestInitialize()]
        void MyTestInitialize() 
        {
            game = new Game();
        }

        void rollMany( int frames, int pins ) 
        {
            for (int i = 0; i < frames; i++)
            {
                game->roll(pins);
            }
        }

        [TestMethod]
        void gutterGame()
        {
            rollMany(20, 0);
            Assert::AreEqual<int>(0, game->score());
        }

        [TestMethod]
        void testAllOnes()
        {
            rollMany(20, 1);
            Assert::AreEqual<int>(20, game->score());
        }
    };
}

【问题讨论】:

  • 如果您删除 using 命名空间并完全限定您的命名空间调用会发生什么?
  • 这是一个 C++/CLI 项目吗(从你的编译器输出看它是这样的)。如果是这样,我很确定您不能使用 std::vector,而是需要使用 cliext vector
  • 嗯,从 std::vector 更改为 cliext vector 给了我更多的托管错误和非托管错误。我想知道我是否可能错误地设置了我的解决方案(或困难的方式)?我有一个带有“托管测试项目”的“Win32 项目”。
  • 这可能是问题所在。如果你可以让你的“游戏”类生成一个 dll,那么让测试项目包含你可能能够解决它的参考,但它可能会是一场让它工作的战斗。另一方面,如果您使用的是 VS2012,它们已经内置了对 C++ 单元测试的支持。然后你可以使用 std::vector 并且不要陷入尝试将代码编译为托管和非托管的问题。
  • 我将使用这些instructions. 重新开始一个全新的解决方案@祝我好运!

标签: c++ include compiler-errors visual-studio-2012


【解决方案1】:

感谢 pstrjds!

回答第 1 部分:将 std::vector 更改为 cliext vector,因为我的单元测试项目是 C++/CLI 项目。

回答第 2 部分:重新配置解决方案。目前,它是本地源代码和托管单元测试,但这会导致很多棘手的问题。将更改为原生源代码和原生单元测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多