Skip to main content
gamedev.net
🔒 Locked

Include problem in class example

Started by Afr0m@n 4 replies 600+ views
Original Post
Afr0m@n
Afr0m@n
Bleh. I'm @ school and I'm bored because we're having a test with stupid tasks. So I thought I'd install Dev C++ and have some fun. And I did - but I ran into a problem. ;/ Currently, when the compiler tries to compile, I get no errors, but the compilation process doesn't stop. It just goes on and on and takes 100% of the CPU so I have to shut down the compiler. Here's the source: CPerson.cpp:
#include "CPerson.h"

int CPerson::TurnOnWashingMachine(int LaundryProgram, CWashingMachine &WMachine)
{
    bool result = WMachine.InitiateWash(LaundryProgram);
    
    if(result = true)
       std::cout << "Turned on the washing machine successfully!";
}

CPerson.h
#include "globals.h"

class CPerson
{
      int LaundryProgram1;
      int LaundryProgram2;
      int LaundryProgram3;
      
      int TurnOnWashingMachine(int LaundryProgram, CWashingMachine &WMachine);
};      

CWashingMachine.cpp:
#include "CWashingMachine.h"

bool CWashingMachine::InitiateWash(int Laundryprogram)
{
     return true;
}

CWashingMachine.h
class CWashingMachine
{
private:      
      int LaundryProgram1;
      int LaundryProgram2;
      int LaundryProgram3;

public:      
      bool InitiateWash(int LaundryProgram);
};      

globals.h:
#include "CPerson.h"
#include "CWashingMachine.h"

CPerson Person;
CWashingMachine WashingMachine;

extern CPerson.TurnOnWashingMachine(int LaundryProgram, CWashingMachine &WMachine);
extern CWashingMachine.InitiateWash(int LaundryProgram);

main.cpp:
#include <cstdlib>
#include <iostream>
#include "globals.h"

using namespace std;

int main(int argc, char *argv[])
{
    Person.TurnOnWashingMachine(1, &WashingMachine);
    system("PAUSE");
    return EXIT_SUCCESS;
}

_______________________Afr0Games
MaulingMonkey
MaulingMonkey
Google "C++ Include Guards", "C++ Header Guards", or "C++ Header Include Guards". Probably has some other names as well.

As is, CPerson.h includes globals.h includes CPerson.h includes ...... ad infinitum. Include guards will prevent this.
zalthar
zalthar
Well its an include-loop.

CPerson.h includes global.h
and
global.h includes CPerson.h

So it goes on forever.

You should also write header files like this:

#ifndef CPERSON_H#define CPERSON_H#include "globals.h"class CPerson{      int LaundryProgram1;      int LaundryProgram2;      int LaundryProgram3;            int TurnOnWashingMachine(int LaundryProgram, CWashingMachine &WMachine);};     #endif 


The preprocessor operators makes sure that the file is only included ones per file.

------------------------------- Zalthar -Joakim Karlsson------------------------------
ToohrVyk
ToohrVyk
You don't seem to be using the contents of "globals.h" anywhere in the "CPerson.h" header file. Why include it?
Zahlman
Zahlman
You also have a bug here once you get it to compile and link properly:

    bool result = WMachine.InitiateWash(LaundryProgram);        if(result = true)       std::cout << "Turned on the washing machine successfully!";


There's really no reason to keep a temporary value here, and there's IMHO never a good reason to compare a boolean value to a boolean literal. I would normally write

    if ( WMachine.InitiateWash(LaundryProgram) ) {       std::cout << "Turned on the washing machine successfully!";    }


(because I do believe in always bracing these things; it avoids errors later in a language where braces are needed - OTOH I have no problem dealing with Python either), and if I were in a real show-off mood I might write

    WMachine.InitiateWash(LaundryProgram) && std::cout << "Turned on the washing machine successfully!";


just because I have a little Perl experience ;)

Leaving out the extra bits doesn't really cost any clarity, saves effort, and avoids the needless opportunity to introduce a bug.
Afr0m@n
Afr0m@n
Thanks guys, issue resolved:)
_______________________Afr0Games

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.