0

When I try the two forms of constructor in classes of derivation hierarchy, the result turns out to be different. Could anybody tell me why? Below are the test code.

//Person.h

#ifndef PERSON_H_
#define PERSON_H_
#include<string>

using std::string;

class Person{
    private:
        string firstname;
        string lastname;
    public:
        Person(const char *fn="NoName", const char *ln="NoName");   //A
        Person(const string &fn, const string &ln);
        virtual ~Person(){}

};

class Gunslinger:virtual public Person{
    private:
        int notchnum;       
    public:
        Gunslinger(const char*f="unknown",const char*n="unknown",int not=0);//B
        virtual ~Gunslinger(){}
};

class PokerPlayer:virtual public Person{

    public:
        PokerPlayer(const char*fn="unknown", const char*ln="unknown");//C;
        virtual ~PokerPlayer(){}
};    

class BadDude:public Gunslinger,public PokerPlayer{
    public:
        BadDude(const char*fn="unknown", const char*ln="unknown", int notc=0);//D

};

#endif

//PersonDefinition.cpp

#include"Person.h"
#include<iostream>
#include<cstdlib>

using std::cout;
using std::endl;
using std::cin;

Person::Person(const char*fn, const char*ln):firstname(fn),lastname(ln){
}

Person::Person(const string &fn,const string &ln):firstname(fn),lastname(ln){

}

Gunslinger::Gunslinger(const char*fn,const char*ln, int not):Person(fn,ln),notchnum(not){
}

PokerPlayer::PokerPlayer(const char*fn,const char*ln):Person(fn,ln){
}

BadDude::BadDude(const char*fn, const char*ln, int notc):Person(fn,ln),PokerPlayer(fn, ln),Gunslinger(fn,ln,notc){

}

//PersonTest.cpp

#include<iostream>
#include "Person.h"

int main(){
    Person a("Jack","Husain");
    PokerPlayer b("Johnson","William",8);
    Gunslinger c("Mensly","Sim");

}

So, here is the problem. The program above fails to compile with the default constructor with default value for all argument and throws an error message saying that "expected ',' or '...' before '!' token", but if I replace the default constructor in Line A,B,C,D with the form without argument, the program compiles and run successfully.Could anyone tell me why? Below is the error message.

error message

1 Answer 1

1

You did not implement all the constructors. For instance, you declared a constructor PokerPlayer::PokerPlayer(char*, char*) but you are trying to create a PokerPlayer with PokerPlayer b("Johnson","William",8); (i.e. you never declared a constructor which takes the third argument). The declaration you want for that previous line to work is PokerPlayer::PokerPlayer(char*, char*, int);

Furthermore, you have the exact opposite problem when trying to declare a GunSlinger. Your GunSlinger class requires the third parameter and you are trying to declare it without that argument.

Even though your base class supports several types of constructors, each derived class must also have every constructor you wish to use on it explicitly declared/implemented (with the exception of the default constructor).

EDIT

Here is some semi-functional code:

class PokerPlayer : public Person
{
  ...
  PokerPlayer(char* fname, char* lname, int val);
  ...
};

Implementation

PokerPlayer::PokerPlayer(char* fname, char* lname, int val) : Person(fname, lname, val)
{
  // Anything else we should do...
}
Sign up to request clarification or add additional context in comments.

5 Comments

I've modified the code according to your advice, yet the program still fails to compile, throwing the error message such as "expected ',' or '...' before '!' token" and "expected primary-expression before')' token". There's no exclamation mark.
I updated my example with some functional code as to how this should look.
I got another problem. The code above compile successfully with VS2010 but fails to compile with the IDE I used yesterday. Why so?
What IDE were you using? It was probably the compiler used behind it. If you were using Dev-C++ with its default compiler, this could be bad. Dev-C++'s compiler is seriously outdated.
Well, I've figure out what's wrong with the code above, the variable name "not" is a preserved keyword in C++, that's why my compiler failed to compile. Thank you all the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.