Thread: Need a little help!!!

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    Need a little help!!!

    I am learning c++ and I am having a little trouble with this program. I converted one big program I created into classes and I am having trouble. I am new to classes. Can someone help. It will be greatly appreciated.

    main.cpp
    Code:
    const int NUM_REPAIR_BAYS = 4;
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <ctime>
    #include <queue>
    #include "vehicle.h"
    
    using namespace std;
    
    // return available vehicle
    Vehicle* vehicle_available(queue<Vehicle*>& q, queue<Vehicle*>& pq);
    
    // return repair bay availabe
    int repair_bay_available(Vehicle* repairBay[]);
    
    int main()
    {
      int i;
      queue<Vehicle*> q; // vehicle que
      queue<Vehicle*> pq; // priority queue
      Vehicle* repair_bay[NUM_REPAIR_BAYS]={0}; // repair bay
      vector<Vehicle*> salvage_yard; // salvage yard
      vector<Vehicle*> active_duty; // active duty
      Vehicle* v;
      int id = 0;
      
    srand((unsigned int)time(0));
      
    for(int t=0;t<5;t++)
      {
            // Create a number (0-4) new random vehicles per day
        int    n = rand() % 4;
    
            // As the new vehicles are created they are placed into a queue
            for(i=0;i<n;i++)
               {
               v = new Vehicle (id++);
               cout << "creating vehicle: " << endl;
               v->print();
               q.push(v);
               cout << endl;
               }
            
    // The members of the queue (#max/time) are then processed by a "yard manager
    // The front of the queue is popped and added to: Salvage Yard
    // or  Repair Bay if available Vector[4]
            
            v = vehicle_available(q,pq); // get available vehicle
            if(v != NULL)
            {
                cout << "processing vehicle: " << endl;
                v->print();
                cout << endl;
                i = repair_bay_available(repair_bay);
                if(i >= 0)
                {
                    v->print();
                    cout <<"was sent to repair bay #" << (i+1) << endl;
                    cout << endl;
                    repair_bay[i] = v;
                }
                else
                {
                    cout << "All repair bays filled, vehicle sent to salvage yard" << endl;
                    salvage_yard.push_back(v);
                }
    
    // When a vehicle in a bay is finished it is then run to testing, each component is tested,
    // if all pass the vehicle is returned to duty else returned to a priority queue which will pre-empt
    // the repair queue (fastest first)
                
    // for each repair bay
                for(i=0;i<NUM_REPAIR_BAYS;i++)
                {
                v = repair_bay[i];
                if(v != NULL)
                {
                if(v->passed_test()) // passed test?
                {
                   v->print();
                   cout << "passed repair test, vehicle sent to active duty" << endl;
                   cout << endl;
                   active_duty.push_back(v);
                   repair_bay[i]=0;
                }
                else  if(v->failed_test()) // failed test
                {
                   v->print();
                   cout << "failed repair test, vehicle sent to priority queue" << endl;
                   cout << endl;
                   pq.push(v);
                   repair_bay[i]=0;
                }
                }
                }
            }
      }
         cout << endl;
         
    // print out whats in queue
         cout << "Vehicles in left in Queue: " << endl;
         while(q.size() > 0)
         {
            print(q.front());
            q.pop();
         }
         cout << endl;
          
    // print out whats in priority queue
         cout << "Vehicles left in priority Queue: " << endl;
         while(pq.size() > 0)
         {
            print(pq.front());
            pq.pop();
         }
         cout << endl;
    
          
    // print out whats in storage bay
         cout << "Vehicles in Repair Bay: " << endl;
         for(i=0;i<NUM_REPAIR_BAYS;i++)
            if(repair_bay[i] != 0)
                print(repair_bay[i]);
         cout << endl;
         
    // print out whats in salvage yard
         cout << "Vehicles in Salvage Yard: " << endl;
         for(i=0;i<salvage_yard.size();i++)
            print(salvage_yard[i]);
         cout << endl;
          
    // print out whats in active duty
         cout << "Vehicles in Active Duty: " << endl;
         for(i=0;i<active_duty.size();i++)
            print(active_duty[i]);
         cout << endl;
    
            return 0;
    }
    
    //******************************************************************************
    
    // return vehicle avaiable
    Vehicle* vehicle_available(queue<Vehicle*>& q, queue<Vehicle*>& pq)
    {
        Vehicle* v=NULL;
        // check priority queue first for vehicles
            if(pq.size() > 0)
            {
                v = pq.front();
                pq.pop();
            }
            // check queue for vehicles
            else if(q.size() > 0)
            {
                v = q.front();
                q.pop();
            }
        return v;
    }
    
    //******************************************************************************
    
    // return repair bay availabLe
    int repair_bay_available(Vehicle* repair_bay[])
    {
        for(int i=0;i<NUM_REPAIR_BAYS;i++)
        {
            if(repair_bay[i] == NULL)
                return i;
        }
        return -1;
    }
    vehicle.cpp
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include "vehicle.h"
    
    using namespace std;
    
    void Vehicle::print()
    { cout << id << " " << Type << " ";
        if(Armour[0]) cout << "F";
        if(Armour[1]) cout << "S";
        if(Armour[2]) cout << "R";
        if(Armour[3]) cout << "P";
        cout << " ";
        cout << Weapons << " ";
        if(Transmission) cout << "Transmission";
        if(Differential) cout << "Differentisl";
        cout << Engine << endl;
    }
    
    Vehicle::Vehicle (int _id)
    { id = _id;
      int x = rand() % 4;
           // V1, V2, etc)
        Type =  "V1";
        if(x == 1)
          Type =  "V2";
       if(x == 2)
          Type =  "V3";
        if(x == 3)
          Type =  "V4";
    
       Armour[0] = true; // bool: F,S,R,P)
       Armour[1] = rand() % 2==0;
       Armour[2] = rand() % 2==0;
       Armour[3] = rand() % 2==0;
       x = rand() % 3 + 1;
       if(x == 1)
          Weapons  = "w1";  // (list of strings w1, w2, w3...etc)
       if(x == 2)
          Weapons = "w1, w2";
       if(x == 3)
          Weapons = "w1, w2, w3";
       if(x == 4)
          Weapons = "w1, w2, w3, w4";
        // Drive train
       Transmission = rand() * 2 == 0;
       Differential = rand() * 2 == 0;
       x = rand() % 3;
       if(x == 0)
          Engine = 'O';  // (O,N,D) O-operational N-No engine D-Damaged
       if(x == 1)
          Engine = 'N';
       if(x == 2)
          Engine = 'D';
    }
    
    // return true if vehicle pased test
    bool Vehicle::passed_test()
    {
         // 50 % vehicle pass
        if(rand()%100>50)
        {
            // set repaired
            Transmission=false;
            Differential=false;
            Engine='O';
            return true;
        }
        else return false;
    }
    
    // return false if vehicle pass test
    bool Vehicle::failed_test()
    {
         return rand()%100<20;
    }
    vehicle.h
    Code:
    using namespace std;
    
    // Vehicle info
    class Vehicle
    {
       int id; // -Id number
       string Type; // V1, V2, etc)
       bool Armour[4]; // bool: F,S,R,P)
       string Weapons; // (list of strings w1, w2, w3...etc)
       // Drive train
       bool Transmission;
       bool Differential;
       char Engine;  // (O,N,D) O-operational N-No engine D-Damaged
    
    public:
       Vehicle (int id);
       void print ();
       bool passed_test();
       bool failed_test();
    };
    error
    Code:
     In function 'int main()':
    error: 105: 'print' was not declared in this scope
    error: 114: 'print' was not declared in this scope
     error: 124: 'print' was not declared in this scope
    warning: 129: comparison between signed and unsigned integer expressions
    error: 130: 'print' was not declared in this scope
    warning: 135: comparison between signed and unsigned integer expressions
    error: 136: 'print' was not declared in this scope
    
    Last edited by binarydude87; 10-22-2012 at 05:22 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hello binarydude and welcome to the forum

    A class is a set of behavior and attributes that an object can have.We can create an instance of the class.Of course we can create more.Every instance has the behavior and the attributes the class have, but in normal situations object's A attribute is different will be different from object's B attribute (even though we assume that the came from the same class).
    Code:
    class A{
       int id;
    }
    class B{
        int id;
    }
    ...
    A instanceOfClassA = new A(); //default constructor will be called which takes no parameters
    B instanceOfClassB = new B();
    instanceOfClassA.id = 136;
    instanceOfClassB.id = 93;
    // notice that id of every instance is a different thing, a different block of memory!
    // now everything seems to be ok,as we say in which id we are referring to.
    id = 54; // id of what?Of what instance? (assume that you have not declare another 
    // variable with name id somewhere inside the code,but only inside the classes A and B
    // now we do not telling the compiler where this id belongs,so how can he know? :/
    // so...ERROR
    Take a look at your code and check the red lines.Try to make an analogy
    Code:
    // print out whats in storage bay
         cout << "Vehicles in Repair Bay: " << endl;
         for(i=0;i<NUM_REPAIR_BAYS;i++)
            if(repair_bay[i] != 0)
                print(repair_bay[i]);
         cout << endl;
          
    // print out whats in salvage yard
         cout << "Vehicles in Salvage Yard: " << endl;
         for(i=0;i<salvage_yard.size();i++)
            print(salvage_yard[i]);
         cout << endl;
           
    // print out whats in active duty
         cout << "Vehicles in Active Duty: " << endl;
         for(i=0;i<active_duty.size();i++)
            print(active_duty[i]);
         cout << endl;
    The compiler will search at the files included(libraries are files too) and in file which hosts main to find a prototype matching the function print.It won't because it is not a build-in function and you do not define somewhere.Well what you define is a member function inside a class.So you want to refer to an instance of the class in order to have access to instance's behavior,thus member functions! (public ones)

    As for the
    Code:
    warning: 129: comparison between signed and unsigned integer expressions
    i can not search your code because in chrome lines of code are not working ... :/ What it says is exactly what the compiler says.You compare a signed with an unsigned value!You should compare signed and signed OR unsigned and unsigned.
    Hope this helps.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Im still a little confused. The reason I am getting the print error messages is because I need to create another class to print it or is the parameters?
    Last edited by binarydude87; 10-22-2012 at 06:56 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by binarydude87 View Post
    Im still a little confused. The reason I am getting the print error messages is because I need to create another class to print it or is the parameters?
    I have no idea; but, a C programmer guess is this.
    Still working on learning C++, myself.

    I would guess this is wrong
    Code:
    print(repair_bay[i]);
    And, I would try this; might not work.

    Code:
    repair_bay[i].print();
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by binarydude87
    I converted one big program I created into classes and I am having trouble.
    stahta01 is right: you declared print to be a member function of the Vehicle class, therefore when you call print, it should use the member function call syntax, i.e.,
    Code:
    repair_bay[i].print();
    not the non-member function call syntax:
    Code:
    print(repair_bay[i]);
    The "comparison between signed and unsigned integer expressions" warning is because of expressions like "i<salvage_yard.size()". i is an int, which is a signed integer type, but size() returns a vector<Vehicle*>::size_type, which is an unsigned integer type (typically std::size_t, which itself is typically a typedef for unsigned int or unsigned long). The compiler is advising you that this is a potential bug (though it is not actually a bug in this case). One solution is to make i a std::size_t instead.

    Quote Originally Posted by std10093
    Code:
    A instanceOfClassA = new A(); //default constructor will be called which takes no parameters
    I note that that is not correct C++ syntax since new A() results in an A* not an A object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    Vehicle::Vehicle (int _id)
    {
       ...
       // Drive train
       Transmission = rand() * 2 == 0;
       Differential = rand() * 2 == 0;
       ...
    }
    Is that supposed to be % instead of *?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suggest you stop using pointers and new in your code. I see no need to use it. It complicates your design and makes it prone to errors.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    I figured it out guys. Thank you for your quick responses and help.

Popular pages Recent additions subscribe to a feed