so im writing this code, trying to do an exercise with friend functions and all that

I have that part down, I was told to take my original code and put in what you see now

"Make function add of type AltMoney. Thus, this function now computes the sum of dollars and cents and returns it as AltMoney. Note that in the above version of the program, you have passed the object sum as call_by_reference"

right so i get that...except now when i try to compile

Error 1 error C2248: 'AltMoney::cents' : cannot access private member declared in class 'AltMoney'

I get that 9 times, from lines 64-69
I'm about to mash my head on the keyboard, I'm just not getting why this is not working, can anyone help me out?

#include <stdafx.h>
#include <iostream> 
#include <cstdlib>

using namespace std; 

class AltMoney 
{ 
    public: 
        AltMoney(); 
        AltMoney(int d, int c); 
        friend AltMoney add(AltMoney m1, AltMoney m2, AltMoney& sum); 
        void display_money(); 	
		AltMoney read_money ();

    private: 
        int dollars; 
        int cents; 
}; 

AltMoney read_money(); 

int main( ) 
{ 
     int d, c; 
     AltMoney sum; 
     read_money(); 
     AltMoney m1(d, c);
     cout << "The first money is:";
     m1.display_money(); 
     read_money(); 
     AltMoney m2(d, c);
     cout << "The second money is:"; 
     m2.display_money(); 
     add(m1,m2, sum); 
     cout << "The sum is:"; 
     sum.display_money(); 

	 system ("PAUSE");
     return 0; 
} 
AltMoney::AltMoney() 
{ 
      dollars = 0;
      cents = 0;
} 
AltMoney::AltMoney(int d, int c) 
{ 
       dollars = d; 
       cents = c; 
} 
void AltMoney::display_money() 
{ 
     cout << "$" << dollars << "."; 
     if(cents <= 9) 
         cout << "0";  //to display a 0 in the left for numbers less than 10 
     cout << cents << endl; 
} 
AltMoney add(AltMoney m1, AltMoney m2) 
{ 
     int extra = 0; 
	 AltMoney sum;

     sum.cents = m1.cents + m2.cents; 
     if(sum.cents >=100){ 
         sum.cents = sum.cents - 100; 
         extra = 1; 
      } 
      sum.dollars = m1.dollars + m2.dollars + extra; 
	  return sum;
} 
AltMoney read_money()
{ 
	 int d, c;
     cout << "Enter dollar \n"; 
     cin >> d; 
     cout << "Enter cents \n"; 
     cin >> c; 
     if( d < 0 || c < 0) 
     { 
            cout << "Invalid dollars and cents, negative values\n"; 
            exit(1); 
      } 
}

You have a mismatch of the two declarations of add.
Change: friend AltMoney add(AltMoney m1, AltMoney m2, AltMoney& sum); to friend AltMoney add(AltMoney m1, AltMoney m2); And change: add(m1,m2, sum); to sum = add(m1,m2);

hmm that definitely solves that problem
i completely missed that, thanks much

only problem is now when I compile I get the lovely break or continue option, saying "d" and "c" variables aren't being initialized

initializing them in main to 0 wont work i dont know what it's talking about now as i have them set to equal my private members... -_-

You're missing "AltMoney::" in the definition of read_money; and it should be putting it's input into "dollars" and "cents" (after validation). Also change the return type to void (in definition and declaration).

// change definition of read_money to
void AltMoney::read_money()

// Also, find and delete this line (just above main):
AltMoney read_money();

// main would go like this (no need for d and c in main):
     AltMoney m1;      // create object
     m1.read_money();  // read value
     cout << "The first amount is:";
     m1.display_money();
     AltMoney m2;
     m2.read_money();
     cout << "The second amount is:";
     m2.display_money();
     AltMoney sum;
     sum = add(m1,m2);
     ...

that makes everything i type in = 0!!
oh lordy
i am getting so frustrated with this language lol

im sorry this took so long to reply...i could have sworn i replied...i dont know why it didn't get posted

at this point i just wanna know whats happening :'(

The error is definitely right. You are using uninitialized d and c.
So what to do?
Look at the defination of

AltMoney read_money()
{ 
[B]	 int d, c;[/B]
     cout << "Enter dollar \n"; 
     cin >> d; 
     cout << "Enter cents \n"; 
     cin >> c; 
    blah
blah
}

I think you suerly meant the global d and c right? The one you defined in main(). So just strike off this line . Doing so will cause the function to use the global d and c instead and hence you will be happy.

even doing that still just gives me 0
im about to jump out my window lol
i cant believe how much this problem is kicking my butt

would it be something with the default constructor?

i have midterm for this class tomorrow :'(

Oh man! its your fault. You have spoiled all over by writing definitions like AltMoney read_money() while they should be AltMoney::read_money() i.e It should be a member function of AltMoney.
Ok now here is what to do:
Rewrite read_money as a member function of AltMoney
and read the post by nucleon he has explained it all.
Also, in the read_money function, you should cin the dollar, and cent not d and c.
Getting me?

#include <stdafx.h>
#include <iostream> 
#include <cstdlib>

using namespace std; 

class AltMoney 
{ 
    public: 
        AltMoney(); 
        AltMoney(int d, int c); 
		friend AltMoney add(AltMoney m1, AltMoney m2);
        void display_money(); 	
		void AltMoney::read_money();

    private: 
        int dollars; 
        int cents; 
}; 

int main( ) 
{ 
	 int d;
	 int c;
	 AltMoney m1;
     m1.read_money();
     cout << "The first amount is:";
     m1.display_money();
     AltMoney m2;
     m2.read_money();
     cout << "The second amount is:";
     m2.display_money();
     AltMoney sum;
     sum = add(m1,m2);
     cout << "The sum is:"; 
     sum.display_money(); 

	 system ("PAUSE");
     return 0; 
}

AltMoney::AltMoney()
{ 
      dollars = 0;
      cents = 0;
} 

AltMoney::AltMoney(int d, int c) 
{ 
       dollars = d; 
       cents = c; 
}  

void AltMoney::display_money() 
{ 
     cout << "$" << dollars << "."; 
     if(cents <= 9) 
         cout << "0";  //to display a 0 in the left for numbers less than 10 
     cout << cents << endl; 
} 
AltMoney add(AltMoney m1, AltMoney m2) 
{ 
     int extra = 0; 
	 AltMoney sum;

     sum.cents = m1.cents + m2.cents; 
     if(sum.cents >=100){ 
         sum.cents = sum.cents - 100; 
         extra = 1; 
      } 
      sum.dollars = m1.dollars + m2.dollars + extra; 
	  return sum;
} 
void AltMoney::read_money()

{  
	 int dollars, cents;
     cout << "Enter dollar \n"; 
     cin >> dollars; 
     cout << "Enter cents \n"; 
     cin >> cents; 
     if( dollars < 0 || cents < 0) 
     { 
            cout << "Invalid dollars and cents, negative values\n"; 
            exit(1); 
      } 
}

thats what i have now, im sorry i didn't think to update what i had, i followed nucleons post, changing them to member functions, i get that, and i changed the d and c to dollar and cent but still...keep getting zero

its like no matter what numbers are entered they are set to 0 but for the life of me i dont get why

>its like no matter what numbers are entered they are set to 0 but for the life of me i dont get why
Look at the read_money() you still have declared the local dollars, cents; Delete them

void AltMoney::read_money()

{  
	[B] int dollars, cents;<<Delete this line[/B]
     cout << "Enter dollar \n";

Plus, the prototype of the function should be just void read_money. So just strike of the AltMoney:: before the defination

class AltMoney 
{ 
    public: 
        AltMoney(); 
        AltMoney(int d, int c); 
		friend AltMoney add(AltMoney m1, AltMoney m2);
        void display_money(); 	
		void AltMoney::read_money();//change it to [B]void read_money();[/B]

But the defination should remain with the AltMoney::read_money() { }

void AltMoney::read_money()

{  
	// int dollars, cents; deleted this line
     cout << "Enter dollar \n"; 
     cin >> dollars; 
blah
blah

ohhh my
it works
this must be a dream -_-
thank you so much ive been working on this for like 2 weeks i feel like such an idiot now

this whole exercise just messed me up i wanna go back to simple things like arrays!

thanks again so much

Please mark the thread as "Solved"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.