指针如何引用结构体中结构体的成员

路人呵呵 2011-04-05 11:07:22

#include<iostream>
using namespace std;
struct birthday
{
int year;
int month;
int day;
};
struct person
{
int age ;
struct birthday;

};

int main ()
{
struct person *p ;
struct person A;
p=&A ;
cin>>p->birthday.year;
cin>>p->birthday.month;
cin>>p->birthday.day;
cin>>p->
cout<<"----------出生信息-------------"<<endl;
cout<<p->birthday.year;
cout<<p->birthday.month;
cout<<p->birthday.day;
cout<<p->age;
return 0;
}



出错信息:
标记“.”在 UDT“person::birthday”之后是非法的
“.month”的左边必须有类/结构/联合

指针如何 引用 结构体中的结构体的成员?
...全文
610 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
路人呵呵 2011-04-05
  • 打赏
  • 举报
回复

忘得差不多了
maoxing63570 2011-04-05
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include<iostream>
using namespace std;
struct birthday
{
int year;
int month;
int day;
};
struct person
{
int age ;
struct birthday birth;

};

int main ()
{
struct person *p ;
struct person A;
p=&A ;
cin>>p->birth.year;
cin>>p->birth.month;
cin>>p->birth.day;
cout<<"----------出生信息-------------"<<endl;
cout<<p->birth.year;
cout<<p->birth.month;
cout<<p->birth.day;
cout<<p->age;
return 0;
}

总说没代码没真相,现在有代码了,有真相么?给力么?
maoxing63570 2011-04-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lurenheheyy 的回复:]
楼上的还是通不过

struct person
{
int age ;
struct birthday.day;
struct birthday.year;
struct birthday.month;
};
也不行:
/*
error C2059: 语法错误:“.”
error C2238: 意外的标记位于“;”之前
error C2059: 语法错误:“.”
er……
[/Quote]
哎,我强烈建议你去找本书看下
路人呵呵 2011-04-05
  • 打赏
  • 举报
回复
楼上的还是通不过

struct person
{
int age ;
struct birthday.day;
struct birthday.year;
struct birthday.month;
};
也不行:
/*
error C2059: 语法错误:“.”
error C2238: 意外的标记位于“;”之前
error C2059: 语法错误:“.”
error C2238: 意外的标记位于“;”之前
error C2059: 语法错误:“.”
error C2238: 意外的标记位于“;”之前
error C2039: “birthday”: 不是“person”的成员
cpp(154) : 参见“person”的声明
error C2228: “.year”的左边必须有类/结构/联合
error C2039: “birthday”: 不是“person”的成员
cpp(154) : 参见“person”的声明
error C2228: “.month”的左边必须有类/结构/联合
error C2039: “birthday”: 不是“person”的成员
cpp(154) : 参见“person”的声明
error C2228: “.day”的左边必须有类/结构/联合
error C2039: “birthday”: 不是“person”的成员
cpp(154) : 参见“person”的声明
error C2228: “.year”的左边必须有类/结构/联合
error C2039: “birthday”: 不是“person”的成员
cpp(154) : 参见“person”的声明
error C2228: “.month”的左边必须有类/结构/联合
error C2039: “birthday”: 不是“person”的成员
cpp(154) : 参见“person”的声明
error C2228: “.day”的左边必须有类/结构/联合
*/

struct person
{
int age ;
struct birthday day;
struct birthday year;
struct birthday month;
};
还是通不过:
error C2039: “birthday”: 不是“person”的成员
cpp(144) : 参见“person”的声明
error C2228: “.year”的左边必须有类/结构/联合
error C2039: “birthday”: 不是“person”的成员
cpp(144) : 参见“person”的声明
error C2228: “.month”的左边必须有类/结构/联合
error C2039: “birthday”: 不是“person”的成员
cpp(144) : 参见“person”的声明
error C2228: “.day”的左边必须有类/结构/联合
error C2039: “birthday”: 不是“person”的成员
cpp(144) : 参见“person”的声明
error C2228: “.year”的左边必须有类/结构/联合
error C2039: “birthday”: 不是“person”的成员
cpp(144) : 参见“person”的声明
error C2228: “.month”的左边必须有类/结构/联合
error C2039: “birthday”: 不是“person”的成员
cpp(144) : 参见“person”的声明
error C2228: “.day”的左边必须有类/结构/联合
---------------------------------------------------------------
maoxing63570 2011-04-05
  • 打赏
  • 举报
回复
struct birthday;
birthday是你的结构体类型名,不是变量名
struct birthday 变量名(像这样来做)
路人呵呵 2011-04-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 visir 的回复:]

struct person
{
int age ;
struct birthday membername;

};

你的结构体定义出错了
[/Quote]

那么应该如何定义?
visir 2011-04-05
  • 打赏
  • 举报
回复
struct person
{
int age ;
struct birthday membername;

};

你的结构体定义出错了
CppCoder 2011-04-05
  • 打赏
  • 举报
回复
写代码时忘记了语法,在文件中搜索类似使用,COPY,MODIFY,COMPILE,OK

64,631

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