gpt4 book ai didi

c++ - 是否可以检查共享指针是否在 native 单元测试中返回 nullptr?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:57 26 4
gpt4 key购买 nike

使用Visual Studio 2012原生单元测试,使用共享指针时无法编译代码。

我的代码:

#include "stdafx.h"
#include "CppUnitTest.h"
#include "ParameterTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace MyParameter;

namespace MyParameterTests
{
TEST_CLASS(MyParameterTests)
{
public:

TEST_METHOD(CheckParametersWithPathReturnsNotNull)
{
//Arrange
Parameter parameter = Parameter();

//Act
std::shared_ptr<std::string> actual = parameter.CheckParameters("C:\\Parameter.db");

//Assert
Assert::IsNotNull(actual, L"Should not return null", LINE_INFO());

}

};
}

编译错误:

2>------ Rebuild All started: Project: MyParameterTests, Configuration: Debug Win32 ------ 2> stdafx.cpp 2> ParameterTestTests.cpp 2>c:\users\\documents\visual studio 2012\projects\myparametertests\parametertests.cpp(26): error C2784: 'void Microsoft::VisualStudio::CppUnitTestFramework::Assert::IsNotNull(const T *,const wchar_t *,const Microsoft::VisualStudio::CppUnitTestFramework::__LineInfo *)' : could not deduce template argument for 'const T *' from 'std::shared_ptr<_Ty>' 2> with 2> [ 2> _Ty=std::string 2> ] 2> c:\program files (x86)\microsoft visual studio 11.0\vc\unittest\include\cppunittestassert.h(259) : see declaration of 'Microsoft::VisualStudio::CppUnitTestFramework::Assert::IsNotNull'

最佳答案

你应该替换

Assert::IsNotNull(actual, L"Should not return null", LINE_INFO()); 

Assert::IsNotNull(actual.get(), L"Should not return null", LINE_INFO());

的确,断言需要一个指针来检查它是否为空,但严格来说智能指针不是指针(它是管理指针的对象)。

关于c++ - 是否可以检查共享指针是否在 native 单元测试中返回 nullptr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16939709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com