C++ 访问类中结构的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16930296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
accessing member of structure within a class
提问by Quick Silver
I have an .hpp and .cpp file. I want to access the variable in the structure within a class which happens to be in the header .hpp file, in the .cpp file.
我有一个 .hpp 和 .cpp 文件。我想访问一个类中结构中的变量,该变量恰好位于 .cpp 文件中的头 .hpp 文件中。
In .hpp, I have
在 .hpp 中,我有
class foo{
public:
struct packet{
int x;
u_int y;
};
};
foo(const char*name)
:m_name(name){}
In .cpp I did:
在 .cpp 我做了:
foo *foo_1 = &foo;
printf("The value of x is : %d",foo_1->packet.x);
printf ("The value of y is : %u", foo_1->packet.y);
On doing this I receive the following error:
这样做时,我收到以下错误:
code_1.cpp:117: error: expected primary-expression before ‘;' token
code_1.cpp:118: error: invalid use of ‘struct foo::packet'
code_1.cpp:119: error: invalid use of ‘struct foo::packet'
make: *** [code_1] Error 1
My objective is to get the values of x and y in the cpp file. Any suggestion/idea will be really appreciated.
我的目标是在 cpp 文件中获取 x 和 y 的值。任何建议/想法将不胜感激。
Thanks.
谢谢。
回答by Mark Garcia
You need a member objectof type foo::packet
in class foo
.
您需要一个in类型的成员对象。foo::packet
class foo
class foo{
public:
struct packet{
int x;
u_int y;
};
packet my_packet; // <- THIS
};
In your .cpp, you should do:
在您的 .cpp 中,您应该执行以下操作:
foo *foo_1 = &foo;
printf("The value of x is : %d",foo_1->my_packet.x);
printf ("The value of y is : %u", foo_1->my_packet.y);
You must remember that even though packet
is inside foo
, it is not included in foo
as a member object. It is just a class enclosed inside another class. And for a class to be used, you must have objects of it (a class can also be used without having objects of it, but, well...).
您必须记住,即使packet
在 inside 中foo
,它也不foo
作为成员对象包含在中。它只是一个封闭在另一个类中的类。对于要使用的类,您必须拥有它的对象(类也可以在没有对象的情况下使用,但是,嗯……)。
回答by Reinstate Monica
In your class Foo
, you have defined a packet
struct, but you have not declared any instances of it. What you want is (this is a compileable self-contained example):
在您的 class 中Foo
,您已经定义了一个packet
结构,但是您还没有声明它的任何实例。你想要的是(这是一个可编译的自包含示例):
#include <iostream>
class Foo {
public:
struct Packet{
Packet() : x(0), y(0) {}
int x;
int y;
} packet;
};
int main(int, char**)
{
Foo foo_1;
std::cout << "The value of x is: " << foo_1.packet.x << std::endl;
std::cout << "The value of y is: " << foo_1.packet.y << std::endl;
}
回答by 0x499602D2
packet
is not a data member of the class, but the class that it defines it ishowever. You need to instantiate an object of that type in order to use it in that way:
packet
不是类的数据成员,但它定义的类是。您需要实例化该类型的对象才能以这种方式使用它:
class foo
{
public:
foo() {} // A default constructor is also needed
struct
{
int x;
u_int y;
} packet;
}; // -- don't forget that semicolon
int main()
{
foo *foo_1 = new foo(); // instantiate a new object on the heap
printf("The value of x is : %d",foo_1->packet.x);
printf("The value of y is : %u", foo_1->packet.y);
delete foo_1; // release the memory
}
回答by Pixelchemist
Class foo does not have a member packet but it contains another class/struct type named "packet". You'll have to provide a member of that type.
类 foo 没有成员包,但它包含另一个名为“包”的类/结构类型。您必须提供该类型的成员。
Can't do a test atm but you could either try
不能做测试 atm 但你可以试试
class foo{
public:
struct {
int x;
u_int y;
} packet;
}
or
或者
class foo{
public:
pack packet;
struct pack {
int x;
u_int y;
};
}
To access x and y via foo_ptr->packet.(...)
or foo_object.packet.(...)
.
通过foo_ptr->packet.(...)
或访问 x 和 y foo_object.packet.(...)
。
回答by Balog Pal
and besides tha packet-related business, foo *foo_1 = &foo;
is bad, you can't take address of a class only of a variable.
除了数据包相关的业务,foo *foo_1 = &foo;
不好,你不能只取一个变量的类的地址。
回答by Eric Duhun Kim
In .hpp you need to declare a variable with the struct type. For example,
在 .hpp 中,您需要声明一个具有 struct 类型的变量。例如,
packet Packet;
inside of your class
在你的班级里面
In .cpp, try this
在 .cpp 中,试试这个
foo *foo_ptr = new foo; // creating new foo object in the heap memory with a pointer foo_ptr
printf("The value of x is : %d",foo_ptr->Packet.x);
printf ("The value of y is : %u", foo_ptr->Packet.y);
回答by Marichyasana
You declare the struct, but you never put any data in it.
您声明了结构,但从未在其中放入任何数据。
回答by Hemanth Kumar
Try
尝试
struct Vehicle
{
int wheels;
char vname[20];
char color[10];
}v1 = {4,"Nano","Red"};
int main()
{
printf("Vehicle No of Wheels : %d",v1.wheels);
printf("Vehicle Name : %s",v1.vname);
printf("Vehicle Color : %s",v1.color);
return(0);
}
hope this may help you we gave the structure name as V1 and acessing the elements by the use of dot operator
希望这可以帮助您我们将结构名称指定为 V1 并使用点运算符访问元素
回答by priteshbaviskar
You have just defined struct. Try something like this -
您刚刚定义了结构。尝试这样的事情 -
struct packet{
int x;
u_int y;
}test;
struct packet{
int x;
u_int y;
}test;
and in your cpp, access your struct elements like this -foo_1.test.x
在你的 cpp 中,像这样访问你的结构元素 -foo_1.test.x
回答by edtheprogrammerguy
The struct
declaration in the class does not create an instance of it - just defines it as a contained struct within foo
.
If you create a struct instance in the class, you can reference it from the containing pointer:
struct
类中的声明不会创建它的实例——只是将它定义为foo
.
如果在类中创建一个 struct 实例,则可以从包含指针中引用它:
struct packet{
int x;
u_int y;
} m_packet;
Now you can say foo_1->m_packet.x = 3;
, etc.
现在你可以说foo_1->m_packet.x = 3;
,等等。
Also, you need to create an instance of foo (in your code you try to take the address of the class name, which won't work):
foo* foo_1 = new foo;
此外,您需要创建一个 foo 实例(在您的代码中,您尝试获取类名的地址,但这是行不通的):
foo* foo_1 = new foo;
Then, delete foo_1
when done with it.
然后,delete foo_1
完成后。