problem defining a function inside a structure in c
-
my code is: #include #include struct point { int x,y; void getdis() { printf("%d",x+y); } }; int main() { struct point p; int a=5,b=10; p.getdis(a,b); getch(); return 0; } problem is:8 2 F:\software\Dev-Cpp\bin\point.c [Error] expected ':', ',', ';', '}' or '__attribute__' before '{' token I cannot understand what to write before the getdis function to avoid this error
suman chandra modak
-
my code is: #include #include struct point { int x,y; void getdis() { printf("%d",x+y); } }; int main() { struct point p; int a=5,b=10; p.getdis(a,b); getch(); return 0; } problem is:8 2 F:\software\Dev-Cpp\bin\point.c [Error] expected ':', ',', ';', '}' or '__attribute__' before '{' token I cannot understand what to write before the getdis function to avoid this error
suman chandra modak
void getdis()
...
p.getdis(a,b);These two do not match. Hint: they should.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
my code is: #include #include struct point { int x,y; void getdis() { printf("%d",x+y); } }; int main() { struct point p; int a=5,b=10; p.getdis(a,b); getch(); return 0; } problem is:8 2 F:\software\Dev-Cpp\bin\point.c [Error] expected ':', ',', ';', '}' or '__attribute__' before '{' token I cannot understand what to write before the getdis function to avoid this error
suman chandra modak
methods are a
C++
feature you can onlymimic
inC
, suing function pointers and the like. For instance#include
struct point
{
int x,y;
void (*pfun)(struct point *pp);
};void getdis(struct point *pp)
{
printf("%d\n", (pp->x + pp->y));
}int main()
{
struct point p;
p.x = 5; p.y = 10;
p.pfun = getdis;p.pfun(&p);
getchar();
return 0;
}