#Include problems

This is a discussion on #Include problems within the C++ Programming forums, part of the General Programming Boards category; I have two classes that need to know about each other. But whenever I #include "Theother.h", I get no-type errors. ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    #Include problems

    I have two classes that need to know about each other. But whenever I #include "Theother.h", I get no-type errors.

    Code:
    /*
     *  PCB.h
     *  3146Assignment4
     *
     *  Created by Sterling McLeod on 10/6/10.
     *  Copyright 2010 University of North Carolina at Charlotte. All rights reserved.
     *
     */
    #ifndef PCB_H
    #define PCB_H
    #define HOLD 0
    #define READY 1
    #define WAITING 2
    #define RUNNING 3
    #define FINISHED 4
    
    
    #include "Job.h"
    
    class PCB {
    public:
    	
    	PCB(Job&);
    	~PCB();
    	
    	int& getRegisterCounter();
    	void setRegisterCounter(int c);
    	
    	int& getProcessStatus();
    	void setProcessStatus(int p);
    	
    	std::string getJobID();
    	
    	void setJob(Job&);
    	Job& getJob();
    private:
    	int registerCounter;
    	int processStatus;
    	std::string jobID;
    	Job* job;
    };
    #endif
    Code:
    /*
     *  PCB.cpp
     *  3146Assignment4
     *
     *  Created by Sterling McLeod on 10/6/10.
     *  Copyright 2010 University of North Carolina at Charlotte. All rights reserved.
     *
     */
    #include "PCB.h"
    
    
    PCB::PCB(Job& j) : job(&j) {
    	jobID = j.getDescription();
    	registerCounter = 0;
    	processStatus = HOLD;
    }
    
    PCB::~PCB() {}
    
    
    int& PCB::getRegisterCounter() {return registerCounter;}
    void PCB::setRegisterCounter(int c) {registerCounter = c;}
    
    int& PCB::getProcessStatus() {return processStatus;}
    void PCB::setProcessStatus(int p) {processStatus = p;}
    
    std::string PCB::getJobID() {return jobID;}
    
    void PCB::setJob(Job& j) {job = &j;}
    Job& PCB::getJob() {return *job;}
    Code:
    /*
     *  Job.h
     *  3146Assignment4
     *
     *  Created by Sterling McLeod on 10/6/10.
     *  Copyright 2010 University of North Carolina at Charlotte. All rights reserved.
     *
     */
    #ifndef JOB_H
    #define JOB_H
    
    #include <string>
    #include "PCB.h"
    
    class Job {
    	
    public:
    	
    	Job(int cycle, std::string descrip);
    	~Job();
    	
    	void setCPUCycle(int x);
    	int& getCPUCycle();
    	
    	PCB& getPCB();
    	void setPCB(PCB&);
    	void makePCB();
    	
    	int& getWaitingTime();
    	void setWaitingTime(int t);
    	
    	int& getTurnaroundTime();
    	void setTurnaroundTime(int t);
    	
    	std::string getDescription();
    	
    	
    private:
    	int waitingTime;
    	int turnaroundTime;
    	int cpuCycle;
    	std::string description;
    	PCB* pcb;
    };
    #endif
    Code:
    /*
     *  Job.cpp
     *  3146Assignment4
     *
     *  Created by Sterling McLeod on 10/6/10.
     *  Copyright 2010 University of North Carolina at Charlotte. All rights reserved.
     *
     */
    
    #include "Job.h"
    
    Job::Job(int cycle, std::string descrip) : 
    cpuCycle(cycle), description(descrip) {}
    
    Job::~Job() {}
    
    void Job::setCPUCycle(int x) {cpuCycle = x;}
    int& Job::getCPUCycle() {return cpuCycle;}
    
    PCB& Job::getPCB() {return pcb;}
    void Job::setPCB(PCB& p) {pcb = p;}
    void Job::makePCB() {
    	PCB* apcb = new PCB(this);
    	this->setPCB(apcb);
    }
    
    int& Job::getWaitingTime() {return waitingTime;}
    void Job::setWaitingTime(int t) {waitingTime = t;}
    
    int& Job::getTurnaroundTime() {return turnaroundTime;}
    void Job::setTurnaroundTime(int t) {turnaroundTime = t;}
    
    std::string Job::getDescription() {return description;}
    I tried using forward declarations, but it still gave me the same problems. Can anyone tell me a way to fix this? Any help is appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Oh, sorry I forgot to include the errors. I get -
    Code:
    In file included from /Users/sterlingmtheman/Documents/3146Assignment4/Job.h:13,
                     from /Users/sterlingmtheman/Documents/3146Assignment4/Job.cpp:10:
    /Users/sterlingmtheman/Documents/3146Assignment4/PCB.h:23: error: expected `)' before '&' token
    /Users/sterlingmtheman/Documents/3146Assignment4/PCB.h:34: error: 'Job' has not been declared
    /Users/sterlingmtheman/Documents/3146Assignment4/PCB.h:35: error: ISO C++ forbids declaration of 'Job' with no type
    /Users/sterlingmtheman/Documents/3146Assignment4/PCB.h:35: error: expected ';' before '&' token
    /Users/sterlingmtheman/Documents/3146Assignment4/PCB.h:40: error: ISO C++ forbids declaration of 'Job' with no type
    /Users/sterlingmtheman/Documents/3146Assignment4/PCB.h:40: error: expected ';' before '*' token
    /Users/sterlingmtheman/Documents/3146Assignment4/Job.cpp: In member function 'PCB& Job::getPCB()':
    /Users/sterlingmtheman/Documents/3146Assignment4/Job.cpp:20: error: invalid initialization of reference of type 'PCB&' from expression of type 'PCB*'
    /Users/sterlingmtheman/Documents/3146Assignment4/Job.cpp: In member function 'void Job::setPCB(PCB&)':
    /Users/sterlingmtheman/Documents/3146Assignment4/Job.cpp:21: error: cannot convert 'PCB' to 'PCB*' in assignment
    /Users/sterlingmtheman/Documents/3146Assignment4/Job.cpp: In member function 'void Job::makePCB()':
    /Users/sterlingmtheman/Documents/3146Assignment4/Job.cpp:23: error: no matching function for call to 'PCB::PCB(Job* const)'
    /Users/sterlingmtheman/Documents/3146Assignment4/PCB.h:20: note: candidates are: PCB::PCB()
    /Users/sterlingmtheman/Documents/3146Assignment4/PCB.h:20: note:                 PCB::PCB(const PCB&)
    /Users/sterlingmtheman/Documents/3146Assignment4/Job.cpp:24: error: no matching function for call to 'Job::setPCB(PCB*&)'
    /Users/sterlingmtheman/Documents/3146Assignment4/Job.cpp:21: note: candidates are: void Job::setPCB(PCB&)

  3. #3
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    So, PCB.h includes Job.h, which includes PCB.h, which includes Job.h, which includes PCB.h, which... I'm getting dizzy...

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    .

    Well I use the #ifndef #endif marks to make them only include once. Or maybe I'm understanding the use of those wrong?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    From both the .h files, remove the #include of the other .h file.

    In Job.h for example, before your class declaration, add

    class PCB;


    Likewise, in pcb.h, before the main class declaration, add

    class Job;


    Both .cpp files would need to include both .h files (in either order).

    So long as each class only contains A POINTER to the other class, then you should be OK.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Or reference. References or pointers.
    Problem is that the compiler parses top to bottom.
    So the preprocessor will encounter the include for Job.h, then PCB.h, then Job.h to infinity. Makes sense? Obviously this cannot work. Thus, the solution is to forward declare them, as Salem demonstrates.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. in a constant loop with threads using socket
    By kiros88 in forum C Programming
    Replies: 1
    Last Post: 08-21-2009, 07:34 PM
  2. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 10:19 AM
  3. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM
  4. #Include "class" problems
    By Unregistered in forum C++ Programming
    Replies: 17
    Last Post: 11-26-2001, 10:40 AM

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