C++ Creating variables from protected abstract/derived class





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to figure out where I am going wrong with creating variables from a derived class. I have the abstract class, the derived class and am trying to create the derived class as a variable in the main testing program. However I get the error: no matching function for call to DerivedPlayer::DerivedPlayer()’. I haven't been able to find the correct syntax as to create and initialize a variable of the derived class. Also note that the abstract class's constructor is protected.



Abstract Header (Base.h)



    #ifndef BASE_H_
#define BASE_H_

#include <iostream>
#include <vector>

class Base {
public:
virtual ~Base() {}

protected:
Base(std::string s) : x(0), s(s), v(0) {}

int x;
std::string s;
std::vector<int> v;
};
#endif


Derived Header (Derived.h)



    #ifndef DERIVED_H_
#define DERIVED_H_

#include "Base.h"

class Derived : public Base {
public:
Derived(std::string name){ s = name; }
virtual ~Derived();
};

#endif


Test Code (InTest.cpp)



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

int main() {

Derived a = Derived("R2-D2");
Derived b = Derived("C-3PO");

return 0;
}


Build Log



    03:23:52 **** Incremental Build of configuration Debug for project InTest ****
make all
Building file: ../src/InTest.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/InTest.d" -MT"src/InTest.o" -o "src/InTest.o" "../src/InTest.cpp"
In file included from ../src/InTest.cpp:2:0:
../src/Derived.h: In constructor ‘Derived::Derived(std::string)’:
../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’
Derived(std::string name){ s = name; }
^
../src/Derived.h:8:27: note: candidates are:
In file included from ../src/Derived.h:4:0,
from ../src/InTest.cpp:2:
../src/Base.h:12:2: note: Base::Base(std::string)
Base(std::string s) : x(0), s(s), v(0) {}
^
../src/Base.h:12:2: note: candidate expects 1 argument, 0 provided
../src/Base.h:7:7: note: Base::Base(const Base&)
class Base {
^
../src/Base.h:7:7: note: candidate expects 1 argument, 0 provided
make: *** [src/InTest.o] Error 1

03:23:52 Build Finished (took 214ms)









share|improve this question

























  • Please copy-paste the full and complete error output you get.

    – Some programmer dude
    Nov 25 '18 at 10:52











  • The full error was posted in the OP and is : ../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’. The entire compile log is too large to post as a comment.

    – TheWinterSnow
    Nov 25 '18 at 11:06








  • 1





    You should post the full error (including informational notes etc.) in the question body. Please edit your question. And please try to create an Minimal, Complete, and Verifiable example to show us, because with the code you show the DerivedPlayer default constructor should not be used, so the error message you show doesn't fit the code you currently show.

    – Some programmer dude
    Nov 25 '18 at 11:16













  • The code has been revised with a build log included.

    – TheWinterSnow
    Nov 25 '18 at 11:27











  • I don't see an abstract class here. Did you intended to have an abstract class?

    – curiousguy
    Nov 27 '18 at 10:43


















0















I am trying to figure out where I am going wrong with creating variables from a derived class. I have the abstract class, the derived class and am trying to create the derived class as a variable in the main testing program. However I get the error: no matching function for call to DerivedPlayer::DerivedPlayer()’. I haven't been able to find the correct syntax as to create and initialize a variable of the derived class. Also note that the abstract class's constructor is protected.



Abstract Header (Base.h)



    #ifndef BASE_H_
#define BASE_H_

#include <iostream>
#include <vector>

class Base {
public:
virtual ~Base() {}

protected:
Base(std::string s) : x(0), s(s), v(0) {}

int x;
std::string s;
std::vector<int> v;
};
#endif


Derived Header (Derived.h)



    #ifndef DERIVED_H_
#define DERIVED_H_

#include "Base.h"

class Derived : public Base {
public:
Derived(std::string name){ s = name; }
virtual ~Derived();
};

#endif


Test Code (InTest.cpp)



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

int main() {

Derived a = Derived("R2-D2");
Derived b = Derived("C-3PO");

return 0;
}


Build Log



