Threading and Polymorphism












-2















#include<iostream> 
#include <thread>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

class base
{
public:
virtual void fun_1() { cout << "base-1n"; }
virtual void fun_2() { cout << "base-2n"; }

};

class derived : public base
{
public:
void fun_1() { cout << "derived-1n"; }
void fun_2() { cout << "derived-2n";
}
};


class caller
{
private:
base *p;
derived obj1;
p = &obj1;
public:
void me()
{
std::thread t(std::bind(&base::fun_2, p), this);
t.join();
}
};

int main()
{
caller c;
c.me();
return 0;
}


I have a written a very simple threading and polymorphism example. All I wanted to do is to call a derived function from a different class which is containing that another class object. The program fails to compile with the message p does not name a type which I could not understand why.










share|improve this question




















  • 5





    p = &obj1 doesn't make sense in the body of the class definition. It's a statement, and must be within some function, say me.

    – Igor Tandetnik
    Nov 22 '18 at 14:15






  • 3





    You can't have generic statements in the declaration section of a class, all statements must be inside functions. I also think you should read more about constructor initializer lists as a way to initialize members (like p).

    – Some programmer dude
    Nov 22 '18 at 14:15








  • 1





    You don't need p, you can pass &obj1 to bind.

    – molbdnilo
    Nov 22 '18 at 14:29











  • Just a suggestion: you don't need caller if all you want to do is explore polymorphism and threads. In main: derived obj1; base *p = &obj1; std::thread t(&base::fun_2, p); t.join();. The constructor for std::thread does just about everything that std::bind does.

    – Pete Becker
    Nov 22 '18 at 14:37











  • this question has nothing to do with threads

    – payo
    Nov 22 '18 at 16:24
















-2















#include<iostream> 
#include <thread>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

class base
{
public:
virtual void fun_1() { cout << "base-1n"; }
virtual void fun_2() { cout << "base-2n"; }

};

class derived : public base
{
public:
void fun_1() { cout << "derived-1n"; }
void fun_2() { cout << "derived-2n";
}
};


class caller
{
private:
base *p;
derived obj1;
p = &obj1;
public:
void me()
{
std::thread t(std::bind(&base::fun_2, p), this);
t.join();
}
};

int main()
{
caller c;
c.me();
return 0;
}


I have a written a very simple threading and polymorphism example. All I wanted to do is to call a derived function from a different class which is containing that another class object. The program fails to compile with the message p does not name a type which I could not understand why.










share|improve this question




















  • 5





    p = &obj1 doesn't make sense in the body of the class definition. It's a statement, and must be within some function, say me.

    – Igor Tandetnik
    Nov 22 '18 at 14:15






  • 3





    You can't have generic statements in the declaration section of a class, all statements must be inside functions. I also think you should read more about constructor initializer lists as a way to initialize members (like p).

    – Some programmer dude
    Nov 22 '18 at 14:15








  • 1





    You don't need p, you can pass &obj1 to bind.

    – molbdnilo
    Nov 22 '18 at 14:29











  • Just a suggestion: you don't need caller if all you want to do is explore polymorphism and threads. In main: derived obj1; base *p = &obj1; std::thread t(&base::fun_2, p); t.join();. The constructor for std::thread does just about everything that std::bind does.

    – Pete Becker
    Nov 22 '18 at 14:37











  • this question has nothing to do with threads

    – payo
    Nov 22 '18 at 16:24














-2












-2








-2








#include<iostream> 
#include <thread>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

class base
{
public:
virtual void fun_1() { cout << "base-1n"; }
virtual void fun_2() { cout << "base-2n"; }

};

class derived : public base
{
public:
void fun_1() { cout << "derived-1n"; }
void fun_2() { cout << "derived-2n";
}
};


class caller
{
private:
base *p;
derived obj1;
p = &obj1;
public:
void me()
{
std::thread t(std::bind(&base::fun_2, p), this);
t.join();
}
};

int main()
{
caller c;
c.me();
return 0;
}


I have a written a very simple threading and polymorphism example. All I wanted to do is to call a derived function from a different class which is containing that another class object. The program fails to compile with the message p does not name a type which I could not understand why.










share|improve this question
















#include<iostream> 
#include <thread>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

class base
{
public:
virtual void fun_1() { cout << "base-1n"; }
virtual void fun_2() { cout << "base-2n"; }

};

class derived : public base
{
public:
void fun_1() { cout << "derived-1n"; }
void fun_2() { cout << "derived-2n";
}
};


class caller
{
private:
base *p;
derived obj1;
p = &obj1;
public:
void me()
{
std::thread t(std::bind(&base::fun_2, p), this);
t.join();
}
};

int main()
{
caller c;
c.me();
return 0;
}


I have a written a very simple threading and polymorphism example. All I wanted to do is to call a derived function from a different class which is containing that another class object. The program fails to compile with the message p does not name a type which I could not understand why.







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 16:24









payo

3,64511529




3,64511529










asked Nov 22 '18 at 14:13









Roshan MehtaRoshan Mehta

85211032




