more header files

This is a discussion on more header files within the C++ Programming forums, part of the General Programming Boards category; I am learning C++ and I am trying to link files together. I know in general how files are linked ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 11:21 AM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 06:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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