What's wrong with this code?

0 Loo Soo Yong · May 18, 2015
#include <iostream>
#include <cstdlib>
using namespace std;

class B
{
private:
int m;

protected:
int n;

public:
B(int m, int n)
{
this->m=m;
this->n=n;
}

void setM(int m)
{
this->m=m;
}

void setN(int n)
{
this->n=n;
}

int getM();
int getN();

int calc(int m, int n)
{
return m*n;
}
};


class D : public B
{
private:
double q;

protected:
double r;

public:
D(double q, double r){
this->q=q;
this->r=r;
}

void setQ(double q)
{
this->q=q;
}

void setR(double r)
{
this->r=r;
}

double getQ();
double getR();

double calc (double q, double r)
{
return q*r;
}
};

int main()
{
B num1(3, 4);
D num2(3.5, 4.7);
return 0;
}

I was asked to find the product of two numbers (a pair of int and a pair of doubles) but the trouble is whenever I compile the source code, Dev-C++ throws me this error:

C:\Users\Admin\Desktop\test 2\inheritance.cpp In constructor 'D::(double, double)':
49 24 C:\Users\Admin\Desktop\test 2\inheritance.cpp [Error] no matching function for call to 'B::B()'
49 24 C:\Users\Admin\Desktop\test 2\inheritance.cpp [Note] candidates are:
14 3 C:\Users\Admin\Desktop\test 2\inheritance.cpp [Note] B::B(int, int)
14 3 C:\Users\Admin\Desktop\test 2\inheritance.cpp [Note] candidate expects 2 arguments, 0 provided
5 7 C:\Users\Admin\Desktop\test 2\inheritance.cpp [Note] B::B(const B&)
5 7 C:\Users\Admin\Desktop\test 2\inheritance.cpp [Note] candidate expects 1 argument, 0 provided

Post a Reply

Replies

Oldest  Newest  Rating
0 sumeet prasad · May 18, 2015
hy friend
just to help u ,,you are not declaring the data type for the function class B
it might help u.just try
0 Loo Soo Yong · May 19, 2015
Mind elaborating a bit?
0 Michael Bradford · May 19, 2015
Class B is not a function, it's a class and is it's own data type.
From what I can see it should be valid code, I'm going to try using it in my IDE and see how it goes ill get back to you, are you SURE that what you have provided is exactly the code you tried to compile because the compiler seems to think your constructing with no arguments (no matching function for call to B::B()), what you've provided is a call to B::B(int, int). Just to be sure make sure you've saved. Get back to you in a bit.
0 Michael Bradford · May 19, 2015
So I cant believe I missed this, the reason your code has problems is because of inheritance. You see in C++ Inheritance can be described as "IS-A" inheritance what that means is a Derived Object "IS-A" Base Object with a little more specialization to it. 

When you construct a Derived Class it must first call some constructor for the Base class it inherits from, because remember Class D "IS-A" Class B so it must construct a Class B then move on to further construct a Class D. 

The problem in your case is when you construct Class D you have not specified what Class B constructor to use (I'm sure you've seen code without it specified and it's actually perfectly valid code to use) here's the problem, when you don't specify a Base Class constructor to use the compiler uses... you guessed it.. the DEFAULT CONSTRUCTOR. This is where we hit a snag with your code because no default constructor exists in either of your classes.

Hence the error code telling you that your trying to call B::B() <-- the default constructor but there is no match for a default constructor, because you haven't defined a default constructor.

There are two solutions to this problem and they are as follows.

1. Create ATLEAST a default constructor in your Base Class (B) something like
B()
{
  m = 10; // an arbitrary default value
  n = 10; //another arbitrary default value
}

OR

2. Specify inside your Derived Class Constructor what Base constructor you want to use like so.
D(double q, double r) : B(int(q), int(r))
{
   this->q=q;
   this->r=r;
}

Note the : syntax used in the Derived constructor is exactly like the : in the class line. Also all that I'm doing there is using the B constructor provided and type-casting q and r to ints (truncating the floating points) a call like D(double q, double r) : B(5, 6){}
would work just fine it just wouldn't be useful in that its basically arbitrary numbers that gets us the same result as using an arbitrary default constructor. 
(It's also worth noting I think it would be valid albeit not wise to make a default constructor for the Base class that did nothing... IE B(){}; )

also for the record you should use proper indentation, all the get functions are declared but not defined (since your not seperating your files and doing declaration and definition in the same file) they should be more like getM(){} or getM(){ std::cout << m << endl;} or int getM(){return m;} as they stand now they are prototypes with no definition. 
0 Loo Soo Yong · May 20, 2015
Now I actually used a default constructor in base class B, though the problem right now is how do I print the result out?
0 sumeet prasad · June 8, 2015
use cout statements
  • 1

C++

106,980 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator