Thread: more header files

  1. #1
    Unregistered
    Guest

    Angry more header files

    I am learning C++ and I am trying to link files together. I know in general how files are linked (did it for a Unix class) but I've noticed that when data structures are introduced, it makes things a lot more difficult. For the life of me, I cannot figure out how to split the program (below) into three files: main.cpp, expand.cpp, and expand.h

    The program below is the working program in one file. When I split it up into modules, I get all sorts of access violations and the blue screen of death. I have the two source files in /MyProjects, and the header in /Include (I've also tried keeping it in /MyProjects).

    I use Dev C++ and Windows. Any guidance would be appreciated.



    #include <iostream>
    #include <list>
    #include <cstdlib>

    using namespace std;

    class resources {
    private:
    float fertility;
    public:
    float GetFertility() { return fertility; }
    void SetFertility( float f ) { fertility = f; }
    };

    class terrain {
    private:
    int terrain_type;
    public:
    int GetTerrain() { return terrain_type; }
    void SetTerrain( int t ) { terrain_type = t; }
    };

    class world : public resources, terrain {
    private:
    int x, y;
    public:
    int Getx() { return x; }
    int Gety() { return y; }
    void Setxy( int a, int b ) { x = a; y = b; }
    };

    void ExpandMap( list<world> &ob )
    {

    list<world>::iterator p;

    p = ob.begin();

    p->Setxy(2,3);
    cout << "X and Y: " << p->Getx() << ' ' << p->Gety() << endl;

    }

    int main()
    {

    list<world> map;
    list<world>::iterator p;

    map.push_back();

    ExpandMap( map );

    p = map.begin();
    cout << "\n\nMAIN: X and Y: " << p->Getx() << ' ' << p->Gety() << endl;

    system("PAUSE");
    return 0;

    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'll give it a shot

    > expand.h
    // Contains the three classes resources, terrain and world
    // The function prototype
    void ExpandMap( list<world> &ob );

    > expand.cpp
    #include <iostream>
    #include <list>
    #include <cstdlib>
    #include "expand.h"
    // And the function declaration
    void ExpandMap( list<world> &ob ) {
    // code
    }

    > main.cpp
    #include <iostream>
    #include <list>
    #include <cstdlib>
    #include "expand.h"
    // And the function declaration
    int main ( ) {
    // code
    return 0;
    }
    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.

  3. #3
    Unregistered
    Guest

    Thumbs up

    Thank you Salem!

    Just one more question for right now. I noticed that you put the #include <list> in expand.cpp but not expand.h. That confused me because I assumed expand.h would have to 'see' the <list> because of this prototype in the header file:

    void ExpandMap( list<world> & );

    With a little playing around I've discovered that the code compiles and runs whether <list> is in expand.cpp, expand.h, or both.
    Can you provide any information on how header files are 'seen' by the various parts of the code?

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM