C++:从指向类的指针访问成员结构的语法

发布于 2024-07-21 19:22:49 字数 405 浏览 7 评论 0原文

我正在尝试访问成员结构变量,但我似乎无法获得正确的语法。 两个编译错误pr。 访问权限是: 错误 C2274:“函数式转换”:“.”的右侧非法 操作员 错误 C2228:“.otherdata”的左侧必须具有类/结构/联合 我尝试过各种改变,但没有成功。

#include <iostream>

using std::cout;

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    int somedata;
};

int main(){
    Foo foo;
    foo.Bar.otherdata = 5;

    cout << foo.Bar.otherdata;

    return 0;
}

I'm trying to access a member structs variables, but I can't seem to get the syntax right.
The two compile errors pr. access are:
error C2274: 'function-style cast' : illegal as right side of '.' operator
error C2228: left of '.otherdata' must have class/struct/union
I have tried various changes, but none successful.

#include <iostream>

using std::cout;

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    int somedata;
};

int main(){
    Foo foo;
    foo.Bar.otherdata = 5;

    cout << foo.Bar.otherdata;

    return 0;
}

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

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

发布评论

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

评论(5

吃→可爱长大的 2024-07-28 19:22:49

BarFoo内部定义的内部结构体。 创建 Foo 对象不会隐式创建 Bar 的成员。 您需要使用 Foo::Bar 语法显式创建 Bar 对象。

Foo foo;
Foo::Bar fooBar;
fooBar.otherdata = 5;
cout << fooBar.otherdata;

否则,

创建 Bar 实例作为 Foo 类中的成员。

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    int somedata;
    Bar myBar;  //Now, Foo has Bar's instance as member

};

 Foo foo;
 foo.myBar.otherdata = 5;

Bar is inner structure defined inside Foo. Creation of Foo object does not implicitly create the Bar's members. You need to explicitly create the object of Bar using Foo::Bar syntax.

Foo foo;
Foo::Bar fooBar;
fooBar.otherdata = 5;
cout << fooBar.otherdata;

Otherwise,

Create the Bar instance as member in Foo class.

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    int somedata;
    Bar myBar;  //Now, Foo has Bar's instance as member

};

 Foo foo;
 foo.myBar.otherdata = 5;
萌吟 2024-07-28 19:22:49

您只在那里定义一个结构,而不是分配一个。 试试这个:

class Foo{
public:
    struct Bar{
        int otherdata;
    } mybar;
    int somedata;
};

int main(){
    Foo foo;
    foo.mybar.otherdata = 5;

    cout << foo.mybar.otherdata;

    return 0;
}

如果你想在其他类中重用该结构体,你也可以在外部定义该结构体:

struct Bar {
  int otherdata;
};

class Foo {
public:
    Bar mybar;
    int somedata;
}

You only define a struct there, not allocate one. Try this:

class Foo{
public:
    struct Bar{
        int otherdata;
    } mybar;
    int somedata;
};

int main(){
    Foo foo;
    foo.mybar.otherdata = 5;

    cout << foo.mybar.otherdata;

    return 0;
}

If you want to reuse the struct in other classes, you can also define the struct outside:

struct Bar {
  int otherdata;
};

class Foo {
public:
    Bar mybar;
    int somedata;
}
只是一片海 2024-07-28 19:22:49

您创建了一个嵌套结构,但从未在类中创建它的任何实例。 你需要这样说:

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    Bar bar;
    int somedata;
};

然后你可以说:

foo.bar.otherdata = 5;

You create a nested structure, but you never create any instances of it within the class. You need to say something like:

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    Bar bar;
    int somedata;
};

You can then say:

foo.bar.otherdata = 5;
时光与爱终年不遇 2024-07-28 19:22:49

您只声明 Foo::Bar 但没有实例化它(不确定这是否是正确的术语)

请参阅此处了解用法:

#include <iostream>

using namespace std;

class Foo
{
    public:
    struct Bar
    {
        int otherdata;
    };
    Bar bar;
    int somedata;
};

int main(){
    Foo::Bar bar;
    bar.otherdata = 6;
    cout << bar.otherdata << endl;

    Foo foo;
    //foo.Bar.otherdata = 5;
    foo.bar.otherdata = 5;

    //cout << foo.Bar.otherdata;
    cout << foo.bar.otherdata << endl;

    return 0;
}

You are only declaring Foo::Bar but you don't instantiate it (not sure if that's the correct terminology)

See here for usage:

#include <iostream>

using namespace std;

class Foo
{
    public:
    struct Bar
    {
        int otherdata;
    };
    Bar bar;
    int somedata;
};

int main(){
    Foo::Bar bar;
    bar.otherdata = 6;
    cout << bar.otherdata << endl;

    Foo foo;
    //foo.Bar.otherdata = 5;
    foo.bar.otherdata = 5;

    //cout << foo.Bar.otherdata;
    cout << foo.bar.otherdata << endl;

    return 0;
}
辞旧 2024-07-28 19:22:49
struct Bar{
        int otherdata;
    };

这里您刚刚定义了一个结构体,但没有创建它的任何对象。 因此,当您说 foo.Bar.otherdata = 5; 时,这是编译器错误。 创建一个 struct Bar 的对象,如 Bar m_bar; ,然后使用 Foo.m_bar.otherdata = 5;

struct Bar{
        int otherdata;
    };

Here you have just defined a structure but not created any object of it. Hence when you say foo.Bar.otherdata = 5; it is compiler error. Create a object of struct Bar like Bar m_bar; and then use Foo.m_bar.otherdata = 5;

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