Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

What's wrong with my constructor?

Think you can spy out that little nasty bug? Try debugging some code in here

What's wrong with my constructor?

Postby jeezcak3 on Tue Aug 04, 2009 12:54 pm

error C2065: 'energy' : undeclared identifier
error C2065: 'money' : undeclared identifier
These errors are for the constructor. Obviously, I can't assign variables to private member unless I want them to be constant, right? So what to do?



Code: Select all
class Loser
{
public:

   Loser(int startEnergy, int startMoney):
      energy(startEnergy),
      money(startMoney)
      {
     
      };

private:
   int energy;
   int money;
};

int main()
{
   Loser Thang(90, 75);
   Thang.drinkCafe(5,5);
   return 0;
}

   void drinkCafe(int negEnergy, int negMoney)
   {
   energy -= negEnergy;
   money -= negMoney;
   }
jeezcak3
 
Posts: 8
Joined: Thu Jul 16, 2009 3:06 pm

Re: What's wrong with my constructor?

Postby jeezcak3 on Wed Aug 05, 2009 4:44 pm

could somenoe answeR!!???
jeezcak3
 
Posts: 8
Joined: Thu Jul 16, 2009 3:06 pm

Re: What's wrong with my constructor?

Postby C++ on Sun Aug 09, 2009 2:38 pm

jeezcak3 wrote:error C2065: 'energy' : undeclared identifier
error C2065: 'money' : undeclared identifier


That's because drinkCafe is not within the scope of class Loser. You either need to inline the function in the class or create a prototype (forward declaration) in the class and append the class name and :: before drinkCafe when defining it.


jeezcak3 wrote:These errors are for the constructor.


No, function drinkCafe does not know of energy and money because drinkCafe is not in the scope of Loser.


jeezcak3 wrote:Obviously, I can't assign variables to private member unless I want them to be constant, right? So what to do?


Your constructor is fine. You can't assign to private members directly even if they were constant. Constant members need to be initialized through the constructor's initialization list, just like you're doing right now.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: What's wrong with my constructor?

Postby jeezcak3 on Mon Aug 10, 2009 1:52 pm

my bad. i accidently removed class method drinkcafe there.
jeezcak3
 
Posts: 8
Joined: Thu Jul 16, 2009 3:06 pm

Re: What's wrong with my constructor?

Postby jeezcak3 on Mon Aug 10, 2009 1:56 pm

even so, it still doesn't know my money and energy

Code: Select all
#include "stdafx.h"
#include <iostream>

using namespace std;

class Loser
{
public:

   Loser(int startEnergy, int startMoney):
      energy(startEnergy),
      money(startMoney)
      {

      };

   void drinkCafe(int negEnergy, int negMoney);

private:
   int energy;
   int money;
};

int main()
{
   Loser Thang(90, 75);
   Thang.drinkCafe(5,5);
   return 0;
}

   void drinkCafe(int negEnergy, int negMoney)
   {
   cout << energy;
   cout << money;
   }
jeezcak3
 
Posts: 8
Joined: Thu Jul 16, 2009 3:06 pm

Re: What's wrong with my constructor?

Postby C++ on Mon Aug 10, 2009 3:28 pm

jeezcak3 wrote:even so, it still doesn't know my money and energy


That's correct. You made a prototype for the drinkCafe method in your class but you never define it.
All you did is define a function called drinkCafe.
You need to let the compiler know that you're defining the prototype in class Loser.

Code: Select all
   void Loser::drinkCafe(int negEnergy, int negMoney)
   {
   cout << energy;
   cout << money;
   }
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: What's wrong with my constructor?

Postby jeezcak3 on Mon Aug 10, 2009 5:26 pm

thanks! i figured out.
jeezcak3
 
Posts: 8
Joined: Thu Jul 16, 2009 3:06 pm


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests