C语言nullptr错误,编译错误'nullptr'未声明的标识符(Compile error 'nullptr' undeclared identifier)...

编译错误'nullptr'未声明的标识符(Compile error 'nullptr' undeclared identifier)

我打算用Visual Studio 2008 Express编译源代码,但是我收到这个错误:

Error C2065: 'nullptr' undeclared identifier.

我的代码:

if (Data == nullptr)

{

show("Data is null");

return 0;

}

我在谷歌上读到我应该升级到Visual Studio 2010,但是我不想这样做是因为2008的智能。 这可以修理或更换吗?

I'm tring to compile a source with Visual Studio 2008 Express but I'm getting this error:

Error C2065: 'nullptr' undeclared identifier.

My code:

if (Data == nullptr)

{

show("Data is null");

return 0;

}

I read on google that I should upgrade to Visual Studio 2010, but I don't want to do this because of the 2008 intelisense. Can this be repaired or replaced?

原文:https://stackoverflow.com/questions/24433436

更新时间:2020-01-19 04:41

最满意答案

你得到的错误是因为编译器不能识别nullptr关键字。 这是因为nullptr是在Visual Studio的后续版本中引入的,而不是您正在使用的版本。

有两种方法可以让你在老版本中工作。 一个想法来自Scott Meyers c ++书,他建议用类似这样的模拟nullptr的类创建一个头文件:

const // It is a const object...

class nullptr_t

{

public:

template

inline operator T*() const // convertible to any type of null non-member pointer...

{ return 0; }

template

inline operator T C::*() const // or any type of null member pointer...

{ return 0; }

private:

void operator&() const; // Can't take address of nullptr

} nullptr = {};

这样你只需要有条件地包含基于msvc版本的文件

#if _MSC_VER < 1600 //MSVC version <8

#include "nullptr_emulation.h"

#endif

这具有使用相同关键字的优点,并且使升级到新编译器更容易一些(并且如果可以的话,请进行升级)。 如果你现在用一个更新的编译器进行编译,那么你的自定义代码根本不会被使用,你只能使用c ++语言,我觉得这很重要。

如果你不想采用这种方法,你可以使用模拟旧C风格的方法( #define NULL ((void *)0) ),其中你为这样的NULL创建一个宏:

#define NULL 0

if(data == NULL){

}

请注意,这与C中的NULL不完全相同,有关更多讨论,请参阅此问题: 为什么在C和C ++中定义的NULL指针不同?

这样做的缺点是你必须改变源代码,而不是类似于nullptr类型安全。 所以谨慎使用它,如果你不小心,它会引入一些细微的错误,而这些微妙的错误首先激发了nullptr的发展。

The error you are getting is because the compiler doesn't recognize the nullptr keyword. This is because nullptr was introduced in a later version of visual studio than the one you are using.

There's 2 ways you might go about getting this to work in an older version. One idea comes from Scott Meyers c++ book where he suggests creating a header with a class that emulates nullptr like this:

const // It is a const object...

class nullptr_t

{

public:

template

inline operator T*() const // convertible to any type of null non-member pointer...

{ return 0; }

template

inline operator T C::*() const // or any type of null member pointer...

{ return 0; }

private:

void operator&() const; // Can't take address of nullptr

} nullptr = {};

This way you just need to conditionally include the file based on the version of msvc

#if _MSC_VER < 1600 //MSVC version <8

#include "nullptr_emulation.h"

#endif

This has the advantage of using the same keyword and makes upgrading to a new compiler a fair bit easier (and please do upgrade if you can). If you now compile with a newer compiler then your custom code doesn't get used at all and you are only using the c++ language, I feel as though this is important going forward.

If you don't want to take that approach you could go with something that emulates the old C style approach (#define NULL ((void *)0)) where you make a macro for NULL like this:

#define NULL 0

if(data == NULL){

}

Note that this isn't quite the same as NULL as found in C, for more discussion on that see this question: Why are NULL pointers defined differently in C and C++?

The downsides to this is that you have to change the source code and it is not typesafe like nullptr. So use this with caution, it can introduce some subtle bugs if you aren't careful and it was these subtle bugs that motivated the development of nullptr in the first place.

2017-05-23

相关问答

您忘记了您的destroyPointer()方法的类标识符。 尝试 void BaseClassWithPointer::destroyPointer()

代替 You forgot the Class identifier for your destroyPointer() method. Try void BaseClassWithPointer::destroyPointer()

instead

您必须将框架MobileCoreServices添加到项目中,然后导入它: #import

这将使问题消失。 You have to add the framework MobileCoreServices to the project, and then import it: Objective C: #import

That

...

此WSMethodInvocationRef在iOS中不支持,仅适用于OS X. This WSMethodInvocationRef does not support in iOS, it only for OS X.

在你的storyboard中必须有一个控制器,其中你已经设置了一个讲故事板ID的课程,并且你从该故事板中删除了该课程,但故事板ID仍在解决它,如下图所示: 因此,您必须删除该Storyboard ID并取消选中“使用故事板ID”。 就这样。 After scratching my head for a day. I manage to get ride off the bizarre errors. Finding starts from below: 1.mapMembersData-class

...

你得到的错误是因为编译器不能识别nullptr关键字。 这是因为nullptr是在Visual Studio的后续版本中引入的,而不是您正在使用的版本。 有两种方法可以让你在老版本中工作。 一个想法来自Scott Meyers c ++书,他建议用类似这样的模拟nullptr的类创建一个头文件: const // It is a const object...

class nullptr_t

{

public:

template

inline operato

...

只需将myWebMain更改为webView [webView stringByEvaluatingJavaScriptFromString:js];

just change myWebMain to webView [webView stringByEvaluatingJavaScriptFromString:js];

NSMigratePersistentStoresAutomaticallyOption在NSPersistentStoreCoordinator.h中声明。 您确定在构建该字典的任何地方(包括CoreData.h)都包含或转发它吗? NSMigratePersistentStoresAutomaticallyOption is declared in NSPersistentStoreCoordinator.h. Are you sure that that is included or for

...

UITextViewTextDidChangeNotification尚未在Apportable平台中实现。 请注意,$ HOME / .apportable / SDK / System / UIKit / UITextView.h中缺少它 UITextViewTextDidChangeNotification is not yet implemented in the Apportable platform. Note that it is missing from $HOME/.apport

...

你有一个循环定义问题。 请注意,foo.h包含bar.h , bar.h包含foo.h. 这不起作用,因为这意味着在第一个完成其他定义之前,两个定义都不能完成。 您可以真正解决此问题的唯一方法是更改您的结构,以便其中一个标头不#include其他标头。 The problem is caused by circular dependency between foo.h and bar.h. It can be resolved using any of the following methods

...

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值