结构体嵌套调用有错误!

stevenjin 2009-04-20 09:23:12
这种结构体嵌套调用有错吗?是林锐书里看到的.应该怎样才正确?
#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;
#include <iostream>
using namespace std;

struct Student
{
int age;
struct Name
{
char firstName[30];
char secondName[30];
};

};
void main()
{
Student stu;
char p[20]="name";
stu.Name.firstName=p;
}


报错:
错误 2 error C2274: “函数样式转换”: 位于“.”运算符右边时非法 c:\documents and settings\administrator\my documents\visual studio 2005\projects\c++ project\c++ project\c++ project.cpp 22
错误 3 error C2228: “.firstName”的左边必须有类/结构/联合 c:\documents and settings\administrator\my documents\visual studio 2005\projects\c++ project\c++ project\c++ project.cpp 22
...全文
238 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ericming200409 2009-04-20
  • 打赏
  • 举报
回复
结构体的嵌套,外面结构体里面定义的必须是结构体变量,而你只是定义了结构体类型
mengde007 2009-04-20
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <iostream>
#include <assert.h>
using namespace std;
#include <iostream>
using namespace std;

struct Student
{
int age;
struct Name
{
char firstName[30];
char secondName[30];
}name;//

};
void main()
{
Student stu;
char p[30]="name";
stu.name.firstName=p;//
}



yangqidong 2009-04-20
  • 打赏
  • 举报
回复
不是发过的吗
struct
{
char firstName[30];
char secondName[30];
} Name ;
pengzhixi 2009-04-20
  • 打赏
  • 举报
回复

struct Student
{
int age;
struct Name
{
char firstName[30];
char secondName[30];
}name;

};
void main()
{
Student stu;
char p[20]="name";
stu.name.firstName=p;
}

65,211

社区成员

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

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