no matching function call to...

Firtly, I would like to mention that I have searched through similar fourm topics with questions similar to mine but I have still not found a solution.

My problem ios this, my program is not compiling and I'm getting this error message...

error: no matching function call to 'main_XavierLinn::fraction::fraction(int)'

I have two constructors one defualt to assign values to member variables if no arguments are passed and one to assign values if arguments are passed.

//CLIENT CODE....
#include <iostream>
#include "fraction.h"
#include <fstream>
using namespace std;
using namespace main_XavierLinn;

void BasicTest();



int main()
{
BasicTest();
}


void BasicTest()
{
cout << "\n----- Testing basic fraction creation & printing\n";
cout << "(fractions should be in reduced form, and as mixed numbers.)\n";

const fraction fr[] = {fraction(4, 8), fraction(-15,21),
fraction(10), fraction(12, -3),
fraction(), fraction(28, 6), fraction(0, 12)};

for (int i = 0; i < 7; i++){
cout << "fraction [" << i <<"] = " << fr[i].numerator << endl;
}
}

// FILE: fraction.h
// CLASS PROVIDED: fraction (part of the namespace main_XavierLinn
//
// CONSTRUCTOR for fraction class:
// fraction(int initial_numerator = 0, initial_denominator = 1)
// Postcondition: The fraction has been intialized to zero

#ifndef FRACTION_H
#define FRACTION_H
namespace main_XavierLinn
{
class fraction
{
public:
// DEFAULT CONSTRUCTOR
fraction();
// CONSTRUCTOR
fraction(int initial_numerator, int initial_denominator);


int numerator; // numerator of fraction
int denominator; // denominator of fraction
};
}
#endif

//IMPLEMENTATION FILE
// FILE: fraction.cpp
// CLASS IMPLEMENTED: fraction (see fraction.h for documentation)

#include "fraction.h"
#include <iostream>
using namespace std;

namespace main_XavierLinn
{
fraction::fraction()
{ // Default constructor sets fraction to zero
numerator = 0;
denominator = 1;
}

fractoin::fraction(int initial_numerator = 0, int initial_denominator = 0)
{ // Constructor accepts arguments and sets them to member variables
numerator = initial_numerator;
denominator = initial_denominator;
}

}

Thanks for any help in advance!
When you initialize your array, you have one fraction(10). I am assuming you forgot to pass a second parameter.
No, if only one parameter is passed then a second one will be set to a defualt.

Oh, I see now. I didn't notice you put defaults in your source file. Put the default values in the prototypes in your header file instead.
The problem is that the main file sees only the definition of class fraction where the constructor with two parameters has no default arguments. So the compiler reports the error because it did not find matching constructor. You should declare the second constructor with two parameters as having a default argument for the second parameter and function specifier explicit. For example


class fraction
{
public:
// DEFAULT CONSTRUCTOR
fraction();
// CONSTRUCTOR
explicit fraction(int initial_numerator, int initial_denominator = 0 );


int numerator; // numerator of fraction
int denominator; // denominator of fraction
};
Topic archived. No new replies allowed.