Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,174 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,350 people online right now. Registration is fast and FREE... Join Now!




Problem with class's

 
Reply to this topicStart new topic

Problem with class's, error

Renzokusen
31 May, 2008 - 06:13 PM
Post #1

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 32

I am doing a lab for a class which in we just started learning classes and I cannot figure out for the life of me why I am recieving this error.

CODE

#include <iostream>
using namespace std;

class plasmaPistolClass
{
private:  
        int ammo;                //  an integer value indicating the number of plasma bolts left in the plasma pistol
        int rateOfFire;          // an integer value indicating the number of bolts fired with one trigger pull. The max value is 10 the minimum value is 1. Ensure that the minimum and max values are not exceeded.
        int destructivePower;    // an integer from 1 to 10 indicating the destructive effect of the weapon. Ensure that the minimum and max values are not exceeded.

public:  
        bool safetyOn; //  a boolean value indicating if the pistols safety feature is activated
        int maxAmmo; //  an integer value indicating the maximum number of ammo bolts the plasma pistol can hold


//The button class should include these public methods:  
void pressTrigger(void);                       // fires the plasma pistol and decreases the ammo count based on the rateOfFire property. The pistol should not fire if the safetyOn property is true.
void load(int nmbrOfBolts);                  // adds ammo bolts to the plasma pistol.  The amount of ammo bolts in the plasma pistol can not exceed the maxAmmo property  
void setDestructivePower(int powerSetting);        //changes the destructive power of the plasma pistol  
int showDestructivePower(void);                        //returns the value of the destructivePower property.  Ensures that the max and minimum values are not exceeded.  
void setRateOfFire(int boltsPerTriggerPress);           //changes the number of plasma bolts fired by the plasma pistol. Ensures that the max and minimum values are not exceeded.  
int showRateOfFire(void);                                //returns the value of the rateOfFire property  
int ammoRemaining(void);                       //displays the amount of ammo remaining in the pistol  


//The plasma pistol class should have two constructors:
      //A default constructor that assigns default values to both the private and public properties of the object.
      plasmaPistolClass();  
      //An overload constructor that allows the destructive power and maxAmmo properties to be set when the object is instantiated.
      plasmaPistolClass(int,int);
};  

int main()
{
    plasmaPistolClass oldPistol();
    plasmaPistolClass newPistol(7,75);
    cout << "Firing Old pistol" << oldPistol.pressTrigger() << endl;

    system("pause");    
    return 0;
}

plasmaPistolClass::plasmaPistolClass()
{
  int ammo = 25;              
  int rateOfFire = 10;        
  int destructivePower = 5;
  bool safetyOn = true;
  int maxAmmo = 50;

}

plasmaPistolClass::plasmaPistolClass(int destructPower, int maxBolts)
{
int ammo = 25;                                        
int rateOfFire = 3;
int maxAmmo = maxBolts;
int destructivePower = destructPower;
bool safetyOn = false;
}

void plasmaPistolClass::pressTrigger(void)
{
if (safetyOn != true)
    {
     if (rateOfFire <= ammo)
        {
          cout << "Pistol Fired";
          ammo -= rateOfFire;
        }
     else
          {cout << "Pistol doesn't have enough ammo to fire";}
     }
else
     {cout << "Pistol's saftey is on";}
}

void plasmaPistolClass::setDestructivePower(int powerSetting)
{
     if (powerSetting >= 1 && powerSetting <= 10)
        {destructivePower = powerSetting;}
     else
        {cout << "Cannot set the plasma pistol to that power setting";}
}

int plasmaPistolClass::showDestructivePower(void)
{
    if (destructivePower >= 1 && destructivePower <= 10)
        {return destructivePower;}
}

void plasmaPistolClass::setRateOfFire(int boltsPerTriggerPress)
{
     if (boltsPerTriggerPress >= 1 && boltsPerTriggerPress <= 10)
     {rateOfFire = boltsPerTriggerPress;}
     else
     {cout << "Cannot set rate of fire to that setting";}
}  

int plasmaPistolClass::showRateOfFire(void)
{
    return rateOfFire;
}

int plasmaPistolClass::ammoRemaining(void)
{
    return ammo;
}



Error giving when compiling is line 37 request for member `pressTrigger' in `oldPistol', which is of non-class type `plasmaPistolClass ()()'
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Problem With Class's
31 May, 2008 - 06:25 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,236



Thanked: 221 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Small adjustment here...

cpp

