0

constructor of base class

Business::Business(const string& nName,const string& nAddress) {    name
= nName;    address = nAddress; }

constructor that i am trying to use from derived class

Restaurant::Restaurant(const float& nRating,const string& nName,const string& nAddress)
        {
            Business();
            Business::setBusinessName(nName);
            Business::setBusinessAddress(nAddress);
            rating = nRating;
            
        }

from main I am trying to set the fields of the class from main using the above method which uses the constructor from restaurant class. I keep on getting error invalid use of 'Restaurant::Restaurant'

#include <iostream>
#include <string>
using namespace std;

#include "Restaurant.h"



int main()
{
    Restaurant m;
    
    m.Restaurant(2.55 , "s" , "224");

    
    cout << m.getRating() <<endl<< m.getBusinessAddress() <<endl<< m.getBusinessName();
    
    return 0;
}
  • The ratio of the usefulness of [the Member Initializer List](https://en.cppreference.com/w/cpp/language/constructor) vs the frequency at which it is taught borders on infinity. – user4581301 Oct 26 '20 at 17:03

1 Answers1

2

The placement of your base class construction off your derived class is wrong. It should look like this:

Restaurant::Restaurant(const float& nRating,const string& nName,const string& nAddress)
    : Business(nName, nAddress) // base construction
    , rating(nRating)           // member initialization
{
}

By the looks of it, your entire understanding of how constructors work is equally wrong. In main(), this:

Restaurant m;
m.Restaurant(2.55 , "s" , "224");

Should simply be:

Restaurant m(2.55 , "s" , "224");
WhozCraig
  • 65,258
  • 11
  • 75
  • 141