Callback issues error: invalid use of non-static data member












-2














EDIT: Updated clean reproducible example.



I'm a beginner in C++ with a few years experience with C, so apologies if I am going about this the wrong way. I have tried to break down the issue to as small an example of code as I can. In my code I have inherited a C callback function and inside this I want to trigger another callback from a class member.



I am having two issues:



a) I don't have an instance of Range in Callback and the callback does not have a void * param for me to use.



b) I am getting an error message




error: invalid use of non-static data member ‘Range::cbk’




I'm just starting to learn C++ and the rules of OOP so apologies if I am doing things that are fundamentally flawed.



#include <iostream>
#include <functional>

class Range
{
public:
typedef std::function<void()> TCallback;
TCallback cbk;

Range()
{
cbk = std::bind(&Range::RunTest, this);
}

void Close() {}

~Range() { Close(); }

void RunTest()
{
std::cout << "RunningTestn";
}
};

static void Callback(bool ev)
{
if (ev)
Range::cbk();
}


int main()
{
std::function<void(bool)> test;
Range r1;
test = std::bind(Callback, 1);
test(1);

return 0;
}









share|improve this question




















  • 1




    Where is your Minimal, Complete, and Verifiable example?
    – Lightness Races in Orbit
    Nov 19 '18 at 23:41






  • 1




    Please provide Minimal, Complete, and Verifiable example. As is currently, it is hard to understand what your problem may be. Is the problem that you are trying to call method, as a static method? If you are calling it from outside the TestOne, it is marked as private, as well, as being non-static.
    – Algirdas Preidžius
    Nov 19 '18 at 23:43










  • @LightnessRacesinOrbit Updated to break down issue to as small an example as I could.
    – tech 1990
    Nov 20 '18 at 23:04










  • @AlgirdasPreidžius Updated to break down issue to as small an example as I could
    – tech 1990
    Nov 20 '18 at 23:05










  • It is unclear what you're asking or what you're trying to do.
    – Killzone Kid
    Nov 20 '18 at 23:22
















-2














EDIT: Updated clean reproducible example.



I'm a beginner in C++ with a few years experience with C, so apologies if I am going about this the wrong way. I have tried to break down the issue to as small an example of code as I can. In my code I have inherited a C callback function and inside this I want to trigger another callback from a class member.



I am having two issues:



a) I don't have an instance of Range in Callback and the callback does not have a void * param for me to use.



b) I am getting an error message




error: invalid use of non-static data member ‘Range::cbk’




I'm just starting to learn C++ and the rules of OOP so apologies if I am doing things that are fundamentally flawed.



#include <iostream>
#include <functional>

class Range
{
public:
typedef std::function<void()> TCallback;
TCallback cbk;

Range()
{
cbk = std::bind(&Range::RunTest, this);
}

void Close() {}

~Range() { Close(); }

void RunTest()
{
std::cout << "RunningTestn";
}
};

static void Callback(bool ev)
{
if (ev)
Range::cbk();
}


int main()
{
std::function<void(bool)> test;
Range r1;
test = std::bind(Callback, 1);
test(1);

return 0;
}









share|improve this question




















  • 1




    Where is your Minimal, Complete, and Verifiable example?
    – Lightness Races in Orbit
    Nov 19 '18 at 23:41






  • 1




    Please provide Minimal, Complete, and Verifiable example. As is currently, it is hard to understand what your problem may be. Is the problem that you are trying to call method, as a static method? If you are calling it from outside the TestOne, it is marked as private, as well, as being non-static.
    – Algirdas Preidžius
    Nov 19 '18 at 23:43










  • @LightnessRacesinOrbit Updated to break down issue to as small an example as I could.
    – tech 1990
    Nov 20 '18 at 23:04










  • @AlgirdasPreidžius Updated to break down issue to as small an example as I could
    – tech 1990
    Nov 20 '18 at 23:05










  • It is unclear what you're asking or what you're trying to do.
    – Killzone Kid
    Nov 20 '18 at 23:22














-2












