class help

This is a discussion on class help within the C++ Programming forums, part of the General Programming Boards category; I have some problems with a class in my program, I have a header file and a .cxx file which ...

  1. #1
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79

    class help

    I have some problems with a class in my program, I have a header file and a .cxx file which I can change that actually contain my class, and a .cxx file that has my main which I cannot touch. Here is my code, help if you can.
    Code:
    #include <iostream>
    #include "money.h"
    using namespace std;
    
    // this file should not change just because you change the money class
    
    int main()
    {
     money mine, yours(123.45);
    
      cout << "mine is " << mine << "\nYours is " << yours;
    
      cout << "\nEnter a double ";
      cin >> mine;
    
      cout << "\nEnter a double ";
      cin >> yours;
    
      cout << "mine is " << mine << "\nYours is " << yours;
    
      if ( mine < yours ) 
        cout << "\n\nmine " << mine << " is less than " << yours << " yours\n";
      else
        cout << "\n\nmine " << mine << " is >= to  " << yours << " yours\n";
        
    
      return (0);
    }
    Code:
    #include "money.h"
    
    money::money() : Dollars(0), Cents(0)
    {}
    
    money::money(long in) 
    {
      Dollars = (long) (in * 100);
    }
    money::money(int in1)
    {
      Cents = (int) (in1*1);
    }
    ostream& operator <<(ostream& p1, const money& p2)
    {
      p1 << "$" << (p2.Dollars / 100) << "." << (p2.Cents / 100) << "." << p2.Dollars % 100 << "." <<p2.Cents;
      return p1;
    } // overloaded  output
    
    istream& operator >>(istream& p1, money &p2)
    {
    
      long in;
      int in1;
    
      p1 >> in;
      p1 >> in1;
      p2.Dollars = (long) (in * 100);
      p2.Cents = (long) (in1*.1);
      return p1;
    } // overloaded input
    
    bool operator <(money p1, money p2)
    {
      return( (p1.Dollars + p1.Cents) < (p2.Dollars + p2.Cents) );
    }
    Code:
    #ifndef MONEYH
    #define MONEYH
    #include <iostream>
    using namespace std;
    
    class money
    {
      public:
       money();
       money(long in, int in1);
       friend ostream& operator <<(ostream &p1, const money& p2);
       friend istream& operator >>(istream& p1, money &p2);
       friend bool operator <(money p1, money p2); // true p1 < p2
     private:
       long Dollars;
       int Cents;
    }; // class money
    
    #endif // MONEYH
    This has been a public service announcement from GOD.

    111 1111

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    Could use please include in your request the specific problem(s) you are having?

  3. #3
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    g++ -Wall -c money.cxx
    money.cxx:12: prototype for `money::money(long int)' does not match any in
    class `money'
    money.h:12: candidates are: money::money(const money&)
    money.h:15: money::money(long int, int)
    money.cxx:8: money::money()
    money.cxx:16: prototype for `money::money(int)' does not match any in class `
    money'
    money.cxx:12: candidates are: money::money(long int)
    money.h:12: money::money(const money&)
    money.h:15: money::money(long int, int)
    money.cxx:8: money::money()
    make: *** [money.o] Error 1
    This has been a public service announcement from GOD.

    111 1111

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    Code:
    // These are the definitions you wrote:
    money::money() : Dollars(0), Cents(0)
    {}
    money::money(long in) 
    {
      Dollars = (long) (in * 100);
    }
    money::money(int in1)
    {
      Cents = (int) (in1*1);
    }
    
    // These are the constructors in the .h file
    money();
    money(long in, int in1);
    The compiler tells you exactly whats wrong. You dont have any constructor that match the constructor definitions you used.

  5. #5
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    I know this makes me look really stupid, but what would that look like?
    This has been a public service announcement from GOD.

    111 1111

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    Do this:
    Code:
    // Replace these two
    money::money(long in) 
    {
      Dollars = in * 100;  // Cast is not necessary
    }
    money::money(int in1)
    {
      Cents = in1;  // Cast and multiplikation with 1 is not necessary
    }
    
    // with this:
    money::money(long in, int in1) 
    {
      Dollars = in * 100;  // No need to perform a cast there
      Cents = in1;  // Same as above and multiplikation with 1 is not necessary
    }
    I strongly suggest you read up some more about classes.

  7. #7
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    ok, I have done what was posted, but it didn't help, I am using a makefile, could this code be wrong?
    Code:
    lab1main : money.o lab1main.o
    	g++ money.o lab1main.o -Wall -o lab1main
    
    money.o : money.h money.cxx
    	g++ -Wall -c money.cxx
    
    lab1main.o : lab1main.cxx money.h
    	g++ -Wall -c lab1main.cxx
    
    clean :
    	rm -f *.o lab1main
    
    all: clean lab1main
    Other than this, I honestly do not know what is wrong.
    This has been a public service announcement from GOD.

    111 1111

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Doing what Shakti said should fix some of your errors. Then post some new code and the errors you're getting.

  9. #9
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    my code is exactly as it is above, with his corrections. my errors are these

    g++ -Wall -c lab1main.cxx
    lab1main.cxx: In function `int main()':
    lab1main.cxx:9: no matching function for call to `money::money(double)'
    money.h:12: candidates are: money::money(const money&)
    money.h:15: money::money(long int, int)
    money.h:14: money::money()
    make: *** [lab1main.o] Error 1
    This has been a public service announcement from GOD.

    111 1111

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > money mine, yours(123.45);

    Are you sure this isn't supposed to be:
    money mine, yours(123, 45);

    Because if this is right, you must change your class definition in money.h, and add a new money constructor like this:

    money(double amount);

    Then the code for this constuctor would have to take amount, and split it into Dollars and Cents.

  11. #11
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    ok, then I got this error

    g++ -Wall -c money.cxx
    g++ -Wall -c lab1main.cxx
    g++ money.o lab1main.o -Wall -o lab1main
    lab1main.o(.text+0x31): In function `main':
    : undefined reference to `money::money[in-charge](double)'
    collect2: ld returned 1 exit status
    make: *** [lab1main] Error 1
    This has been a public service announcement from GOD.

    111 1111

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Now you need to add function money(double amount) to your money.cxx file, with the actual function body:
    Code:
    money::money(double amount) 
    {
    // Add code here to split amount into Dollars and Cents
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21