c++ - 我怎样才能结交 AND TEST CLASS(SomeTest ClassName) friend

标签 c++

如何让 VS TEST_CLASS(SomeTestClassName) 成为我正在测试的类的 friend ? 我是 c++ 的新手,在网上找不到任何信息。 如果是简单的添加

friend class SomeTestClassName;

到被测试的类的标题 - 这是行不通的。

添加示例:

#include "..\CoreTests\ChallengeManagerTests.cpp" //Circular reference? But forward declaration don't works

class ChallengeManager
{
private:
 void PrivateMethod();
public:
 void PublicMethod();
 friend class CoreTests::ChallengeManagerTests; // Compiler can't find ChallengeManagerTests in CoreTests namespace. Forward declaration give class redefinition error.
}

//ChallengeManagerTests:
#include "stdafx.h"
#include "CppUnitTest.h"
#include "..\Core\ChallengeManager.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace Brans;
namespace CoreTests
{
    TEST_CLASS(ChallengeManagerTests)
    {
    public:

        TEST_METHOD(RandomInputs)
        {
            Brans::ChallengeManager cm;
            cm. // here i got is not accessible error for private members 

        }

    };
}

最佳答案

从您删除的上一个问题来看,您的测试类名称是在命名空间内定义的,因此您需要在 friend 声明中提供相同的命名空间。

关于c++ - 我怎样才能结交 AND TEST CLASS(SomeTest ClassName) friend ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19520670/

相关文章:

C++ 列表迭代器 reverse_iterator

C++ recv() 问题

c++ - 通过 setsockopt() 禁用套接字超时

c++ - 如何判断函数是否使用系统调用

c++ - 使用 std::tr1::function 的成员回调函数出现“不匹配”错误

c++ - opencv c++ 找到轮廓的内接圆

c++ - C++中的switch语句错误

c# - 跟踪 GPU 内存泄漏的专业方法(分配而不释放)

c++ - 为什么每次RNG的结果都一样?

c++ - COM:使用指向它实现的接口(interface)的指针获取 coclass 对象的 GUID