    03:23:52 **** Incremental Build of configuration Debug for project InTest ****
make all
Building file: ../src/InTest.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/InTest.d" -MT"src/InTest.o" -o "src/InTest.o" "../src/InTest.cpp"
In file included from ../src/InTest.cpp:2:0:
../src/Derived.h: In constructor ‘Derived::Derived(std::string)’:
../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’
Derived(std::string name){ s = name; }
^
../src/Derived.h:8:27: note: candidates are:
In file included from ../src/Derived.h:4:0,
from ../src/InTest.cpp:2:
../src/Base.h:12:2: note: Base::Base(std::string)
Base(std::string s) : x(0), s(s), v(0) {}
^
../src/Base.h:12:2: note: candidate expects 1 argument, 0 provided
../src/Base.h:7:7: note: Base::Base(const Base&)
class Base {
^
../src/Base.h:7:7: note: candidate expects 1 argument, 0 provided
make: *** [src/InTest.o] Error 1

03:23:52 Build Finished (took 214ms)









share|improve this question

























  • Please copy-paste the full and complete error output you get.

    – Some programmer dude
    Nov 25 '18 at 10:52











  • The full error was posted in the OP and is : ../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’. The entire compile log is too large to post as a comment.

    – TheWinterSnow
    Nov 25 '18 at 11:06








  • 1





    You should post the full error (including informational notes etc.) in the question body. Please edit your question. And please try to create an Minimal, Complete, and Verifiable example to show us, because with the code you show the DerivedPlayer default constructor should not be used, so the error message you show doesn't fit the code you currently show.

    – Some programmer dude
    Nov 25 '18 at 11:16













  • The code has been revised with a build log included.

    – TheWinterSnow
    Nov 25 '18 at 11:27











  • I don't see an abstract class here. Did you intended to have an abstract class?

    – curiousguy
    Nov 27 '18 at 10:43














0












0








0








I am trying to figure out where I am going wrong with creating variables from a derived class. I have the abstract class, the derived class and am trying to create the derived class as a variable in the main testing program. However I get the error: no matching function for call to DerivedPlayer::DerivedPlayer()’. I haven't been able to find the correct syntax as to create and initialize a variable of the derived class. Also note that the abstract class's constructor is protected.



Abstract Header (Base.h)



    #ifndef BASE_H_
#define BASE_H_

#include <iostream>
#include <vector>

class Base {
public:
virtual ~Base() {}

protected:
Base(std::string s) : x(0), s(s), v(0) {}

int x;
std::string s;
std::vector<int> v;
};
#endif


Derived Header (Derived.h)



    #ifndef DERIVED_H_
#define DERIVED_H_

#include "Base.h"

class Derived : public Base {
public:
Derived(std::string name){ s = name; }
virtual ~Derived();
};

#endif


Test Code (InTest.cpp)



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

int main() {

Derived a = Derived("R2-D2");
Derived b = Derived("C-3PO");

return 0;
}


Build Log



    03:23:52 **** Incremental Build of configuration Debug for project InTest ****
make all
Building file: ../src/InTest.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/InTest.d" -MT"src/InTest.o" -o "src/InTest.o" "../src/InTest.cpp"
In file included from ../src/InTest.cpp:2:0:
../src/Derived.h: In constructor ‘Derived::Derived(std::string)’:
../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’
Derived(std::string name){ s = name; }
^
../src/Derived.h:8:27: note: candidates are:
In file included from ../src/Derived.h:4:0,
from ../src/InTest.cpp:2:
../src/Base.h:12:2: note: Base::Base(std::string)
Base(std::string s) : x(0), s(s), v(0) {}
^
../src/Base.h:12:2: note: candidate expects 1 argument, 0 provided
../src/Base.h:7:7: note: Base::Base(const Base&)
class Base {
^
../src/Base.h:7:7: note: candidate expects 1 argument, 0 provided
make: *** [src/InTest.o] Error 1

03:23:52 Build Finished (took 214ms)









share|improve this question
















I am trying to figure out where I am going wrong with creating variables from a derived class. I have the abstract class, the derived class and am trying to create the derived class as a variable in the main testing program. However I get the error: no matching function for call to DerivedPlayer::DerivedPlayer()’. I haven't been able to find the correct syntax as to create and initialize a variable of the derived class. Also note that the abstract class's constructor is protected.



Abstract Header (Base.h)