-2








-2







EDIT: Updated clean reproducible example.



I'm a beginner in C++ with a few years experience with C, so apologies if I am going about this the wrong way. I have tried to break down the issue to as small an example of code as I can. In my code I have inherited a C callback function and inside this I want to trigger another callback from a class member.



I am having two issues:



a) I don't have an instance of Range in Callback and the callback does not have a void * param for me to use.



b) I am getting an error message




error: invalid use of non-static data member ‘Range::cbk’




I'm just starting to learn C++ and the rules of OOP so apologies if I am doing things that are fundamentally flawed.



#include <iostream>
#include <functional>

class Range
{
public:
typedef std::function<void()> TCallback;
TCallback cbk;

Range()
{
cbk = std::bind(&Range::RunTest, this);
}

void Close() {}

~Range() { Close(); }

void RunTest()
{
std::cout << "RunningTestn";
}
};

static void Callback(bool ev)
{
if (ev)
Range::cbk();
}


int main()
{
std::function<void(bool)> test;
Range r1;
test = std::bind(Callback, 1);
test(1);

return 0;
}









share|improve this question















EDIT: Updated clean reproducible example.



I'm a beginner in C++ with a few years experience with C, so apologies if I am going about this the wrong way. I have tried to break down the issue to as small an example of code as I can. In my code I have inherited a C callback function and inside this I want to trigger another callback from a class member.



I am having two issues:



a) I don't have an instance of Range in Callback and the callback does not have a void * param for me to use.



b) I am getting an error message




error: invalid use of non-static data member ‘Range::cbk’




I'm just starting to learn C++ and the rules of OOP so apologies if I am doing things that are fundamentally flawed.



#include <iostream>
#include <functional>

class Range
{
public:
typedef std::function<void()> TCallback;
TCallback cbk;

Range()
{
cbk = std::bind(&Range::RunTest, this);
}

void Close() {}

~Range() { Close(); }

void RunTest()
{
std::cout << "RunningTestn";
}
};

static void Callback(bool ev)
{
if (ev)
Range::cbk();
}


int main()
{
std::function<void(bool)> test;
Range r1;
test = std::bind(Callback, 1);
test(1);

return 0;
}






c++ class oop std






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 23:49

























asked Nov 19 '18 at 23:10









tech 1990

14




14








  • 1




    Where is your Minimal, Complete, and Verifiable example?
    – Lightness Races in Orbit
    Nov 19 '18 at 23:41






  • 1




    Please provide Minimal, Complete, and Verifiable example. As is currently, it is hard to understand what your problem may be. Is the problem that you are trying to call method, as a static method? If you are calling it from outside the TestOne, it is marked as private, as well, as being non-static.
    – Algirdas Preidžius
    Nov 19 '18 at 23:43










  • @LightnessRacesinOrbit Updated to break down issue to as small an example as I could.
    – tech 1990
    Nov 20 '18 at 23:04










  • @AlgirdasPreidžius Updated to break down issue to as small an example as I could
    – tech 1990
    Nov 20 '18 at 23:05










  • It is unclear what you're asking or what you're trying to do.
    – Killzone Kid
    Nov 20 '18 at 23:22














  • 1




    Where is your Minimal, Complete, and Verifiable example?
    – Lightness Races in Orbit
    Nov 19 '18 at 23:41






  • 1




    Please provide Minimal, Complete, and Verifiable example. As is currently, it is hard to understand what your problem may be. Is the problem that you are trying to call method, as a static method? If you are calling it from outside the TestOne, it is marked as private, as well, as being non-static.
    – Algirdas Preidžius
    Nov 19 '18 at 23:43










  • @LightnessRacesinOrbit Updated to break down issue to as small an example as I could.
    – tech 1990
    Nov 20 '18 at 23:04










  • @AlgirdasPreidžius Updated to break down issue to as small an example as I could
    – tech 1990
    Nov 20 '18 at 23:05










  • It is unclear what you're asking or what you're trying to do.
    – Killzone Kid
    Nov 20 '18 at 23:22








1




1




Where is your Minimal, Complete, and Verifiable example?
– Lightness Races in Orbit
Nov 19 '18 at 23:41




Where is your Minimal, Complete, and Verifiable example?
– Lightness Races in Orbit
Nov 19 '18 at 23:41




1




1




Please provide Minimal, Complete, and Verifiable example. As is currently, it is hard to understand what your problem may be. Is the problem that you are trying to call method, as a static method? If you are calling it from outside the TestOne, it is marked as private, as well, as being non-static.
– Algirdas Preidžius
Nov 19 '18 at 23:43




Please provide Minimal, Complete, and Verifiable example. As is currently, it is hard to understand what your problem may be. Is the problem that you are trying to call method, as a static method? If you are calling it from outside the TestOne, it is marked as private, as well, as being non-static.
– Algirdas Preidžius
Nov 19 '18 at 23:43












@LightnessRacesinOrbit Updated to break down issue to as small an example as I could.
– tech 1990
Nov 20 '18 at 23:04




@LightnessRacesinOrbit Updated to break down issue to as small an example as I could.
– tech 1990
Nov 20 '18 at 23:04












@AlgirdasPreidžius Updated to break down issue to as small an example as I could
– tech 1990
Nov 20 '18 at 23:05




@AlgirdasPreidžius Updated to break down issue to as small an example as I could
– tech 1990
Nov 20 '18 at 23:05












It is unclear what you're asking or what you're trying to do.
– Killzone Kid
Nov 20 '18 at 23:22




It is unclear what you're asking or what you're trying to do.
– Killzone Kid
Nov 20 '18 at 23:22












1 Answer
1






active

oldest

votes


















0














You can't call a member function without an instance.



You did it right in the Range constructor; you did it wrong in main and Callback.



static void Callback(Range* ptr, bool ev)
{
if (ev)
ptr->cbk();
}


And:



test = std::bind(Callback, &r1, 1);
// ^^^^^


If you can't change Callback, then what you want to do is simply impossible. You'd have to have some global Range pointer, but this is really minging. Ultimately it's why classes were invented but if you can't use them because you're stuck with a really flat C API then you just have to hack around it like you would in C.






share|improve this answer





















  • Thanks very much for the input; yes this is exactly the issue I can't change the declaration of Callback. I will probably go down the global pointer route even though I will need an array of global pointers for multiple instances. As ugly as this it seems my only option.
    – tech 1990
    Nov 21 '18 at 11:27










  • @tech1990 Pretty much. As an aside the callback is poorly designed because, knowing that this was always a problem, people tended to at least stick a void* in there for passing information.
    – Lightness Races in Orbit
    Nov 21 '18 at 11:43











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%2f53383960%2fcallback-issues-error-invalid-use-of-non-static-data-member%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









0














You can't call a member function without an instance.



You did it right in the Range constructor; you did it wrong in main and Callback.



static void Callback(Range* ptr, bool ev)
{
if (ev)
ptr->cbk();
}


And:



test = std::bind(Callback, &r1, 1);
// ^^^^^


If you can't change Callback, then what you want to do is simply impossible. You'd have to have some global Range pointer, but this is really minging. Ultimately it's why classes were invented but if you can't use them because you're stuck with a really flat C API then you just have to hack around it like you would in C.






share|improve this answer





















  • Thanks very much for the input; yes this is exactly the issue I can't change the declaration of Callback. I will probably go down the global pointer route even though I will need an array of global pointers for multiple instances. As ugly as this it seems my only option.
    – tech 1990
    Nov 21 '18 at 11:27










  • @tech1990 Pretty much. As an aside the callback is poorly designed because, knowing that this was always a problem, people tended to at least stick a void* in there for passing information.
    – Lightness Races in Orbit
    Nov 21 '18 at 11:43
















0














You can't call a member function without an instance.



You did it right in the Range constructor; you did it wrong in main and Callback.



static void Callback(Range* ptr, bool ev)
{
if (ev)
ptr->cbk();
}


