Thread: Conversion Constructor help

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

    Conversion Constructor help

    Hello, I am writing a class for a mixed number, and I have all of the operator overloads working and such when it comes to other mixed number objects. But My conversion constructor for if just an int is entered is not working. If someone could take a quick peak and see if anything jumps out, it would be much appreciated.

    Declaration:

    Code:
    Mixed(int w = 0);
    Definition:

    Code:
    Mixed::Mixed(int w){
    
    	whole = w;
    	numerator = 0;
    	denominator = 1;
    }

    These are the operations I am trying to perform and getting a type conversion error:

    Code:
    Mixed x;
    
      cout << "(x + 10) = " << x + 10 << '\n';
      cout << "(x - 4) = " << x - 4 << '\n';
      cout << "(x * -13) = " << x * -13 << '\n';
      cout << "(x / 7) = " << x / 7 << '\n';

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I'd post all of your code, for now we can probably only guess various problems with your implementation (i.e., did you properly overload the "<<" operator of your class). Its also a good idea to post the exact error message (not the "type" of error message).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by AvtrOfWoe View Post
    Hello, I am writing a class for a mixed number, and I have all of the operator overloads working and such when it comes to other mixed number objects. But My conversion constructor for if just an int is entered is not working. If someone could take a quick peak and see if anything jumps out, it would be much appreciated.

    Declaration:

    Code:
    Mixed(int w = 0);
    Definition:

    Code:
    Mixed::Mixed(int w){
    
    	whole = w;
    	numerator = 0;
    	denominator = 1;
    }

    These are the operations I am trying to perform and getting a type conversion error:

    Code:
    Mixed x;
    
      cout << "(x + 10) = " << x + 10 << '\n';
      cout << "(x - 4) = " << x - 4 << '\n';
      cout << "(x * -13) = " << x * -13 << '\n';
      cout << "(x / 7) = " << x / 7 << '\n';
    For those operations to invoke your constructor that takes an int, you'd have to have written operator overloads of +, -, *, and /. Perferably the two-parameter friend version so that both sides are implicitly convertible. I would also bracket the calculations so that the operator precedence is more explicit.
    You should also be using a "constructor initialisation list".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM