绑定到函数的指针只能用于调用函数

发布于 2024-11-27 12:46:05 字数 332 浏览 4 评论 0原文

我刚刚从 char 数组转移到 std::string 并且已经遇到了问题,我可能正在做一些非常愚蠢的事情,请随意嘲笑:

int main()
{
    string * p = new string;
    memset(p, 0, sizeof(string));

    expected_exepath(p);

    cout << p->data;

    delete p;
}

错误在 p->data 中,其中显示“绑定到函数的指针只能用于调用函数”.. p 是 std::string,所以我不明白为什么它认为我正在尝试调用函数。

I've just moved from char arrays to std::string and I've already ran into a problem, I'm probably doing something extremely stupid, feel free to ridicule:

int main()
{
    string * p = new string;
    memset(p, 0, sizeof(string));

    expected_exepath(p);

    cout << p->data;

    delete p;
}

The error is in p->data, which says "a pointer bound to a function may only be used to call a function".. p is std::string, so I don't see why it thinks I'm trying to call a function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱的故事 2024-12-04 12:46:05

因为数据是一个函数,而不是数据成员。更重要的是,std::string 的一半意义在于它是一个值。你不应该使用new,除非你有一个非常充分的理由——在堆栈上分配,或者如果你必须动态分配使用容器或智能指针。

另外:永远、永远、永远不要像这样 memset UDT。他们始终照顾自己的内部状态,并且不会扰乱它。

Because data is a function, not a data member. More importantly, half the point of std::string is that it's a value. You shouldn't use new unless you have an extremely good reason- allocate on the stack, or if you must dynamically allocate use a container or smart pointer.

Also: Do not ever, ever, ever memset UDTs like that. They take care of their own internal state, all the time, and do not mess with it.

沙与沫 2024-12-04 12:46:05

错误出现在 p->data 中,其中表示“绑定到函数的指针可能会
仅用于调用函数”.. p 是 std::string,所以我看不到
为什么它认为我正在尝试调用函数。

几点:

  • string::data() 是一个函数,因此该错误消息是完全合适的。

  • p 是指向 std::string指针,而不是 std::string。< /p>

  • p->data() 传递给 cout 会很危险,因为 data() 返回一个没有 null 终止符的 char 数组,与 string::c_str() 不同。我建议你只使用

    <前><代码>cout << *p;

    ...相反。

如果 expected_exepath 接受 std::string* 参数,那么我建议像这样重写你的函数:

int main()
{
    string p;    
    expected_exepath(&p); 
    cout << p;
}

The error is in p->data, which says "a pointer bound to a function may
only be used to call a function".. p is std::string, so I don't see
why it thinks I'm trying to call a function.

A few points:

  • string::data() is a function, hence that error message is entirely appropriate.

  • p is a pointer to a std::string, not an std::string.

  • Passing p->data() to cout would be dangerous given that data() returns a char array without a null terminator, unlike string::c_str(). I'd suggest you just use

    cout << *p;
    

    ...instead.

If expected_exepath takes a std::string* argument, then I'd suggest re-writing your function like this:

int main()
{
    string p;    
    expected_exepath(&p); 
    cout << p;
}
缺⑴份安定 2024-12-04 12:46:05
cout << p->data;

string::data() 是一个函数,而不是数据成员。您需要调用它,而不仅仅是取消引用它。像这样:

cout << p->data();
cout << p->data;

string::data() is a function, not a data member. You need to call it, not just dereference it. Like this:

cout << p->data();
掌心的温暖 2024-12-04 12:46:05

我想提供另一个可能的错误,该错误会导致相同的错误消息,尽管与您的具体问题无关。

当您使用 vector.emplace_back{.. , ..} 而不是 vector.emplace_back(.. , ..) 时。

希望像我这样犯这个错误的人不会困惑几个小时。

I'd like to provide another possible mistake that cause the same error message although not related to your specific question.

It's when you use vector.emplace_back{.. , ..} instead of vector.emplace_back(.. , ..).

Hope maybe someone making this mistake like me does not get confused for hours.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文