And:



test = std::bind(Callback, &r1, 1);
// ^^^^^


If you can't change Callback, then what you want to do is simply impossible. You'd have to have some global Range pointer, but this is really minging. Ultimately it's why classes were invented but if you can't use them because you're stuck with a really flat C API then you just have to hack around it like you would in C.






share|improve this answer





















  • Thanks very much for the input; yes this is exactly the issue I can't change the declaration of Callback. I will probably go down the global pointer route even though I will need an array of global pointers for multiple instances. As ugly as this it seems my only option.
    – tech 1990
    Nov 21 '18 at 11:27










  • @tech1990 Pretty much. As an aside the callback is poorly designed because, knowing that this was always a problem, people tended to at least stick a void* in there for passing information.
    – Lightness Races in Orbit
    Nov 21 '18 at 11:43














0












0








0






You can't call a member function without an instance.



You did it right in the Range constructor; you did it wrong in main and Callback.



static void Callback(Range* ptr, bool ev)
{
if (ev)
ptr->cbk();
}


And:



test = std::bind(Callback, &r1, 1);
// ^^^^^


If you can't change Callback, then what you want to do is simply impossible. You'd have to have some global Range pointer, but this is really minging. Ultimately it's why classes were invented but if you can't use them because you're stuck with a really flat C API then you just have to hack around it like you would in C.






share|improve this answer












You can't call a member function without an instance.



You did it right in the Range constructor; you did it wrong in main and Callback.



static void Callback(Range* ptr, bool ev)
{
if (ev)
ptr->cbk();
}


And:



test = std::bind(Callback, &r1, 1);
// ^^^^^


If you can't change Callback, then what you want to do is simply impossible. You'd have to have some global Range pointer, but this is really minging. Ultimately it's why classes were invented but if you can't use them because you're stuck with a really flat C API then you just have to hack around it like you would in C.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 10:23









Lightness Races in Orbit

284k51460781




284k51460781












  • Thanks very much for the input; yes this is exactly the issue I can't change the declaration of Callback. I will probably go down the global pointer route even though I will need an array of global pointers for multiple instances. As ugly as this it seems my only option.
    – tech 1990
    Nov 21 '18 at 11:27










  • @tech1990 Pretty much. As an aside the callback is poorly designed because, knowing that this was always a problem, people tended to at least stick a void* in there for passing information.
    – Lightness Races in Orbit
    Nov 21 '18 at 11:43


















  • Thanks very much for the input; yes this is exactly the issue I can't change the declaration of Callback. I will probably go down the global pointer route even though I will need an array of global pointers for multiple instances. As ugly as this it seems my only option.
    – tech 1990
    Nov 21 '18 at 11:27










  • @tech1990 Pretty much. As an aside the callback is poorly designed because, knowing that this was always a problem, people tended to at least stick a void* in there for passing information.
    – Lightness Races in Orbit
    Nov 21 '18 at 11:43
















Thanks very much for the input; yes this is exactly the issue I can't change the declaration of Callback. I will probably go down the global pointer route even though I will need an array of global pointers for multiple instances. As ugly as this it seems my only option.
– tech 1990
Nov 21 '18 at 11:27




Thanks very much for the input; yes this is exactly the issue I can't change the declaration of Callback. I will probably go down the global pointer route even though I will need an array of global pointers for multiple instances. As ugly as this it seems my only option.
– tech 1990
Nov 21 '18 at 11:27












@tech1990 Pretty much. As an aside the callback is poorly designed because, knowing that this was always a problem, people tended to at least stick a void* in there for passing information.
– Lightness Races in Orbit
Nov 21 '18 at 11:43




@tech1990 Pretty much. As an aside the callback is poorly designed because, knowing that this was always a problem, people tended to at least stick a void* in there for passing information.
– Lightness Races in Orbit
Nov 21 '18 at 11:43


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53383960%2fcallback-issues-error-invalid-use-of-non-static-data-member%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

Costa Masnaga

Fotorealismo

Sidney Franklin