Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem defining a function inside a structure in c

problem defining a function inside a structure in c

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13646245
    wrote on last edited by
    #1

    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

    D CPalliniC 2 Replies Last reply
    0
    • U User 13646245

      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

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • U User 13646245

        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

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        methods are a C++ feature you can only mimic in C, 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;
        }

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups