Using classes in function outside main

This is a discussion on Using classes in function outside main within the C++ Programming forums, part of the General Programming Boards category; Please help me figure out what I'm doing wrong. I have a class, declared in a header file: Code: // ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    8

    Using classes in function outside main

    Please help me figure out what I'm doing wrong.

    I have a class, declared in a header file:
    Code:
    // std_dice.h 
    // header file for standard system include files,
    // project include files 
    
    #include <stdio.h>
    #include <tchar.h>
    
    // additional headers 
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <time.h>
    namespace DICE {
      class SingleDie {
        public:
        int nRolled;
        public:
        void RollDie();
      };
    }
    
    void fStupidFunction();
    Also declared was a stupid little function, to be used in main. It replaces a larger function, for the question, that was / is going to collect some statistics about rolled dice.

    Here is main.cpp, including the class definition, the main function, and the function where I can't properly use the class.

    Code:
    // main.cpp 
    #include "std_dice.h"
    using namespace std;
    
    int main(){ 
    /***************Classes here********************************/
      // set up the SingleDie class
      class SingleDie {
        public:
        int nRolled;
        
        // constructor
        SingleDie() {
          cout << "Constructor performed \n\n";  // for testing
          nRolled = 0;
        } // SingleDie constructor
        
        public: 
        void RollDie() {
          nRolled = 1;
          nRolled = 10 + rand() % 60;
          nRolled /= 10;
        } // RollDie
      
      }; //SingleDie class
    
    /***************Main testing here*******************************************/  
      using namespace DICE;
      SingleDie ssss;
      ssss.RollDie();
      cout << ssss.nRolled << "  = Value of nRolled in main \n\n";
      fStupidFunction();  
     
    // pause after executing console application
     system("PAUSE");
        return EXIT_SUCCESS;
    return 0;
    }//main
    
    /*********************Functions Here****************************/  
      void fStupidFunction() {
        //using namespace DICE;
        DICE::SingleDie sOneDice;
        sOneDice.nRolled = 7;  // Works: member variable is public for now
        sOneDice.RollDie();    // Doesn't work!
        // If I comment out the above line, the program compiles.
        cout << sOneDice.nRolled << " In the stupid little function! \n\n";
      };
    As per the comments in the function: fStupidFunction: I can access the member variable .nRolled just fine. (I'll later fix the class so that there is a member function to set .nRolled, make it private, and make sure it never gets set to 7.)

    In fact, if I comment out the line SOneDice.RollDie(); then the program compiles and runs. Therefore, I can suppose that my namespace is okay and that the class variable is declared okay--or can I? The problem comes when I try to use the member function RollDie().

    RollDie() works in main but not in fStupidFunction.

    It doesn't make sense to me that one cannot use a class variable in a function--only in main. I've looked in Bjarne's book (which is way above my head anyway), in C++ for Dummies (which doesn't cover the issue), and in various tutorials. If someone can help me to understand what I've done wrong and how to fix it, I would be very appreciative.

    Thanks,
    Keith

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your function RollDie is not defined. (Yes, I can see a definition in main(), but that definition is local to main() and therefore doesn't count.) All the class definition stuff should be in a separate file, not included in some other random function.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    Quote Originally Posted by tabstop View Post
    Your function RollDie is not defined. (Yes, I can see a definition in main(), but that definition is local to main() and therefore doesn't count.) All the class definition stuff should be in a separate file, not included in some other random function.
    Thanks!!! That fixed it... I moved the line with main(){ down to where I had the comment about main testing. (Makes sense, but I'm still very new at this stuff!!)

    Then I had to move the constructor to the header file and I left the other class member function up at the top. Probably ought to move it into the header file later...

    Anyway, I beat my head against the wall for hours not noticing what you told me about, so

    THANKS!!!!!

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Are you trying to save memory by excluding the "c" from "dice" in "RollDie" and "SingleDie"?

    J/k... but still, readability can sometimes be good. One could think this was a class about a dog which has the ability to roll over and die.
    home = hat.position;

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    8

    Talking

    Quote Originally Posted by DrSnuggles View Post
    Are you trying to save memory by excluding the "c" from "dice" in "RollDie" and "SingleDie"?

    J/k... but still, readability can sometimes be good. One could think this was a class about a dog which has the ability to roll over and die.
    LOL! One die plus one die equals two dice.

    FWIW, the result will be some stats about this silly little dice game some friends taught us after dinner a few weeks ago. It's similar to Cosmic Wimpout, but you use 6 regular dice. Aces are worth 100. Fives are worth 50. Three of a kind: three deuces = 200. Four deuces = 400. Five of them = 600. Six would be 1,200. Three sixes = 600. Three fives = 500. Three aces = 1,000. Game is 15,000. We've been playing it since then, my wife and I, at dinner or at breakfast, and the odds don't always work out the way I would expect them to. Thus, a good excuse to get back into some programming for fun. It's been almost a decade since I took a c++ intro course (for fun) and I was just looking for an excuse. The dice were it.

    I may even have something worthy of sharing when I get done--with any luck.

    Keith

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    The problem is that you aren't implementing your class, but rather defining it twice. One inside the DIE namespace and one outside.
    There are class tutorials on the site. I suggest you read them for better understanding on how to create and implement classes.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Quote Originally Posted by Bladeforger View Post
    LOL! One die plus one die equals two dice.

    FWIW, the result will be some stats about this silly little dice game some friends taught us after dinner a few weeks ago. It's similar to Cosmic Wimpout, but you use 6 regular dice. Aces are worth 100. Fives are worth 50. Three of a kind: three deuces = 200. Four deuces = 400. Five of them = 600. Six would be 1,200. Three sixes = 600. Three fives = 500. Three aces = 1,000. Game is 15,000. We've been playing it since then, my wife and I, at dinner or at breakfast, and the odds don't always work out the way I would expect them to. Thus, a good excuse to get back into some programming for fun. It's been almost a decade since I took a c++ intro course (for fun) and I was just looking for an excuse. The dice were it.

    I may even have something worthy of sharing when I get done--with any luck.

    Keith
    Aha I didn't know that one "die" could also be one dice. When I translate the swedish word for one "tärning" I get "dice", but if i look up the english word die there is also that translation.

    I know that game you're talking about, I used to play it a lot one summer with my friends. Do share if you get something working, dice games can be a good timepasser.
    home = hat.position;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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