2 questions - classes and variables

This is a discussion on 2 questions - classes and variables within the C++ Programming forums, part of the General Programming Boards category; 2 Questions -> 1 How do I change a float into an int without getting a warning? 2 I have ...

  1. #1
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    Question 2 questions - classes and variables

    2 Questions ->

    1 How do I change a float into an int without getting a warning?

    2 I have a large project and have split it into about 14 files, how do I get a class variable to be read in another part of the program?

    eg

    "player.h"
    class
    {
    char name[10];
    int age;
    player (name, age);
    }

    "make_team.h"
    player player01(James, 21);
    cout<< player01.age; // This line works correctly

    "main.cpp"
    {
    cout<<player01.age; // This line doesn't work, I can't get the class to read variables made in another program
    }

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    float num = 3.14159265f;
    int integer = (int) num; // explicit cast from float to int

    As for global variables, you can use extern.

    extern player player01; // can now be accessed from any file

    Avoid externing as much as possible. Use arrays/vectors to store lists of players, it's much more elegant.

  3. #3
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    thanks for the quick reply

    I was hoping I could get an answer within 48 hours so I am very happy to get it this quick

  4. #4
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    The float to int has worked fine, no problems there.

    I have now changed my class variables to an array, but still have the problem where I initialise the array but I can't read it form another part of the program. I tried using extern but I get the feeling I didn't initialise the variable in the correct place.

    "player.h"
    class player
    {
    int stats[1];
    }

    "make_team.h"
    player player01;
    player01.stats[0]=0;
    player01.stats[1]=1; // This line works correctly

    "main.cpp"
    {
    cout<<player01.stats[0]; // This line doesn't work, I can't get the class to read variables made in 'make_team.h'
    }

  5. #5
    Unregistered
    Guest
    Originally posted by phantom
    The float to int has worked fine, no problems there.

    I have now changed my class variables to an array, but still have the problem where I initialise the array but I can't read it form another part of the program. I tried using extern but I get the feeling I didn't initialise the variable in the correct place.

    "player.h"
    class player
    {
    int stats[1];
    }

    "make_team.h"
    player player01;
    player01.stats[0]=0;
    player01.stats[1]=1; // This line works correctly

    "main.cpp"
    {
    cout<<player01.stats[0]; // This line doesn't work, I can't get the class to read variables made in 'make_team.h'
    }
    Ok, if you can't read a class data mamber, did you:

    1) #include the header file which defines the class, and the class's members, in every .CPP file that requires it?
    2) make the member public?

  6. #6
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    >1) #include the header file which defines the class, and the class's members, in every .CPP file that requires it?

    At the moment I am only running one CPP file which is called 'main.cpp', all my other files are header files. The order of the header files is correct in that the class is declared first, then variables are declared but I still can't read them in another header.

    >2) make the member public?
    At the moment everything is public. When I start optimizing then I'll introduce private members.
    My site to register for all my other websites!
    'Clifton Bazaar'

  7. #7
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    After working on the problem a while longer I wonder if I have declared my class at the correct place? I try to declare it in int main() but it doesn't work
    My site to register for all my other websites!
    'Clifton Bazaar'

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    classes are global. Do not define them inside a function. Give the class declaration its own .h file and put the class implementation in a .cpp file..... Include the .h file in all other files that use that class.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    thank you, thank you, thank you ....

    I first got this problem 10pm friday night and it is now 1pm wednesday and all I have done in that time is try and figure this problem out. I was in fact trying to declare a class in a function and when that didn't work I tried to declare it in ANOTHER function!
    Does this make me eligable for 'dumbass of the year' award?

    Stoned_coder, I thank you again

    James

    PS I have drunk 3 litres of rum while trying to work this problem out!
    My site to register for all my other websites!
    'Clifton Bazaar'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes as member variables
    By Stonehambey in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2008, 08:01 PM
  2. Providing access to member variables between classes
    By Tonto in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2006, 02:06 PM
  3. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 11:06 AM
  4. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 06:49 AM
  5. Classes, dynamic variables, and leaks.
    By Grins2Pain in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2003, 04:07 PM

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