结构体嵌套调用有错误!

stevenjin 2009-04-20 08:52:30
#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()
{
struct Student stu;
char *p="ok";
strcpy(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
...全文
143 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
shexinwei 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 //此处LZ定义了一个结构体,名字叫做Name,这是一个结构体,不是一个变量,所以必须重新定义变量
{
char firstName[30];
char secondName[30];
};

};
void main()
{
struct Student stu;
char *p="ok";
strcpy(stu.Name.firstName,p); //同上,stu.Name.firstName中的Name为类型,不是变量,因此不是stu的成员,不能使用"."操作符

}

细心点呀!!
  • 打赏
  • 举报
回复

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

};
void main()
{
struct Student stu;
char *p="ok";
strcpy(stu.name.firstName,p);

}


对象跟声明不分.
doublemoon 2009-04-20
  • 打赏
  • 举报
回复
struct Student
{
int age;
struct Name
{
char firstName[30];
char secondName[30];
};

};
在c语言是不能这样写的,在c里如果改成这样就对了:
struct Name
{
char firstName[30];
char secondName[30];
};

struct Student
{
int age;
struct Name *name;
};
在c++里我就不大清楚了,不过二种语言大部分都差不多,你改成这样试
yangqidong 2009-04-20
  • 打赏
  • 举报
回复
结构名可以省略,但是结构变量不能少
yangqidong 2009-04-20
  • 打赏
  • 举报
回复
struct Student
{
int age;
struct
{
char firstName[30];
char secondName[30];
} Name ;

};
mengde007 2009-04-20
  • 打赏
  • 举报
回复

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

};
void main()
{
struct Student stu;
char *p="ok";
strcpy(stu.name.firstName,p); //

}

mengde007 2009-04-20
  • 打赏
  • 举报
回复

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

};
pengzhixi 2009-04-20
  • 打赏
  • 举报
回复
struct Student
{
int age;

};
struct Name
{
char firstName[30];
char secondName[30];
};和你定义的没区别,因为你student里面根本就不含有name类型的成员除非这样

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

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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