Classes Help

This is a discussion on Classes Help within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm a c++ newbie and I need some help with classes. Take a look at this code snippet and ...

  1. #1
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40

    Classes Help

    Hi, I'm a c++ newbie and I need some help with classes. Take a look at this code snippet and see if you can figure out why I get an error. (Besides the absence of constructor/destructor)

    Code:
    #pragma once
    
    class Part {
    public:
    
    	int ID;
    	int daysNeeded;
    	int buyPrice;
    	int sellPrice;
    	char name[20];
    	char description[100];
    };
    
    Part item;
    item.ID = 5;
    Help is greatly appreciated!! Thanks
    - Grady (:

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Do you have the class definition in a separate file than the instance? Try putting the class stuff in a header and then in a separate cpp file instantiating the class and assigning the member ID.

    EDIT: Also a default constructor and destructor are provided for you if you do not specify them.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >item.ID = 5;
    This is the problem. You can create declarations in the global scope, but operations like this must be performed inside a function. Use the following to see the difference:
    Code:
    class Part {
    public:
    
    	int ID;
    	int daysNeeded;
    	int buyPrice;
    	int sellPrice;
    	char name[20];
    	char description[100];
    };
    
    Part item;
    
    int main()
    {
      item.ID = 5;
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User Gnoober's Avatar
    Join Date
    Oct 2002
    Posts
    40
    It worked! Thanks a lot, I would have never figured that out by myself. (;
    - Grady (:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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