Thread: Class Include Problem (I Think)

  1. #1
    Registered User gpr1me's Avatar
    Join Date
    Mar 2006
    Posts
    14

    Class Include Problem (I Think)

    Im having a very weird problem. Im defining some classes:

    Project (base Class)
    --> SimpleProject
    --> MinorProject
    --> MajorProject

    Task

    Now my project class definition is:

    Code:
    #ifndef PROJECT_H
    #define PROJECT_H
    
    #include "Task.h"
    
    class Project
    {
    
    public:
    	Project();
    	Project(const Project &);
    	
    	virtual ~Project();
    
    	virtual void AddTask(const Task & t) = 0;
    	virtual void DoTask() = 0;
    	virtual void HasTasks() = 0;
    
    private:
    	// still to add...
    };
    
    #endif
    As you can see, i have a pure virtual function AddTask which takes a constant reference to a task object as it's papameter. That means i had to include the Task.h file in Project.h.

    Ok no problem, and my Task.h file is:

    Code:
    #ifndef TASK_H
    #define TASK_H
    
    #include "Project.h"
    
    class Task
    {
    
    public:
    	Task();	
    	Task(const Task & t);
    	Task(int, const Project &);	
    	~Task();
    
    	void DoIt();
    
    private:
    	int id;
    	Project *pp;
    
    };
    
    #endif
    I have included Project.h because i want to have a pointer to the project which this task is associated with. So i have included Project.h in Task.h. Now if i do this everything goes crazy when i compile.

    i get these error messages:

    Code:
    Error	1	error C2062: type 'int' unexpected	d:\school\comp446\project\a3\task.h	12	
    Error	2	error C2238: unexpected token(s) preceding ';'	d:\school\comp446\project\a3\task.h	12	
    Error	3	error C2143: syntax error : missing ';' before '*'	d:\school\comp446\project\a3\task.h	19	
    Error	4	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\school\comp446\project\a3\task.h	19	
    Error	5	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\school\comp446\project\a3\task.h	19	
    Error	6	error C2062: type 'int' unexpected	d:\school\comp446\project\a3\task.h	12	
    Error	7	error C2238: unexpected token(s) preceding ';'	d:\school\comp446\project\a3\task.h	12	
    Error	8	error C2143: syntax error : missing ';' before '*'	d:\school\comp446\project\a3\task.h	19	
    Error	9	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\school\comp446\project\a3\task.h	19	
    Error	10	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\school\comp446\project\a3\task.h	19	
    Error	11	error C2062: type 'int' unexpected	d:\school\comp446\project\a3\task.h	12	
    Error	12	error C2238: unexpected token(s) preceding ';'	d:\school\comp446\project\a3\task.h	12	
    Error	13	error C2143: syntax error : missing ';' before '*'	d:\school\comp446\project\a3\task.h	19	
    Error	14	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\school\comp446\project\a3\task.h	19	
    Error	15	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\school\comp446\project\a3\task.h	19	
    Error	16	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\school\comp446\project\a3\project.h	15	
    Error	17	error C2143: syntax error : missing ',' before '&'	d:\school\comp446\project\a3\project.h	15	
    Error	18	error C2062: type 'int' unexpected	d:\school\comp446\project\a3\task.h	12	
    Error	19	error C2238: unexpected token(s) preceding ';'	d:\school\comp446\project\a3\task.h	12	
    Error	20	error C2143: syntax error : missing ';' before '*'	d:\school\comp446\project\a3\task.h	19	
    Error	21	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\school\comp446\project\a3\task.h	19	
    Error	22	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\school\comp446\project\a3\task.h	19	
    Error	23	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	d:\school\comp446\project\a3\project.h	15	
    Error	24	error C2143: syntax error : missing ',' before '&'	d:\school\comp446\project\a3\project.h	15
    Is this caused by circular includes? Should i not be including Project/h in Task.h and Task.h in Project.h? Because ass soon as i do not use Task.h in Project/h the errors go away.

    If the circular includes is the problem how would i use Task objects in my Project file and vice versa?

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Try putting them in the same file (without the includes) and see if you get the same errors perhaps?

  3. #3
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    keep every thing the same but take out the includes in each of your .h files then do this

    Code:
    #include Task.h
    #include Project.h
    ect...

  4. #4
    Registered User gpr1me's Avatar
    Join Date
    Mar 2006
    Posts
    14
    Quote Originally Posted by loopshot
    keep every thing the same but take out the includes in each of your .h files then do this

    Code:
    #include Task.h
    #include Project.h
    ect...
    You mean?

    Code:
    #include "Task.h"
    #include "Project.h"
    And where do i put this?

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    main perhaps?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably need to use forward declarations. In Task.h, replace:
    #include "Project.h"
    with
    class Project;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In fact, you should use forward declarations instead of #includes in header files whenever they are enough, so you should change #include "Task.h" to class Task; as well.

  8. #8
    Registered User gpr1me's Avatar
    Join Date
    Mar 2006
    Posts
    14
    awesome, thanks alot, that solved the problem. I will read up on forward declarations now.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In a nutshell, at the point that the Project class is being compiled, the compiler doesn't need to know anything about the Task class. All you use in Project.h is a reference to something called Task, so the compiler doesn't need to know the size, or the member functions or anything else about Task, only that it is a class that is declared elsewhere. Later, when you actually use the class by creating an instance of it, calling a member function or using a member variable, do you need the full class declaration that you get by #including the Task.h header. You will probably be doing that in the cpp file, and so you will probably need to #include "Task.h" in the cpp file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 10:08 PM
  2. Weird, cards not outputting correctly
    By Shamino in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2007, 03:26 PM
  3. Multiple include problem
    By VirtualAce in forum Game Programming
    Replies: 13
    Last Post: 02-04-2006, 06:09 PM
  4. problem with class
    By Mr.Pink in forum C++ Programming
    Replies: 26
    Last Post: 07-10-2005, 10:24 PM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM