pointer to struct object in class object

This is a discussion on pointer to struct object in class object within the C++ Programming forums, part of the General Programming Boards category; Hi how do you get a struct object to work inside a class object? [code] struct MyStruct{float x,float y}; class ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    23

    pointer to struct object in class object

    Hi how do you get a struct object to work inside a class object?

    [code]

    struct MyStruct{float x,float y};

    class CMyClass
    {
    MyStruct ArrayStruct[3]; //this works
    MyStruct *PointerStruct; //this doesn't
    //...
    };

    [code]
    I can put values in the first one, but once I've done ;

    [code]
    PointerStruct=new MyStruct[3];
    [code]I still cant get any values into it.
    Last edited by Stevo; 02-25-2004 at 05:19 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >struct MyStruct{float x,float y};

    Correct is:
    Code:
    struct MyStruct{float x;float y;};
    Then it should work fine with:
    MyStruct *PointerStruct;

    If not, post some code, or explain the problem in more detail.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Are you doing something like this:
    Code:
    #include <iostream>
    
    struct MyStruct
    {
      float x;
      float y;
    };
    
    class myClass
    {
      private:
        MyStruct  *p;
    
      public:
        void testme(void)  
        { 
          p = new MyStruct[3]; 
          p[0].x = 10.0; 
          
          //
        }
    };
    
    int main(void)
    {
      myClass c;
      c.testme();
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    23
    Hammer, yes that is what I'm doing, I use a separate function for putting the values in though, I'd also like to get it where I can put in or change any of the struct values dynamically as well. If there doesn't seem to be any thing wrong with this then I'll go and try it with a console app so that I can test if the values are getting into the structs, at the moment I'm using a graphics thing, and it's only because the graphics aren't showing up that I think the values aren't getting into the structs.
    Last edited by Stevo; 02-25-2004 at 08:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. struct pointer
    By t014y in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 03:50 PM
  3. Is there a way to tell what class an object is?
    By Loduwijk in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 09:20 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21