Structs in classes

This is a discussion on Structs in classes within the C++ Programming forums, part of the General Programming Boards category; Here is my code: Code: class GK{ public: struct General { char* str1; float i; }; }; and in my ...

  1. #1
    Unregistered
    Guest

    Structs in classes

    Here is my code:
    Code:
    class GK{
    public:
       struct General {
           char* str1;
           float   i;
       };
    
    };
    and in my cpp file I have in my main:
    Code:
    GK* g;
    
    int main()
    {
        g->General.str1 = "hello world!";
    
    return 0;
    }
    the problem is:
    error C2273: 'function-style cast' : illegal as right side of '->' operator
    error C2228: left of '.aspect' must have class/struct/union type

    I am using VC++6, can someone tell me whats wrong here?
    Thanks

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    You have made a pointer of class type GK. You have not yet made a GK object however, therefore no place to store the string.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    The pointer to your class is g but you don't have an instance of your class. You also need to alocate some memory and get a pointer to your struct before you can use it.
    Last edited by Barjor; 06-06-2002 at 12:49 PM.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    Code:
    class GK{
    public:
       struct General {
           char* str1;
           float   i;
       }general; //instance of struct
    
    };
    
    GK* g = new GK; //create a new object of your class
    
    int main()
    {
        g->general.str1 = "hello world!";
    
    	delete g; //free memory used by new
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs or classes?
    By legit in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2009, 11:16 AM
  2. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 11:26 AM
  3. Why use Classes instead of Structs?
    By yaya in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2008, 01:39 AM
  4. Are structs and classes the same thing?
    By dwks in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2005, 03:21 PM
  5. Classes or Structs?
    By DeanDemon in forum C++ Programming
    Replies: 12
    Last Post: 12-05-2002, 03:58 AM

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