同一头文件结构体互相引用报错

暮光后 2017-11-23 03:59:31
在.H里是这样定义的
struct Edge;
struct Vector3f;
struct Vector3;
typedef struct Vector3f
{
Vector3f(float _x, float _y, float _z) :x(_x), y(_y), z(_z),entryPoint(NULL) {}
float x, y, z;
Edge* entryPoint;
Vector3f& operator=(const Vector3& vec3)
{
x = vec3.x;
y = vec3.y;
z = vec3.z;
entryPoint = NULL;
}
}*pVector3f;
typedef struct Vector3
{
float x, y, z;
Vector3(float _x,float _y,float _z):x(_x),y(_y),z(_z){}
}*pVector3;
struct Edge
{

};
然后就报错了。也按照网上的声明的,不知道是不是忽略了什么,求帮主
...全文
315 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
暮光后 2017-11-27
  • 打赏
  • 举报
回复
struct Edge; struct Vector3f; struct Vector3 ; typedef struct Vector3f { Vector3f(float _x, float _y, float _z) :x(_x), y(_y), z(_z),entryPoint(NULL) {} float x, y, z; Edge* entryPoint; Vector3f& operator=(const Vector3& vec3) { x = vec3.x; y = vec3.y; z = vec3.z; entryPoint = NULL; } }*pVector3f; typedef struct Vector3 { float x, y, z; Vector3(float _x,float _y,float _z):x(_x),y(_y),z(_z){} Vector3& operator=(const Vector3f& vec3) { x = vec3.x; y = vec3.y; z = vec3.z; } }*pVector3; struct Edge { }; 准确的说,这才是原型= =只写着相互引用,忘记加这句重载操作符了
暮光后 2017-11-27
  • 打赏
  • 举报
回复
引用 5 楼 cfjtaishan 的回复:
typedef struct Vector3
{
    float x, y, z;
    Vector3(float _x,float _y,float _z):x(_x),y(_y),z(_z){}
}*pVector3;
把struct Vector3的声明提前,遵循先声明后使用的原则; 结构体struct Vector3f中,这些操作是对结构体struct Vector3的结构体成员的使用,所以声明必须在前;
Vector3f& operator=(const Vector3& vec3)
    {
        x = vec3.x;
        y = vec3.y;
        z = vec3.z;
        entryPoint = NULL;
    }
可能是我没写完全,Vector3也重载了=操作符,可以让Vector3f=Vector3的,也就是说存在相互引用
heronism 2017-11-24
  • 打赏
  • 举报
回复
typedef struct Vector3f
{
    Vector3f(float _x, float _y, float _z) :x(_x), y(_y), z(_z), entryPoint(NULL) {}
    float x, y, z;
    Edge* entryPoint;
    /* 这个赋值搞错了吧,参数应该是Vector3f &,编译器应该有相关提示吧 */
    Vector3f& operator=(const Vector3& vec3)
    {
        x = vec3.x;
        y = vec3.y;
        z = vec3.z;
        entryPoint = NULL;
    }
}*pVector3f;
自信男孩 2017-11-24
  • 打赏
  • 举报
回复
typedef struct Vector3
{
    float x, y, z;
    Vector3(float _x,float _y,float _z):x(_x),y(_y),z(_z){}
}*pVector3;
把struct Vector3的声明提前,遵循先声明后使用的原则; 结构体struct Vector3f中,这些操作是对结构体struct Vector3的结构体成员的使用,所以声明必须在前;
Vector3f& operator=(const Vector3& vec3)
    {
        x = vec3.x;
        y = vec3.y;
        z = vec3.z;
        entryPoint = NULL;
    }
暮光后 2017-11-24
  • 打赏
  • 举报
回复
有没大神可以帮个忙啊
错误 C2027 使用了未定义类型“Vector3”
错误 C2228 “.x”的左边必须有类/结构/联合
icewebar 2017-11-24
  • 打赏
  • 举报
回复
typedef struct Edge EDGE; typedef struct Vector3 MVector;//typedef typedef struct Vector3f { Vector3f(float _x, float _y, float _z) :x(_x), y(_y), z(_z),entryPoint(NULL) {} float x, y, z; Edge* entryPoint; /*use new typeMVector3*/ Vector3f& operator=(const MVector3& vec3) { x = vec3.x; y = vec3.y; z = vec3.z; entryPoint = NULL; } }*pVector3f; struct Vector3 { float x, y, z; Vector3(float _x,float _y,float _z):x(_x),y(_y),z(_z){} }; struct Edge { }; 这样你试试
开源学开源 2017-11-23
  • 打赏
  • 举报
回复
struct Edge;
struct Vector3f;
struct Vector3;
typedef struct Vector3
{
	float x, y, z;
	Vector3(float _x, float _y, float _z) :x(_x), y(_y), z(_z) {}
}*pVector3;
typedef struct Vector3f
{
	Vector3f(float _x, float _y, float _z) :x(_x), y(_y), z(_z), entryPoint(NULL) {}
	float x, y, z;
	Edge* entryPoint;
	Vector3f& operator=(const Vector3& vec3)
	{
		x = vec3.x;
		y = vec3.y;
		z = vec3.z;
		entryPoint = NULL;
	}
}*pVector3f;

struct Edge
{

};
把vector3放前面定义就行了!至于为什么,我就不知道!你还得看看声明了

65,179

社区成员

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

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