Threading and Polymorphism
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
#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++
|
show 1 more comment
#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++
5
p = &obj1
doesn't make sense in the body of the class definition. It's a statement, and must be within some function, sayme
.
– 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 (likep
).
– Some programmer dude
Nov 22 '18 at 14:15
1
You don't needp
, you can pass&obj1
tobind
.
– molbdnilo
Nov 22 '18 at 14:29
Just a suggestion: you don't needcaller
if all you want to do is explore polymorphism and threads. Inmain
:derived obj1; base *p = &obj1; std::thread t(&base::fun_2, p); t.join();
. The constructor forstd::thread
does just about everything thatstd::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
|
show 1 more comment
#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++
#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++
c++
edited Nov 22 '18 at 16:24
payo
3,67911529
3,67911529
asked Nov 22 '18 at 14:13
Roshan MehtaRoshan Mehta
86711032
86711032
5
p = &obj1
doesn't make sense in the body of the class definition. It's a statement, and must be within some function, sayme
.
– 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 (likep
).
– Some programmer dude
Nov 22 '18 at 14:15
1
You don't needp
, you can pass&obj1
tobind
.
– molbdnilo
Nov 22 '18 at 14:29
Just a suggestion: you don't needcaller
if all you want to do is explore polymorphism and threads. Inmain
:derived obj1; base *p = &obj1; std::thread t(&base::fun_2, p); t.join();
. The constructor forstd::thread
does just about everything thatstd::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
|
show 1 more comment
5
p = &obj1
doesn't make sense in the body of the class definition. It's a statement, and must be within some function, sayme
.
– 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 (likep
).
– Some programmer dude
Nov 22 '18 at 14:15
1
You don't needp
, you can pass&obj1
tobind
.
– molbdnilo
Nov 22 '18 at 14:29
Just a suggestion: you don't needcaller
if all you want to do is explore polymorphism and threads. Inmain
:derived obj1; base *p = &obj1; std::thread t(&base::fun_2, p); t.join();
. The constructor forstd::thread
does just about everything thatstd::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
|
show 1 more comment
1 Answer
1
active
oldest
votes
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) {}
......
};
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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) {}
......
};
add a comment |
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) {}
......
};
add a comment |
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) {}
......
};
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) {}
......
};
edited Nov 22 '18 at 14:30
answered Nov 22 '18 at 14:19
snake_stylesnake_style
1,156511
1,156511
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
5
p = &obj1
doesn't make sense in the body of the class definition. It's a statement, and must be within some function, sayme
.– 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
tobind
.– 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. Inmain
:derived obj1; base *p = &obj1; std::thread t(&base::fun_2, p); t.join();
. The constructor forstd::thread
does just about everything thatstd::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