cppgameprogramming.com

It is currently Mon May 20, 2013 7:45 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Sep 17, 2012 4:54 am 
Offline
Contributer

Joined: Thu Jul 26, 2012 3:56 am
Posts: 20
*Just started learning classes/objects*
I am having trouble with my private bool arrays in my class flight.

Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

class Flight
{
private:
   bool f_seats[10];
   bool e_seats[10];

public:
   Flight();
   void f_reserve(int &s);
   void e_reserve(int &s);

};



int main()
{
   Flight greyhound;
   int chose = 0;
   
   cout << "Please choose section of preference: Press..  " << endl;
   cout << "1) First Class " << endl;
   cout << "2) Economy Class " << endl;
   cin >> chose;

   greyhound.f_reserve(chose);

   


   system("Pause");
  return 0;
}

Flight::Flight()
{
    bool f_seats[10] = {false, false, false, false, false, false, false, false, false, false};
    bool e_seats[10] = {false, false, false, false, false, false, false, false, false, false};

}

void Flight::f_reserve(int &s)
{
   cout << "Seat: " << f_seats[0] << endl; 

}




I am running a test thus far, but I'm not getting the result I want. In
Code:

void Flight::f_reserve(int &s)
{
   cout << "Seat: " << f_seats[0] << endl; 

}


The output should be 0, since the first element in the array is false. However, the output that I am getting is 204. I am confused as to how that is possible since f_seats[10] is a member function of that class. Should I place the bool f_seats in the Flight constructor parameter list? such as Flight::Flight(bool f_seat[10]).

or am I doing something else wrong.

Feel free to ask questions for clarification.


Top
 Profile  
 
PostPosted: Mon Sep 17, 2012 9:03 am 
Offline
5 Star Code Master
User avatar

Joined: Sat Jul 03, 2010 9:28 am
Posts: 767
Location: Belgium
You are re-declaring those variables in your constructor, thus making it variables local to the constructor, and leaving the original ones uninitiated. Try this:
Code:
Flight::Flight() {
    f_seats = {false,false, etc.};
    e_seats = {false, false, etc.};
};


Pro-tip: turn up the warning level of your compiler to the max. It'd warn you if you make this mistake again. If you're using gcc(on windows, this means you're using MinGW), you should add these parameters to the compiler settings: -Wall -Wextra

_________________
By reading this sig, I, the reader, agree to render my soul to J-Gamer. I, the reader, understand this is a legally binding contract and freely render my soul.
My website: J-Games
My DA


Top
 Profile  
 
PostPosted: Tue Sep 18, 2012 6:00 pm 
Offline
Contributer

Joined: Thu Jul 26, 2012 3:56 am
Posts: 20
I tried initializing the variables in the constructor

Code:
Flight::Flight()
{
     f_seats[10]= {false, false, false, false, false, false, false, false, false, false};
     e_seats[10] = {false, false, false, false, false, false, false, false, false, false};

}



I keep getting an error on the open curly brace { (Error: expected an expression)
I am getting this error from the output window:
'e_seats' : local variable is initialized but not referenced

should I have placed parameters(bool &f_seats, bool &e_seats) in the flight constructors?, to make it local to Flight::Flight()


Top
 Profile  
 
PostPosted: Wed Sep 19, 2012 9:10 am 
Offline
5 Star Code Master
User avatar

Joined: Sat Jul 03, 2010 9:28 am
Posts: 767
Location: Belgium
If you do it like that, you're trying to initialise the 11th element of both arrays(which doesn't exist). just use their names.

_________________
By reading this sig, I, the reader, agree to render my soul to J-Gamer. I, the reader, understand this is a legally binding contract and freely render my soul.
My website: J-Games
My DA


Top
 Profile  
 
PostPosted: Thu Sep 27, 2012 3:33 am 
Offline
Moderator
User avatar

Joined: Wed Feb 25, 2009 3:00 am
Posts: 987
Location: Argentina
That's illegal because f_seats and e_seats aren't initialized.
You can't index a pointer with the [] operator until you initialize it. This is because A[I] translates directly into *(A+I), the address of A plus I bytes. If A isn't initialized this operation is not possible.
To initialize it, use the way that jerre showed.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group