#Include "class" problems

This is a discussion on #Include "class" problems within the C++ Programming forums, part of the General Programming Boards category; Assuming all your function names match in your actual project and that Adventure::random() has been declared in your class header, ...

  1. #16
    Unregistered
    Guest
    Assuming all your function names match in your actual project and that Adventure::random() has been declared in your class header, you could try re-building all of your project as MSVC doesn't always recompile files when it should.

  2. #17
    Unregistered
    Guest

    Nothing...

    I will print the files after another and see if the helps.


    Adventure.h
    ---------------
    #ifndef ADVENTURE_H
    #define ADVENTURE_H

    class Adventure
    {
    public:
    void happenOnRoll();
    void monsterMeeting();
    void itemFind();
    void goldFind();

    int random(int RND_MAX);
    }a;

    #endif


    Adventure.cpp
    ------------------
    #include <iostream>
    #include <ctime>
    using namespace std;
    #include "Adventure.h"

    void Adventure::happenOnRoll()
    {
    int rndSeed = 4;
    int randAction;

    randAction = random(rndSeed);

    switch (randAction)
    {
    case 1:
    monsterMeeting();
    break;

    case 2:
    itemFind();
    break;

    case 3:
    goldFind();
    break;
    }
    }

    // Function that randomizes a number between 1 and RND_MAX
    int Adventure::random(int RND_MAX)
    {
    unsigned int rndNum;
    rndNum = 1 + rand() % RND_MAX;
    return rndNum;
    }

    void Adventure::monsterMeeting()
    {
    // just putting out text for the moment
    cout << "You see a horrid creature coming at you" << endl;
    }

    void Adventure::itemFind()
    {
    // just putting out text for the moment
    cout << "You have found an item" << endl;
    }

    void Adventure::goldFind()
    {
    // just putting out text for the moment
    cout << "You have found some gold" << endl;
    }


    Main.cpp
    -----------
    #include <iostream>
    #include <ctime>
    using namespace std;
    #include "Character.h"
    #include "Adventure.h"


    //***********************************************
    // Main.cppīs function declarations
    //***********************************************
    void gameMenu(); // Game menu function
    void printCoordinates();
    const int rndSeed = 6; // number to randomize
    int random(int RND_MAX); // random function

    //************************************************** *
    // Game starting menu
    //************************************************** *
    void gameMenu()
    {
    char menuChoice;

    cout << "*******************" << endl;
    cout << "=NIGHT OF VENGANCE=" << endl;
    cout << "*******************" << endl << endl;

    cout << "What do you want to do?" << endl;
    cout << "1.Start a new game" << endl;
    cout << "2.Load a saved game" << endl;
    cout << "3.Quit" << endl;

    cin >> menuChoice;

    switch (menuChoice)
    {
    case '1':
    cout << "Start the game" << endl;
    break;

    case '2':
    cout << "Load a game" << endl;
    break;

    case '3':
    cout << "Exiting the game" << endl;
    break;

    default:
    break;
    }
    }

    //************************************************** **************
    // Characters walking function
    //************************************************** **************
    void Character::walking()
    {
    char menuChoice;
    cout << "Move the character, and se how the coordinates change" << endl;

    while (menuChoice != '5')
    {
    cout << "Where do you want to go?" << endl;

    if (x > 50)
    x = 50; // Highest value of x
    if (x < 0)
    x = 0; // Lowest value of x
    if (y > 50)
    y = 50; // highest value of y
    if (y < 0)
    y = 0; // Lowest value of y

    printCoordinates();

    if (y < 50)
    cout << "1.North" << endl;
    if (x > 0)
    cout << "2.West" << endl;
    if (x < 50)
    cout << "3.East" << endl;
    if (y > 0)
    cout << "4.South" << endl;

    cout << "5.Exit program" << endl;


    cin >> menuChoice;

    switch (menuChoice)
    {
    case '1':
    y += random(rndSeed); // How far you get in a direction
    //a.happenOnRoll(); // Calls the function happenOnRoll
    break;

    case '2':
    x -= random(rndSeed);
    //a.happenOnRoll();
    break;

    case '3':
    x += random(rndSeed);
    //a.happenOnRoll();
    break;

    case '4':
    y -= random(rndSeed);
    //a.happenOnRoll();
    break;

    default:
    break;
    }
    system("cls");
    }
    }

    // prints character coordinates
    void printCoordinates() // For testing only
    {
    cout << "X = " << c.getXcoord() << endl;
    cout << "Y = " << c.getYcoord() << endl << endl;
    }

    // Function take a int argument RND_MAX, and returns the randomized number
    //*****************
    // Random function
    //*****************
    int random(int RND_MAX)
    {
    unsigned int rndNum;
    srand(time(0));
    rndNum = 1 + rand() % RND_MAX;

    return rndNum;
    }

    //****************
    // Main function
    //****************
    int main()
    {
    a.happenOnRoll(); // Testing (fails when I try this)
    gameMenu();
    c.walking();
    return 0;
    }


    Hope it shows as it should do!!

  3. #18
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Some general things:

    Drop the variables from your headers. No variable declaration in header files !

    you header file should look like

    #define XXX_H
    #ifndef XXX_H

    class XXX
    {
    //stuff here
    };

    #endif //XXX_H

    Your error means the functions belonging to your class cannot be found. The compiler noticed it's syntactically fine, but your linker had no clue where it should get the code from. Make sure you have included your .cpp file in your project.

    If you need variables, declare them in one .cpp file. If they are global, make an headerfile called globals.h and declare them as extern there.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 01:53 PM
  2. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:19 AM
  3. some problems with #include
    By Cmaniac in forum C Programming
    Replies: 6
    Last Post: 04-15-2007, 11:28 PM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

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