85211032








  • 5





    p = &obj1 doesn't make sense in the body of the class definition. It's a statement, and must be within some function, say me.

    – Igor Tandetnik
    Nov 22 '18 at 14:15






  • 3





    You can't have generic statements in the declaration section of a class, all statements must be inside functions. I also think you should read more about constructor initializer lists as a way to initialize members (like p).

    – Some programmer dude
    Nov 22 '18 at 14:15








  • 1





    You don't need p, you can pass &obj1 to bind.

    – molbdnilo
    Nov 22 '18 at 14:29











  • Just a suggestion: you don't need caller if all you want to do is explore polymorphism and threads. In main: derived obj1; base *p = &obj1; std::thread t(&base::fun_2, p); t.join();. The constructor for std::thread does just about everything that std::bind does.

    – Pete Becker
    Nov 22 '18 at 14:37











  • this question has nothing to do with threads

    – payo
    Nov 22 '18 at 16:24














  • 5





    p = &obj1 doesn't make sense in the body of the class definition. It's a statement, and must be within some function, say me.

    – Igor Tandetnik
    Nov 22 '18 at 14:15






  • 3





    You can't have generic statements in the declaration section of a class, all statements must be inside functions. I also think you should read more about constructor initializer lists as a way to initialize members (like p).

    – Some programmer dude
    Nov 22 '18 at 14:15








  • 1





    You don't need p, you can pass &obj1 to bind.

    – molbdnilo
    Nov 22 '18 at 14:29











  • Just a suggestion: you don't need caller if all you want to do is explore polymorphism and threads. In main: derived obj1; base *p = &obj1; std::thread t(&base::fun_2, p); t.join();. The constructor for std::thread does just about everything that std::bind does.

    – Pete Becker
    Nov 22 '18 at 14:37











  • this question has nothing to do with threads

    – payo
    Nov 22 '18 at 16:24








5




5





p = &obj1 doesn't make sense in the body of the class definition. It's a statement, and must be within some function, say me.

– Igor Tandetnik
Nov 22 '18 at 14:15





p = &obj1 doesn't make sense in the body of the class definition. It's a statement, and must be within some function, say me.

– Igor Tandetnik
Nov 22 '18 at 14:15




3




3





You can't have generic statements in the declaration section of a class, all statements must be inside functions. I also think you should read more about constructor initializer lists as a way to initialize members (like p).

– Some programmer dude
Nov 22 '18 at 14:15







You can't have generic statements in the declaration section of a class, all statements must be inside functions. I also think you should read more about constructor initializer lists as a way to initialize members (like p).

– Some programmer dude
Nov 22 '18 at 14:15






1




1





You don't need p, you can pass &obj1 to bind.

– molbdnilo
Nov 22 '18 at 14:29





You don't need p, you can pass &obj1 to bind.

– molbdnilo
Nov 22 '18 at 14:29













Just a suggestion: you don't need caller if all you want to do is explore polymorphism and threads. In main: derived obj1; base *p = &obj1; std::thread t(&base::fun_2, p); t.join();. The constructor for std::thread does just about everything that std::bind does.

– Pete Becker
Nov 22 '18 at 14:37





Just a suggestion: you don't need caller if all you want to do is explore polymorphism and threads. In main: derived obj1; base *p = &obj1; std::thread t(&base::fun_2, p); t.join();. The constructor for std::thread does just about everything that std::bind does.

– Pete Becker
Nov 22 '18 at 14:37













this question has nothing to do with threads

– payo
Nov 22 '18 at 16:24





this question has nothing to do with threads

– payo
Nov 22 '18 at 16:24












1 Answer
1






active

oldest

votes


















1














Your error is in line:



p = &obj1;


It is better to write like this, it should help:



class caller
{
private:
derived obj1;
base *p = &obj1;
......
};


or initialize pointer in costructor:



class caller
{
private:
derived obj1;
base *p = nullptr;
caller() : p(&obj1) {}
......
};





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%2f53432858%2fthreading-and-polymorphism%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














    Your error is in line:



    p = &obj1;


    It is better to write like this, it should help:



    class caller
    {
    private:
    derived obj1;
    base *p = &obj1;
    ......
    };


    or initialize pointer in costructor:



    class caller
    {
    private:
    derived obj1;
    base *p = nullptr;
    caller() : p(&obj1) {}
    ......
    };





    share|improve this answer






























      1














      Your error is in line:



      p = &obj1;


      It is better to write like this, it should help:



      class caller
      {
      private:
      derived obj1;
      base *p = &obj1;
      ......
      };


      or initialize pointer in costructor:



      class caller
      {
      private:
      derived obj1;
      base *p = nullptr;
      caller() : p(&obj1) {}
      ......
      };





      share|improve this answer




























        1












        1








        1







        Your error is in line:



        p = &obj1;


        It is better to write like this, it should help:



        class caller
        {
        private:
        derived obj1;
        base *p = &obj1;
        ......
        };


        or initialize pointer in costructor:



        class caller
        {
        private:
        derived obj1;
        base *p = nullptr;
        caller() : p(&obj1) {}
        ......
        };





        share|improve this answer















        Your error is in line:



        p = &obj1;


        It is better to write like this, it should help:



        class caller
        {
        private:
        derived obj1;
        base *p = &obj1;
        ......
        };


        or initialize pointer in costructor:



        class caller
        {
        private:
        derived obj1;
        base *p = nullptr;
        caller() : p(&obj1) {}
        ......
        };






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 14:30

























        answered Nov 22 '18 at 14:19









        snake_stylesnake_style

        1,170410




        1,170410






























            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%2f53432858%2fthreading-and-polymorphism%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







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen