- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试此 SO Q/A 中提供的解决方案 Compiler error while using shared_ptr with a pointer to a pointer而且我无法以正确的方式使用提供的解决方案。我在使用 g++ 7.3 版的 Ubuntu 18.04 上仍然遇到编译错误
这是我重现问题的最小完整可验证示例
测试.h
# include <memory>
using std::shared_ptr;
using std::unique_ptr;
struct DataNode
{
shared_ptr<DataNode> next;
} ;
struct ProxyNode
{
shared_ptr<DataNode> pointers[5];
} ;
struct _test_
{
shared_ptr<shared_ptr<ProxyNode>> flane_pointers;
};
测试.cpp
#include <stdint.h>
#include "test.h"
shared_ptr<DataNode> newNode(uint64_t key);
shared_ptr<ProxyNode> newProxyNode(shared_ptr<DataNode> node);
struct _test_ test1;
int main(void)
{
test1.flane_pointers(nullptr);
shared_ptr<DataNode> node = newNode(1000);
}
shared_ptr<ProxyNode> newProxyNode(shared_ptr<DataNode> node) {
shared_ptr<ProxyNode> proxy(new ProxyNode());
return proxy;
}
shared_ptr<DataNode> newNode(uint64_t key) {
shared_ptr<DataNode> node(new DataNode());
return node;
}
这是我得到的错误
test.cpp: In function ‘int main()’:
test.cpp:11:31: error: no match for call to ‘(std::shared_ptr<std::shared_ptr<ProxyNode> >) (std::nullptr_t)’
test1.flane_pointers(nullptr);
^
您还尝试过什么?
我也尝试在头文件中初始化nullptr
struct _test_
{
shared_ptr<shared_ptr<ProxyNode>> flane_pointers(nullptr);
};
但这也不起作用。我哪里出错了?
我的目标
我正在尝试做的是以下 - 我正在尝试初始化 flane_pointers,它是一个指向 nullptr 的指针 vector 。关于它是什么类型的声明已在头文件中进行,我正试图在 .cpp 文件中对其进行初始化。这样做时,我遇到了上述编译错误。
flane_pointers(nullptr)
更新
任何答案都可以解释此 Compiler error while using shared_ptr with a pointer to a pointer 中提供的初始化是否对不对?
std::shared_ptr<std::shared_ptr<ProxyNode> > ptr2ptr2ProxyNode(nullptr);
对我(我是 C++ 的新手)来说,初始化看起来也像一个函数调用。这是不正确的吗?
最佳答案
在这一行:
test1.flane_pointers(nullptr);
您正在尝试调用 flane_pointers
,就好像它是一个成员函数一样。 shared_ptr
不能像函数一样被调用,所以你会得到编译器错误。
如果你想初始化flane_pointers
,你可以直接赋值给它:
test1.flane_pointers = nullptr;
或者,您可以在创建 test1
时进行分配:
// Initialize test1 with a nullptr
_test_ test1{nullptr};
关于c++ - 错误使用 nullptr 的编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55682351/
我正在尝试为我的学校作业制作信息亭程序。当我编译它时,它不会发出任何错误信号。但是当我尝试初始化我的队列列表时,它说我不能使用 nullptr...我不明白我的代码有什么问题。 typedef str
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 4 年前。 Improve
这个问题在这里已经有了答案: Placement of the asterisk in pointer declarations (14 个答案) 关闭 4 年前。 在博客和论坛上,我注意到人们使用
为什么std::string str(nullptr)和 std::string str=nullptr是错的?有人可以详细解释原因吗? 最佳答案 这样的例子编译是因为存在一个 std::string
这个定义有效: const auto &b{nullptr}; 虽然失败: auto *b{nullptr}; 我尝试在 Visual C++、GCC 和 Clang 中编译它。他们都提示“无法推断类
刚刚看了一篇关于C++0x标准的文章:http://www.softwarequalityconnection.com/2011/06/the-biggest-changes-in-c11-and-w
我已将节点指针初始化为 nullptr 并将其作为引用传递给辅助函数。在辅助函数内,我将之前为 nullptr 的指针设置为等于 new Pointer。然而,函数结束后,它又被设置为nullptr。
有谁知道为什么分配type* = 0无效,而分配type* = nullptr无效呢?在这两种情况下typedef void type。谢谢 #include #include template
我正在尝试为类(class)项目创建链表。我的节点类有一个指向链接节点的指针和另一个指向专门书籍类的指针。 class Node{ private: Book *data; Node
我在destroyStudent()函数中删除指针aStudent,然后我将aStudent设置为空指针。但是,运行该函数后,aStudent 不再设置为nullptr,所以我必须再次将其设置为nul
我正在尝试制作一个基本的 HashMap。在将元素插入索引之前,我正在检查它是否存在于索引中。当我插入第一个元素时,它说该位置已经存在一个元素。我已经通过调试器,我的所有值都符合预期,map[hash
我正在尝试做类似的事情: if (foo) return SET_ERROR_AND_RETURN_NULL(ERROR_HERE); 使用... #define SET_ERROR_AND
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
我有一个看起来像这样的结构: struct Wolf { Wolf *dog; Wolf *puppy; }; 我写过: Wolf *alphawolf = new Wolf; 当我尝
我正在使用带有 Visual Studio 2012 的 C++(11)。 我使用定制的包装器类创建窗口。 CUIWindow* winA = new CUIWindow ( NULL, TEXT("
我为它编写了一个 PriorityQueue 类和一个迭代器类。但我不明白为什么这些行编译? PriorityQueue pq; auto z =pq.begin(); z=nullptr
我正在学习 enable_if 的用法我偶然发现了以下代码。 template ::value, T>::type* = nullpt
我有一个函数func(int a, char b, void *ptr)。第三个参数保留供内部使用,它应该是当前版本的 nullptr。有没有办法在函数原型(prototype)而不是定义中强制执行此
所以当我在学校编译它时 nullptr 错误没有出现,我想我可以通过在编译时添加一行来修复它,有没有另一种方法来摆脱它,另外两个错误我不明白为什么我要得到它们。有人可以至少解释一下 nullptr 错
这个问题在这里已经有了答案: Checking if this is null (8 个答案) 关闭 4 年前。 我想像这样替换代码: if ((obj != nullptr) && obj->is
我是一名优秀的程序员,十分优秀!