    #ifndef BASE_H_
#define BASE_H_

#include <iostream>
#include <vector>

class Base {
public:
virtual ~Base() {}

protected:
Base(std::string s) : x(0), s(s), v(0) {}

int x;
std::string s;
std::vector<int> v;
};
#endif


Derived Header (Derived.h)



    #ifndef DERIVED_H_
#define DERIVED_H_

#include "Base.h"

class Derived : public Base {
public:
Derived(std::string name){ s = name; }
virtual ~Derived();
};

#endif


Test Code (InTest.cpp)



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

int main() {

Derived a = Derived("R2-D2");
Derived b = Derived("C-3PO");

return 0;
}


Build Log



    03:23:52 **** Incremental Build of configuration Debug for project InTest ****
make all
Building file: ../src/InTest.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/InTest.d" -MT"src/InTest.o" -o "src/InTest.o" "../src/InTest.cpp"
In file included from ../src/InTest.cpp:2:0:
../src/Derived.h: In constructor ‘Derived::Derived(std::string)’:
../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’
Derived(std::string name){ s = name; }
^
../src/Derived.h:8:27: note: candidates are:
In file included from ../src/Derived.h:4:0,
from ../src/InTest.cpp:2:
../src/Base.h:12:2: note: Base::Base(std::string)
Base(std::string s) : x(0), s(s), v(0) {}
^
../src/Base.h:12:2: note: candidate expects 1 argument, 0 provided
../src/Base.h:7:7: note: Base::Base(const Base&)
class Base {
^
../src/Base.h:7:7: note: candidate expects 1 argument, 0 provided
make: *** [src/InTest.o] Error 1

03:23:52 Build Finished (took 214ms)






c++ inheritance abstract-class derived-class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 11:26







TheWinterSnow

















asked Nov 25 '18 at 10:45









TheWinterSnowTheWinterSnow

738




738













  • Please copy-paste the full and complete error output you get.

    – Some programmer dude
    Nov 25 '18 at 10:52











  • The full error was posted in the OP and is : ../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’. The entire compile log is too large to post as a comment.

    – TheWinterSnow
    Nov 25 '18 at 11:06








  • 1





    You should post the full error (including informational notes etc.) in the question body. Please edit your question. And please try to create an Minimal, Complete, and Verifiable example to show us, because with the code you show the DerivedPlayer default constructor should not be used, so the error message you show doesn't fit the code you currently show.

    – Some programmer dude
    Nov 25 '18 at 11:16













  • The code has been revised with a build log included.

    – TheWinterSnow
    Nov 25 '18 at 11:27











  • I don't see an abstract class here. Did you intended to have an abstract class?

    – curiousguy
    Nov 27 '18 at 10:43



















  • Please copy-paste the full and complete error output you get.

    – Some programmer dude
    Nov 25 '18 at 10:52











  • The full error was posted in the OP and is : ../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’. The entire compile log is too large to post as a comment.

    – TheWinterSnow
    Nov 25 '18 at 11:06








  • 1





    You should post the full error (including informational notes etc.) in the question body. Please edit your question. And please try to create an Minimal, Complete, and Verifiable example to show us, because with the code you show the DerivedPlayer default constructor should not be used, so the error message you show doesn't fit the code you currently show.

    – Some programmer dude
    Nov 25 '18 at 11:16













  • The code has been revised with a build log included.

    – TheWinterSnow
    Nov 25 '18 at 11:27











  • I don't see an abstract class here. Did you intended to have an abstract class?

