宏定义里面含有结构体怎么办

tiandyoin 2008-10-15 10:34:38
有这么个宏定义:
struct node{
int x,y;
}P[N];
#define MAKE_VECTOR(p1,p2) (struct node) {P[p2].x-P[p1].x,P[p2].y-P[p1].y}
int crossP(struct node v1,struct node v2){
return v1.x*v2.y - v2.x*v1.y;
}
int isleft(int p1,int p2,int p3){
return crossP(MAKE_VECTOR(p1,p2),MAKE_VECTOR(p2,p3));
}
可是不知道怎么写才对,总报错~~~。请高手指点下
...全文
313 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tiandyoin 2008-12-15
  • 打赏
  • 举报
回复
我用的是vc++ 6.0
tiandyoin 2008-12-15
  • 打赏
  • 举报
回复
// compute convex hull via melkman algorithm
// procondition: the original points' queue should be a simple polygonal chain
// O(n) using deque
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define N 1024
#define pi 3.141592653589793
struct node
{
int x,y;
} P[N];
#define Make_Vector(p1,p2) (struct node){P[p2].x-P[p1].x, P[p2].y-P[p1].y}


int D[N*2],top,bot;
int n,L;
bool input()
{
if (scanf("%d%d",&n,&L) != 2) return false;
for (int i = 0;i < n; i++) scanf("%d%d",&P[i].x,&P[i].y);
return true;
}
// v1 and v2 is two vector
inline int crossP(struct node v1,struct node v2)//叉积 Cross Product
{
return v1.x * v2.y - v2.x * v1.y;
}
/*inline struct node Make_Vector(int a,int b)
{
struct node n;
n.x = P[b].x - P[a].x;
n.y = P[b].y - P[a].y;
return n;
}*/
int isleft(int p1,int p2,int p3)
{
return crossP(Make_Vector(p1,p2),Make_Vector(p2,p3));
}


上面摘自http://richardxx.yo2.cn/articles/%E5%BC%80%E5%A7%8B%E5%AD%A6%E4%B9%A0%E8%AE%A1%E7%AE%97%E5%87%A0%E4%BD%95.html
我的程序一部分是这样的,上面的return crossP会报错,似乎不能用,怎么改呢,就事论事,不要说改成函数啊这类的。。。

错误提示是:
tangtang02520 2008-10-16
  • 打赏
  • 举报
回复
1.N定义了没有?如果定义了,就不是语法错误了 而是
2.P[N]定义为全局变量,所以所有的P[i].x == 0,P[i].y == 0
调用MAKE_VECTOR宏得到的所有结构的值也都是{0,0} 可能和你想要的值不一样
另外,up楼上,最好用函数
  • 打赏
  • 举报
回复
你写不用宏写出来看看

这个地方最好将MAKE_VECTOR写成函数.
开心爸爸84 2008-10-15
  • 打赏
  • 举报
回复
用函数吧

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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