int main()
{

// Create your two classes
plasmaPistolClass oldPistol;
plasmaPistolClass newPistol(7,75);

cout << "Firing Old pistol ";

// Can't include this function directly in a cout statement, its void.
// Its not returning anything to print.
oldPistol.pressTrigger();
cout << endl;


system("pause");
return 0;
}


You create the two classes, but you can't include pressTrigger as part of cout, it is not returning anything to be printed. Here we call the cout with our string, then close it off, call our function which itself prints using cout and lastly print the endl.

smile.gif
User is online!Profile CardPM
+Quote Post

Renzokusen
RE: Problem With Class's
31 May, 2008 - 08:54 PM
Post #3

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 32

Oh I see now. Only if the function has a return can it be included in a cout statement. Thanks
User is offlineProfile CardPM
+Quote Post

Renzokusen
RE: Problem With Class's
1 Jun, 2008 - 08:53 AM
Post #4

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 32

Yet another problem with this program. Man I cannot seem to get a grip on these classes. Now my problem is I just wish to set the destructive power of pistol2 to 5 and display it, but it just displays garbage.

CODE

#include <iostream>
using namespace std;

class plasmaPistolClass
{
private:  
        int ammo;                //  an integer value indicating the number of plasma bolts left in the plasma pistol
        int rateOfFire;          // an integer value indicating the number of bolts fired with one trigger pull. The max value is 10 the minimum value is 1. Ensure that the minimum and max values are not exceeded.
        int destructivePower;    // an integer from 1 to 10 indicating the destructive effect of the weapon. Ensure that the minimum and max values are not exceeded.

public:  
        bool safetyOn; //  a boolean value indicating if the pistols safety feature is activated
        int maxAmmo; //  an integer value indicating the maximum number of ammo bolts the plasma pistol can hold
        
//The plasma pistol class should have two constructors:
      //A default constructor that assigns default values to both the private and public properties of the object.
      plasmaPistolClass();  
      //An overload constructor that allows the destructive power and maxAmmo properties to be set when the object is instantiated.
      plasmaPistolClass(int,int);

//The button class should include these public methods:  
void pressTrigger(void);                       // fires the plasma pistol and decreases the ammo count based on the rateOfFire property. The pistol should not fire if the safetyOn property is true.
void load(int nmbrOfBolts);                  // adds ammo bolts to the plasma pistol.  The amount of ammo bolts in the plasma pistol can not exceed the maxAmmo property  
void setDestructivePower(int powerSetting);        //changes the destructive power of the plasma pistol  
int showDestructivePower(void);                        //returns the value of the destructivePower property.  Ensures that the max and minimum values are not exceeded.  
void setRateOfFire(int boltsPerTriggerPress);           //changes the number of plasma bolts fired by the plasma pistol. Ensures that the max and minimum values are not exceeded.  
int showRateOfFire(void);                                //returns the value of the rateOfFire property  
int ammoRemaining(void);                       //displays the amount of ammo remaining in the pistol  



};  

int main()
{
    plasmaPistolClass pistol1();  
    plasmaPistolClass pistol2(5,20);
    
    cout << "Destructive Power : " << pistol2.showDestructivePower() << endl;
  
    cout << endl;  
    system("pause");    
    return 0;
}

plasmaPistolClass::plasmaPistolClass()
{
  int maxAmmo = 50;

}

plasmaPistolClass::plasmaPistolClass(int destructPower, int maxBolts)
{
int ammo;                                        
int rateOfFire;
int maxAmmo = maxBolts;
int destructivePower = destructPower;
bool safetyOn = false;
}

void plasmaPistolClass::pressTrigger(void)
{
if (safetyOn != true && rateOfFire <= ammo)
    {        
          cout << "Pistol Fired";
          ammo -= rateOfFire;
    }
else
     {cout << "pistol couldn't fire";}
    

}

void plasmaPistolClass::setDestructivePower(int powerSetting)
{
     if (powerSetting >= 1 && powerSetting <= 10)
        {destructivePower = powerSetting;}
     else
        {cout << "Cannot set the plasma pistol to that power setting";}
}

int plasmaPistolClass::showDestructivePower(void)
{
    
    if (destructivePower >= 1 && destructivePower <= 10)
        {return destructivePower;}
}

void plasmaPistolClass::setRateOfFire(int boltsPerTriggerPress)
{
     if (boltsPerTriggerPress >= 1 && boltsPerTriggerPress <= 10)
     {rateOfFire = boltsPerTriggerPress;}
     else
     {cout << "Cannot set rate of fire to that setting";}
}  

int plasmaPistolClass::showRateOfFire(void)
{
    return rateOfFire;
}