    – curiousguy
    Nov 27 '18 at 10:43

















Please copy-paste the full and complete error output you get.

– Some programmer dude
Nov 25 '18 at 10:52





Please copy-paste the full and complete error output you get.

– Some programmer dude
Nov 25 '18 at 10:52













The full error was posted in the OP and is : ../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’. The entire compile log is too large to post as a comment.

– TheWinterSnow
Nov 25 '18 at 11:06







The full error was posted in the OP and is : ../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’. The entire compile log is too large to post as a comment.

– TheWinterSnow
Nov 25 '18 at 11:06






1




1





You should post the full error (including informational notes etc.) in the question body. Please edit your question. And please try to create an Minimal, Complete, and Verifiable example to show us, because with the code you show the DerivedPlayer default constructor should not be used, so the error message you show doesn't fit the code you currently show.

– Some programmer dude
Nov 25 '18 at 11:16







You should post the full error (including informational notes etc.) in the question body. Please edit your question. And please try to create an Minimal, Complete, and Verifiable example to show us, because with the code you show the DerivedPlayer default constructor should not be used, so the error message you show doesn't fit the code you currently show.

– Some programmer dude
Nov 25 '18 at 11:16















The code has been revised with a build log included.

– TheWinterSnow
Nov 25 '18 at 11:27





The code has been revised with a build log included.

– TheWinterSnow
Nov 25 '18 at 11:27













I don't see an abstract class here. Did you intended to have an abstract class?

– curiousguy
Nov 27 '18 at 10:43





I don't see an abstract class here. Did you intended to have an abstract class?

– curiousguy
Nov 27 '18 at 10:43












1 Answer
1






active

oldest

votes


















1














Here's the main part of the error message:



../src/Derived.h: In constructor ‘Derived::Derived(std::string)’:
../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’
Derived(std::string name){ s = name; }


Because Derived inherits from Base, each time a Derived object is constructed the Base class constructor also have to run. The problem with your current code is that you let the default Base constructor be called, but there isn't any.



You solve it by "calling" the correct Base constructor in the Derived constructor initializer list:



Derived::Derived(std::string name)
: Base(name)
{}





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466688%2fc-creating-variables-from-protected-abstract-derived-class%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Here's the main part of the error message:



    ../src/Derived.h: In constructor ‘Derived::Derived(std::string)’:
    ../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’
    Derived(std::string name){ s = name; }


    Because Derived inherits from Base, each time a Derived object is constructed the Base class constructor also have to run. The problem with your current code is that you let the default Base constructor be called, but there isn't any.



    You solve it by "calling" the correct Base constructor in the Derived constructor initializer list:



    Derived::Derived(std::string name)
    : Base(name)
    {}





    share|improve this answer




























      1














      Here's the main part of the error message:



      ../src/Derived.h: In constructor ‘Derived::Derived(std::string)’:
      ../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’
      Derived(std::string name){ s = name; }


      Because Derived inherits from Base, each time a Derived object is constructed the Base class constructor also have to run. The problem with your current code is that you let the default Base constructor be called, but there isn't any.



      You solve it by "calling" the correct Base constructor in the Derived constructor initializer list:



      Derived::Derived(std::string name)
      : Base(name)
      {}





      share|improve this answer


























        1












        1








        1







        Here's the main part of the error message:



        ../src/Derived.h: In constructor ‘Derived::Derived(std::string)’:
        ../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’
        Derived(std::string name){ s = name; }


        Because Derived inherits from Base, each time a Derived object is constructed the Base class constructor also have to run. The problem with your current code is that you let the default Base constructor be called, but there isn't any.



        You solve it by "calling" the correct Base constructor in the Derived constructor initializer list:



        Derived::Derived(std::string name)
        : Base(name)
        {}





        share|improve this answer













        Here's the main part of the error message:



        ../src/Derived.h: In constructor ‘Derived::Derived(std::string)’:
        ../src/Derived.h:8:27: error: no matching function for call to ‘Base::Base()’
        Derived(std::string name){ s = name; }


        Because Derived inherits from Base, each time a Derived object is constructed the Base class constructor also have to run. The problem with your current code is that you let the default Base constructor be called, but there isn't any.



        You solve it by "calling" the correct Base constructor in the Derived constructor initializer list:



        Derived::Derived(std::string name)
        : Base(name)
        {}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 11:33









        Some programmer dudeSome programmer dude

        306k25266429




        306k25266429
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466688%2fc-creating-variables-from-protected-abstract-derived-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud