Class with members as classes

This is a discussion on Class with members as classes within the C++ Programming forums, part of the General Programming Boards category; hey ppl ive got a little problem here... Code: #ifndef _EVENT_H_ #define _EVENT_H_ class Time; class Date; class Event { ...

  1. #1
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18

    Class with members as classes

    hey ppl ive got a little problem here...

    Code:
    #ifndef _EVENT_H_
    #define _EVENT_H_
    
    class Time;
    class Date;
    
    class Event
    {
    private:
    	
    	Date inputDate;
    	Time startTime;
    	int inputTimeInterval;
    	Time endTime;
    	std::string eventName;
    	std::string eventLocaton;
    	std::string eventComments;
    
    public:
    	Event();
                    ~Event();
    	void printEvent();
    }
    #endif
    --------------------------------------------
    this is my class for event
    ive declared another class (like date and time) as its members...
    does this method of forward declaration works...
    how would i initialize Event() constructor???

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    if think you must clarify your question a little.

    What do you mean by "initialize Event() constructor"?

  3. #3
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18
    ok just tell me that have i declred the class Event correctly or not (assumung Date and Time are present in their .h and .cc files)??

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    looks fine to me. You just forgot a ; at the end of the class decl.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    The forward declaration is not enough here. You must include the header files that define Date and Time. You can use the forward declaration if your class definition includes only references or pointers to the other classes, but in this case, your class has actual member instances. In addition, you'd probably need to include <string>.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: Class with members as classes

    Originally posted by justdoit22
    does this method of forward declaration works...
    No. In this case you need the actual class definition, not just a declaration. Forward declarations will only let you refer to an instance of the class indirectly (IE via pointers and references).

    Originally posted by justdoit22
    how would i initialize Event() constructor???
    Rephrase that

  7. #7
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18
    ok i put STATIC in front of a DATE and TIME member as belo
    in Event.h
    Code:
    #ifndef _EVENT_H_
    #define _EVENT_H_
    
    class Time;
    class Date;
    
    class Event
    {
    private:
    	
    	static Date inputDate;
    	static Time startTime;
    	int inputTimeInterval;
    	static Time endTime;
    	std::string eventName;
    	std::string eventLocation;
    	std::string eventComments;
    
    public:
    	Event();
    	void printEvent();
    	//void set_inputDate()
    };
    #endif 
    in Event.cc

    now compiler doesnt complain about (i dont know DATE undeclred type etc etc...)

    ive the Event initialized as follows
    Code:
    Event::Event(): inputDate(SimpleInput::ReadDate("Please Enter Date: ")),
    				startTime(SimpleInput::ReadTime("Enter Event Start Time (hh:mm 24hr format): ")),
    				inputTimeInterval(SimpleInput::ReadInteger("Please Enter The Time (in minutes) Interval for Event: ")),
    				eventName(SimpleInput::ReadLine("What's the Event Name: ")),
    				eventLocation(SimpleInput::ReadLine("What's the Event Location: ")),
    				eventComments(SimpleInput::ReadLine("Any Comments: "))
    {
    
    
      //adds timeInterval to get endTime
      	int mhours= inputTimeInterval/60;
    	int mminutes= inputTimeInterval%60;
    	int hours= startTime.getHour();
    	int minutes= startTime.getMinute();
    	int endHour = hours+mhours;
    	int endMinute = minutes+mminutes;
    	if (endMinute>59)
    		{
    			endMinute = endMinute - 60;
    			endHour = endHour + 1;
    		}
    
      //gives endTime of an event
    
      this->endTime = Time(endHour, endMinute);
    }
    all other things are correct just this error is coming 2 times...
    event.cpp(17) : error C2614: 'Event' : illegal member initialization: '<Unknown>' is not a base or member

    While cpp(17 ) refers to line where { bracket of Event constructor starts

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Chances are, you really don't want those members to be static. Making them static means that only one copy of that member will exist, and it will be shared by all the instances of the class.

    Additionally, in your source file, did you remember to include the headers for the other classes? That is most likely the cause of your error.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Registered User justdoit22's Avatar
    Join Date
    Dec 2003
    Posts
    18
    i had to include these files in Event.h
    and now my very first declaration of Event works fine....

    #include <string>
    #include "Time.h"
    #include "Date.h"


    i had to include those files so that i can make member variables of type Date and Time..

    Thanx all
    hope ill post another problem too....lolz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An Array of Classes in a different Class
    By mas0nite in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2006, 03:28 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. organizing classes in a game
    By MathFan in forum Game Programming
    Replies: 2
    Last Post: 04-28-2005, 10:23 AM
  4. VC++ IntelliSense (showing class members)
    By _Elixia_ in forum Tech Board
    Replies: 0
    Last Post: 08-02-2003, 02:14 PM
  5. Protected Inheritance
    By golfinguy4 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2002, 10:56 AM

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