Using the RPI Pico SDK, I have three files.
I want to use a callback function and access private members of a class.
The callback function is in an SDK and I can not modify it.
How do I do this?
/////// test.h
bool main_loop_timer_callback(struct repeating_timer *t);
class MyClass {
private:
static int count;
struct repeating_timer main_loop_timer;
public:
MyClass();
friend bool main_loop_timer_callback(struct repeating_timer *t);
};
//////// test.cc
#include <iostream>
#include "pico/stdlib.h"
#include "test.h"
using namespace std;
bool main_loop_timer_callback(struct repeating_timer *t) {
MyClass::count++;
cout << "callback " << MyClass::count << endl;
return true;
}
MyClass::MyClass() {
add_repeating_timer_us(-50000,
main_loop_timer_callback,
NULL,
&main_loop_timer);
count = 0;
};
/////// test-main.cc
#include "pico/stdlib.h"
#include "test.h"
using namespace std;
int main() {
MyClass test;
stdio_init_all();
}
The Pi Pico repeating_timer API allows you to pass a user_data argument when adding a timer (the third parameter to add_repeating_timer_us).
This value is later returned in the user_data member of the struct repeating_timer passed to your callback.
This can be anything you want, e.g. in your case, a pointer to MyClass, so long as you cast it to and from void *.
Related
I'm trying to make something similar to std::Map. I have two classes, NameValue which takes a name and a Value. The class Value can hold data of type int and string. I want the Value class to also accept NameValue to be able to create nested objects. Currently the boost::variant is used to hold the data types allowed to be used.
NameValue.h
#ifndef INC_NAME_VALUE_H_
#define INC_NAME_VALUE_H_
#include <boost/variant.hpp>
#include <iostream>
#include <string>
#include "value.h"
namespace config {
using namespace std;
class Value; // forward declaration
class NameValue {
private:
string name;
Value* valuePtr;
public:
NameValue(){};
NameValue(string name, Value& value)
: name(name)
, valuePtr(&value){};
void Print() {
cout << name << " : ";
// valuePtr->Print();
}
void Set(Value* value) { valuePtr = value; }
};
}
#endif /* INC_NAME_VALUE_H_ */
Value.h
#ifndef INC_VALUE_H_
#define INC_VALUE_H_
#include <boost/variant.hpp>
#include <iostream>
#include <string>
#include "name_value.h"
namespace config {
using namespace std;
using variantDataType = boost::variant<int, string>;
class Value {
private:
variantDataType value;
public:
Value(){};
Value(variantDataType const& value)
: value(value){};
void Print() { cout << value << endl; }
};
}
#endif /* INC_VALUE_H_ */
In Value.h I want to add NameValue to variant like this:
boost::variant<int,string,NameValue> value;
main.cpp
Value i(42);
NameValue nv("meaning", i);
NameValue nv2("nested, NameValue("deep", "value"));//this is what I want
Maybe I'm on the wrong track using variant or the way I'm using dependencies. If there is some other way to make it work I would appreciate the suggestions.
I have a programming assignment where I'm supposed to write up the code for inserting and removing linked lists. However I haven't used C++ in a while and am struggling remember certain things.
Right now, I am simply trying to put a prototype method in a header file, define it in my cpp file, and then call it in my main method. this is what I have.
LinkedList.h
#include <iostream>
using namespace std;
class LinkedList {
public:
void testPrint();
};
LinkedList.cpp
#include "LinkedList.h"
int main() {
LinkedList::testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
I am getting the following errors
a nonstatic member reference must be relative to a specific object
'LinkedList::testPrint': non-standard syntax; use & to create a pointer to member
LinkedList::testPrint() is a member function.
It is not declared static, so that means it must be called on a particular object, defined as LinkedList linked_list, for example. Then use linked_list.testPrint().
Option 1 - static member function declaration
#include <iostream>
using namespace std;
class LinkedList {
public:
static void testPrint();
};
int main() {
LinkedList::testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
Option 2 - Instantiated object with call to member function
#include <iostream>
using namespace std;
class LinkedList {
public:
void testPrint();
};
int main() {
LinkedList linked_list;
linked_list.testPrint();
}
void LinkedList::testPrint() {
cout << "Test" << endl;
}
I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...error: '_bookingVector' was not declared in this scope|
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
Is my syntax wrong? Or are vectors initialized differently?
Here is my code:
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>
using namespace std;
class Schedule
{
public:
Schedule();
void AddBooking(int bday, int btime, int btrainer, int bid);
void RemoveBooking(int bday, int btime);
void DisplaySchedule();
void DisplayAvailableTimeSlots();
//For Testing
void DisplayDebug();
private:
vector<Booking> _bookingVector;
};
#endif // SCHEDULE_H
Schedule.cpp
#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream>
Schedule::Schedule() : _bookingVector()
{ }
void AddBooking(int bday, int btime, int btrainer, int bid){
Booking bookingObject(bday, btime, btrainer, bid);
_bookingVector.push_back(bookingObject);
}
void DisplayDebug(){
for(int i = 0; i < _bookingVector.size(); ++i){
cout << _bookingVecotr[i] << endl;
}
}
I'm very eager to learn what I'm doing wrong and fix it.
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking and DisplayDebug as non-member functions, but these should be members in order to access other members of the class.
Modify the definitions to be in the scope of the Schedule class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
Also, don't say using namespace std in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1 Your default constructor does not do anything that the compiler-generated one wouldn't do. You can safely remove it.
I have the below code that compiles and executes without error, but the line that should be printed in the menu() function is never printed.
Menu.cpp
#include "stdio.h"
#include "Menu.hpp"
#include <iostream>
using namespace std;
namespace View
{
void Menu::startMenu()
{
cout << "2\n";
}
}
Menu.hpp
#ifndef MENU_H //"Header guard"
#define MENU_H
namespace View
{
class Menu
{
void startMenu();
};
}
#endif
I wrote a simple test to call the menu function, if it works correctly the output should be
1
2
3
but the 2 is never printed.
MenuTest.cpp
#include "Menu.hpp"
#include "stdio.h"
#include <iostream>
using namespace std;
int main()
{
cout << "1\n";
View::Menu startMenu();
cout << "3\n";
}
Can someone see what's going on here?
View::Menu startMenu();
Declares a function which returns View::Menu type, which is also known as most vexing parse
To initialize an object and call it's member function, you should do:
View::Menu menu;
menu.startMenu();
BTW, you need to make startMenu() function public:
class Menu
{
public: //<-----
void startMenu();
};
See live sample.
help this helps.
Because you declare function "startMenu()", that is returns type "View:Menu"
But you don't call function startMenu().
Try make following code:
View::Menu obj;
obj.startMenu();
PS. And make startMenu() as public:
class Menu
{
public:
void startMenu();
};
When substracting the brckets of View::Menu startMenu();, the code
View::Menu startMenu;
is a object definition of View::Menu, which does not call the function Menu::startMenu(). And that is when "cout << "2\n";" is not executed. To call further Menu::startMenu():
startMenu.startMenu();
How do I execute a member's function by passing the object and the member's function to another function in c++. I do understand the answer to my question is out there; however, I do not know what this is called. So far I created 2 files, exeFunc.h and exeFunc.cpp. Their code consist of:
exeFunc.h
/*
File: exeFunc.h
Header file for exeFunc Library.
*/
#ifndef EXEFUNC_H
#define EXEFUNC_H
#include "mbed.h"
#include "msExtensions.h"
#include "cfExtensions.h"
#include <map>
class exeFunc
{
public:
exeFunc(msExtensions &msExt, cfExtensions &cfExt);
private:
void _splitFuncFromCmd();
void _attachCallback();
msExtensions &_msExt;
cfExtensions &_cfExt;
//FunctionPointer _p;
};
#endif
exeFunc.cpp
/*
File: exeFunc.cpp
Execute functions in other Sensor libraries/classes
Constructor
*/
#include "mbed.h"
#include "ConfigFile.h"
#include "msExtensions.h"
#include "cfExtensions.h"
#include "exeFunc.h"
#include <map>
#include <string>
using namespace std;
exeFunc::exeFunc(msExtensions &msExt, cfExtensions &cfExt) : _msExt(msExt), _cfExt(cfExt)
{
//_cfExt.checkConfigForFirstStart();
//_p.attach(&_cfExt, &cfExtensions::checkConfigForFirstStart);
//_p.call();
}
void exeFunc::_splitFuncFromCmd()
{
}
void exeFunc::_attachCallback()
{
}
I wrote a completed example, may helps
class MyClass
{
public:
MyClass(int b)
:_b(b)
{
}
int Foo(int a)
{
return a * _b;
}
int _b;
};
typedef int (MyClass::*MFP)(int);
int get_result(MyClass* obj, MFP mfp)
{
int r = (obj->*mfp)(5); // 30
return r;
}
int _tmain(int argc, _TCHAR* argv[])
{
MFP mfp = &MyClass::Foo;
MyClass m(6);
get_result(&m, mfp);
return 0;
}
You call it by another function.if you have an independent function.
To be honesty your question is not completely clear.However :
int F(int,int,int);
int g();
//main scope
F(g(),a,b)