int plasmaPistolClass::ammoRemaining(void)
{
    return ammo;
}


This post has been edited by Renzokusen: 1 Jun, 2008 - 08:54 AM
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Problem With Class's
1 Jun, 2008 - 09:03 AM
Post #5

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
I suggest you consider looking into the this-> pointer. It basically points to the object itself, and I've changed your constructor to use it:
cpp
plasmaPistolClass::plasmaPistolClass(int destructPower, int maxBolts)
{
this->ammo;
this->rateOfFire;
this->maxAmmo = maxBolts;
this->destructivePower = destructPower;
this->safetyOn = false;
}

Here is the complete code:
cpp
#include <iostream>
using namespace std;

class plasmaPistolClass
{
private:
int ammo; // an integer value indicating the number of plasma bolts left in the plasma pistol
int rateOfFire; // an integer value indicating the number of bolts fired with one trigger pull. The max value is 10 the minimum value is 1. Ensure that the minimum and max values are not exceeded.
int destructivePower; // an integer from 1 to 10 indicating the destructive effect of the weapon. Ensure that the minimum and max values are not exceeded.

public:
bool safetyOn; // a boolean value indicating if the pistols safety feature is activated
int maxAmmo; // an integer value indicating the maximum number of ammo bolts the plasma pistol can hold

//The plasma pistol class should have two constructors:
//A default constructor that assigns default values to both the private and public properties of the object.
plasmaPistolClass();
//An overload constructor that allows the destructive power and maxAmmo properties to be set when the object is instantiated.
plasmaPistolClass(int,int);

//The button class should include these public methods:
void pressTrigger(void); // fires the plasma pistol and decreases the ammo count based on the rateOfFire property. The pistol should not fire if the safetyOn property is true.
void load(int nmbrOfBolts); // adds ammo bolts to the plasma pistol. The amount of ammo bolts in the plasma pistol can not exceed the maxAmmo property
void setDestructivePower(int powerSetting); //changes the destructive power of the plasma pistol
int showDestructivePower(void); //returns the value of the destructivePower property. Ensures that the max and minimum values are not exceeded.
void setRateOfFire(int boltsPerTriggerPress); //changes the number of plasma bolts fired by the plasma pistol. Ensures that the max and minimum values are not exceeded.
int showRateOfFire(void); //returns the value of the rateOfFire property
int ammoRemaining(void); //displays the amount of ammo remaining in the pistol



};

int main()
{
plasmaPistolClass pistol1();
plasmaPistolClass pistol2(5,20);

cout << "Destructive Power : " << pistol2.showDestructivePower() << endl;

cout << endl;
system("pause");
return 0;
}

plasmaPistolClass::plasmaPistolClass()
{
int maxAmmo = 50;

}

plasmaPistolClass::plasmaPistolClass(int destructPower, int maxBolts)
{
this->ammo;
this->rateOfFire;
this->maxAmmo = maxBolts;
this->destructivePower = destructPower;
this->safetyOn = false;
}

void plasmaPistolClass::pressTrigger(void)
{
if (safetyOn != true && rateOfFire <= ammo)
{
cout << "Pistol Fired";
ammo -= rateOfFire;
}
else
{cout << "pistol couldn't fire";}


}

void plasmaPistolClass::setDestructivePower(int powerSetting)
{
if (powerSetting >= 1 && powerSetting <= 10)
{destructivePower = powerSetting;}
else
{cout << "Cannot set the plasma pistol to that power setting";}
}

int plasmaPistolClass::showDestructivePower(void)
{

if (destructivePower >= 1 && destructivePower <= 10)
{return destructivePower;}
}

void plasmaPistolClass::setRateOfFire(int boltsPerTriggerPress)
{
if (boltsPerTriggerPress >= 1 && boltsPerTriggerPress <= 10)
{rateOfFire = boltsPerTriggerPress;}
else
{cout << "Cannot set rate of fire to that setting";}
}

int plasmaPistolClass::showRateOfFire(void)
{
return rateOfFire;
}

int plasmaPistolClass::ammoRemaining(void)
{
return ammo;
}

Hope this helps smile.gif

This post has been edited by gabehabe: 1 Jun, 2008 - 09:03 AM
User is offlineProfile CardPM
+Quote Post

Renzokusen
RE: Problem With Class's
1 Jun, 2008 - 09:43 AM
Post #6

New D.I.C Head
*

Joined: 11 Mar, 2008
Posts: 32

works like a dream. Since my teacher hasn't taught us about these yet I'll have to look into them, because they make my program work tongue.gif. Thanks
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 10:32AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month