我已经看过关于这个的类似帖子,但是"Unable to resolve identifier nullptr"当它工作正常时,无法让Netbeans停止在我的代码中显示错误消息.我正确启用了C++ 11,不确定问题是什么?
我有以下测试代码:
#include <cstdint>
#include <cassert>
enum class Result : std::uint32_t {SUCCESS = 0, INSUCCESS = 1};
void* func(Result& result)
{
// works great
/*
result = Result::INSUCCESS;
return NULL;
*/
// error: invalid conversion from ‘long int’ to ‘void*’ [-fpermissive]
/*
return result = Result::INSUCCESS, NULL;
*/
// compiles, but <result> is not set???
return result = Result::INSUCCESS, nullptr;
}
void testReturnWithSideEffects()
{
Result result = Result::SUCCESS;
func(result);
assert(result == Result::INSUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
这里有两个问题但我对第二个问题最感兴趣:
为什么没有设置结果?
编辑:感谢大家的确认.我决定使用的解决方法是替换:
return result = Result::INSUCCESS, nullptr; …Run Code Online (Sandbox Code Playgroud) 我尝试了nullptr的官方实现
我知道编译器支持nullptr和std :: nullptr_t,这个实现没有意义.我只是想学习C++.
一切工作深受GCC4.9.1我的电脑上,无论是-std = C++ 03和-std = C++ 11,即使是成功的在线@ ideone.com GCC4.3.2.
但是在VS2013中,第125行出现错误2440,转换失败
error C2440: "initialize": cannot convert "const <unnamed-type-my_nullptr>" to "int (__thiscall Test::* )(void) const" in line125
Run Code Online (Sandbox Code Playgroud)
>
#include <iostream>
#include <cassert>
// The Official proposal
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf
const // this is a const object...
class {
public:
template<class T> // convertible to any type
operator T*() const // of null non-member
{
return 0;
} // pointer...
template<class C, class T> // or any type of null …Run Code Online (Sandbox Code Playgroud) 我有一小段代码分配nullptr给bool类型.
#include <iostream>
int main()
{
bool b = nullptr;
std::cout << b;
}
Run Code Online (Sandbox Code Playgroud)
在clang 3.8.0工作正常.它给出了一个输出0.Clang Demo
但是g ++ 5.4.0给出了一个错误:
source_file.cpp: In function ‘int main()’:
source_file.cpp:5:18: error: converting to ‘bool’ from ‘std::nullptr_t’ requires direct-initialization [-fpermissive]
bool b = nullptr;
Run Code Online (Sandbox Code Playgroud)
哪个编译器正确?
我std::is_pointer在C++中读到过.
然后我写了程序并检查是否T是指针类型使用std::is_pointer.
#include <iostream>
int main()
{
std::cout<<std::boolalpha;
std::cout<<"char : " <<std::is_pointer<char>::value<<std::endl; // OK
std::cout<<"char * : "<<std::is_pointer<char *>::value<<std::endl; // OK
std::cout<<"char ** : "<<std::is_pointer<char **>::value<<std::endl; // OK
std::cout<<"char *** : "<<std::is_pointer<char ***>::value<<std::endl; // OK
std::cout<<"std::nullptr_t : "<<std::is_pointer<std::nullptr_t>::value<<std::endl; // Not ok, Why false??
}
Run Code Online (Sandbox Code Playgroud)
输出:[Wandbox演示]
char : false
char * : true
char ** : true
char *** : true
std::nullptr_t : false
Run Code Online (Sandbox Code Playgroud)
为什么std::is_pointer<std::nullptr_t>::value等于false?
我正在为SDL_Texture*原始指针编写包装,该包装返回a unique_ptr。
using TexturePtr = std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)>;
TexturePtr loadTexture(SDL_Renderer* renderer, const std::string &path) {
ImagePtr surface =
loadImage(path);
if (surface) {
return TexturePtr(
SDL_CreateTextureFromSurface(renderer, surface.get())
, SDL_DestroyTexture);
}
return nullptr;
}
Run Code Online (Sandbox Code Playgroud)
但是它给出了以下错误:
no suitable constructor exists to convert from "std::nullptr_t" to "std::unique_ptr<SDL_Texture, void (__cdecl *)(SDL_Texture *texture)>"
Run Code Online (Sandbox Code Playgroud)
根据我的理解,传递nullptr代替unique_ptr是可以接受的。我事件尝试在最后一次返回时传递一个空的unique_ptr:
return TexturePtr();
Run Code Online (Sandbox Code Playgroud)
但是在构建过程中得到类似的错误。
请让我知道我在做什么错。
Env:编译器:Visual C ++ 14.1
我想保持我的代码可以在遗留C++(使用"NULL"的C++代码)和新的C++ 11标准(使用"nullptr"的C++代码)上编译
我正在使用GCC,但是当我完成最重要的事情时,我还计划为VS重新编译整个代码库.
我是否应该期待GCC和VS都会这样做
#define NULL nullptr
Run Code Online (Sandbox Code Playgroud)
或者我自己会做得更好(当然使用不同的名称,其中MY_LIB将被我的库后缀替换)?
#ifndef nullptr
#define MY_LIB_NULL NULL
#else
#define MY_LIB_NULL nullptr
#endif
Run Code Online (Sandbox Code Playgroud)
我想要实现的是编译的代码,无论C++ 11的功能是否已经实现(并且因为我没有使用模板,所以它们很少).
例如,关键字"覆盖"和"最终"已经完成.
MY_LIB_OVERRIDE //macro, defines to "override" if c++11 is present.
MY_LIB_FINAL //macro, defines to "final" if c++11 is present.
Run Code Online (Sandbox Code Playgroud)
我问这个问题是因为我知道"nullptr"问题有点奇怪,所以也许只是我已经为覆盖和最终做了同样的事情,这是错误的.需要对此提出意见.任何帮助都很好.
你好?我正在研究双向链表
无论如何,当我过去将数据放在节点上时,出现了 C6011 错误 [:Dereferencing Null Pointer 'NewNode']
所以我参考了https://learn.microsoft.com/ko-kr/visualstudio/code-quality/c6011?view=vs-2019和
尝试检查空值。但它没有用..
我检查了错误窗口的一些详细信息,例如我添加的图片
他们说“NewNode 将为 NULL”,“即使取消引用,NewNode 仍将为 NULL”
代码如下
1.头文件(DoubleLinkedList.h)
#pragma once
#ifndef DOUBLY_LINKEDLIST_H
#define DOUBLY_LINKEDLIST_H
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElementType;
typedef struct MyNode
{
ElementType Data;
struct MyNode* Prev;
struct MyNode* Next;
}Node;
Node* DLL_CreateNode(ElementType NewData); //1.create node
void DLL_DestroyNode(Node* Node);//2.destroy node
void DLL_AppendNode(Node** Head, Node* NewNode);//3.append node
/*
4.insert node
connect
prev-insert-next
*/
void DLL_InsertAfter(Node* Current, Node* NewNode);
void DLL_RemoveNode(Node** Head, Node* Remove);//5.remove node …Run Code Online (Sandbox Code Playgroud) 更新到 Ventura 13.3,安装最新的 Xcode 和命令行工具后,我在编译任何 cpp 文件时收到此错误
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cstddef:50:9: error: no member named 'nullptr_t' in the global namespace
Run Code Online (Sandbox Code Playgroud)
它发生在#include <iostream>
例如:
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
In file included from Untitled.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:41:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:221:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:18:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:841:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/copy.h:12:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/unwrap_iter.h:13:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__iterator/iterator_traits.h:14:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__iterator/incrementable_traits.h:15:
In …Run Code Online (Sandbox Code Playgroud)