编译错误'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

...

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

  1. MFC 错误异常,用vs添加资源并为资源定义类后报错:error C2065 : 未声明的标识符...

    添加了一个Dialog资源,修改了ID之后右击资源添加了一个类,在类里面有一个成员变量: // 对话框数据     enum { IDD = IDD_GETIN }; 而在编译过程中出现报错,错误代号 ...

  2. c语言中error c2065: c: 未声明的标识符,error C2065:未声明的标识符错误

    添加了一个Dialog资源,修改了ID之后右击资源添加了一个类,在类里面有一个成员变量: // 对话框数据    enum { IDD = IDD_GETIN }; 而在编译过程中出现报错,错误代号是 ...

  3. VS2015报错 error C2065:未声明的标识符错误 (__func__)

    问题:VS2015 编译VTK8.2 报错  error C2065 未声明的标识符错误 (__func__) 解决:(两种) (1)网上更换头文件顺序(第一种方法测试,但是没作用,暂时放在这里,第二 ...

  4. c++: 已经声明过变量,依然报 C2065:未声明的标识符错误 解决办法

    问题:已经声明过变量,依然报 C2065:未声明的标识符错误 解决办法 问题分析:已经声明过变量,依旧报错未声明的标识符错误 (大家是不是很郁闷!!!),仔细观察两种情况的"=", ...

  5. OpenCV的cv::cvtColor函数之“CV_RGB2GRAY”: 未声明的标识符错误解决方法

    OpenCV 4.1,cv::cvtColor函数提示:"CV_RGB2GRAY": 未声明的标识符. 原因:缺少头文件opencv2/imgproc/types_c.h 解决:直 ...

  6. c语言中未声明的标识符要怎么办,vc ++未声明的标识符,虽然它已声明

    我在tetrisdoc.h头文件中获取了'm_colorGrid'的未声明标识符,尽管它声明了: 请参阅下面的代码段: // TetrisDoc.h : interface of the CTetri ...

  7. vs编译运行报错:未声明的标识符

    报错如下图: 很奇怪,我这个testList是已经声明的,就在上一行,但是还是会给我报错:未声明的标识符. 最后发现原因是在报warning的地方. 解决办法: 在代码处右击出现菜单栏,点击最下方的[ ...

  8. error C2065: “IDD_DIALOG1”: 未声明的标识符

    error C2065: "IDD_DIALOG1": 未声明的标识符 分类: VC 中常见BUG 小菜 2011-08-18 18:22 634人阅读 评论(1) 收藏 举报 在 ...

  9. C2065 “__m128d”: 未声明的标识符

    ncnn编译后,报错: C2065    "__m128d": 未声明的标识符 原因: net.h在opencv头文件之前引用,就会报错 解决方法: 把opencv放在前面引用 # ...

最新文章

  1. 向linux kernel中添加cmdline的四种方式
  2. Csv解析CsvFile
  3. Java 虚拟机运行时数据区
  4. javase-字符串次数查找
  5. python源码中的学习笔记_第6章_元组
  6. Java高并发程序设计学习笔记(八):NIO和AIO
  7. go - struct
  8. 利用模板导出文件(一)之XLSTransformer导出excel文件
  9. 飞凌单片机解密_常见的IC芯片解密方法与原理解析!
  10. Excel如何对比两列数据
  11. kettle使用httpClient获取ES索引数据
  12. python变量、运算符
  13. 实现 RadioButtonList 单选按钮组设置及横向布局
  14. python 代码格式化工具:pep8ify
  15. 英语每日听写练习 Day 19
  16. 5分绩点转4分_5分绩点转4分_gpa5分制换算4分制(5分绩点转4分)
  17. 从零开始的Android:Android应用程序开发概述
  18. Source Insight4.0结构体使用宏定义之后,成员没关联?
  19. React Native解决在ios13暗黑模式下状态栏都为白色的问题
  20. Scala映射(Map)

热门文章

  1. 修改netbox 及使用技巧
  2. 如何快速计算出星期几
  3. php 正则特殊字符转义,php 正则表达式字符如何转义
  4. rust矿洞绳子怎么爬下_腐蚀RUST实用技巧分享 教你在游戏中横着走
  5. 如何查看端口号是否被占用
  6. JVM系列之垃圾回收器(下篇)——Shenandoah垃圾回收器
  7. VB XMLHTTP 异步调用
  8. atm(选做)——实现信用卡程序 python
  9. 软件测试基础知识巩固(面试准备)
  10. 曝苹果最早4月推出新款iPad Pro 配备雷电接口,与M1 Mac相同