Issue:
I am trying to instantiate a class (say class A) inside another class (say class B) and then call all the member functions of class A inside the class B for the instantiated object. I get the following error message for every call of class A methods inside class B:
error: invalid use of non-static member function
I am aware of the following thread about the same problem. Invalid use of non-static member function c++. But I don't find that solution relevant to my problem. I am elaborating the problem below. Can you please enlighten me what is going wrong ?
Code:
The following are my classes:
Navigation.h
class Navigation {
public:
Navigation() {
laserData = n.subscribe("/scan", 200, &Navigation::laserCallback, this);
velPub = n.advertise<geometry_msgs::Twist> ("/mobile_base/commands/velocity", 100, this);
}
~Navigation() {}
geometry_msgs::Twist moveCommand();
geometry_msgs::Twist turnCommand();
geometry_msgs::Twist stopCommand();
void laserCallback(const sensor_msgs::LaserScan::ConstPtr& data);
float getObstacleRange();
void broadcastVelocity(geometry_msgs::Twist velocity);
private:
ros::NodeHandle n;
ros::Publisher velPub;
ros::Subscriber laserData;
float obstacleRange;
ros::Timer normalTurnTimer;
ros::Timer driveTimer;
ros::Timer periodicTurnTimer;
geometry_msgs::Twist drivePower;
};
Navigation.cpp
geometry_msgs::Twist Navigation::moveCommand() {
return drivePower;
}
geometry_msgs::Twist Navigation::turnCommand() {
return drivePower;
}
geometry_msgs::Twist Navigation::stopCommand() {
return drivePower;
}
void Navigation::laserCallback(const sensor_msgs::LaserScan::ConstPtr& data) {
obstacleRange = 0.8;
}
float Navigation::getObstacleRange() {
return obstacleRange;
}
void Navigation::broadcastVelocity(geometry_msgs::Twist velocity) {
velPub.publish(velocity);
}
Turtlebot.h
class Turtlebot {
public:
Turtlebot() {
Navigation nomad;
}
~Turtlebot() {}
void drive();
private:
Navigation nomad;
};
Turtlebot.cpp
void Turtlebot::drive() {
if (nomad.getObstacleRange() < 0.7) {
nomad.broadcastVelocity(nomad.stopCommand);
int i;
while (i < 5) {
nomad.broadcastVelocity(nomad.turnCommand);
i++;
}
} else nomad.broadcastVelocity(nomad.moveCommand);
}
Errors:
I get the following errors during cmake .. && make
/home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp: In member function ‘void Turtlebot::drive()’:
/home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp:37:50: error: invalid use of non-static member function
nomad.broadcastVelocity(nomad.stopCommand);
^
/home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp:40:54: error: invalid use of non-static member function
nomad.broadcastVelocity(nomad.turnCommand);
^
/home/arun/Documents/catkin_ws/src/TinyNomad/src/Turtlebot.cpp:43:53: error: invalid use of non-static member function
} else nomad.broadcastVelocity(nomad.moveCommand);
^
TinyNomad/CMakeFiles/tinynomad.dir/build.make:110: recipe for target 'TinyNomad/CMakeFiles/tinynomad.dir/src/Turtlebot.cpp.o' failed
make[2]: *** [TinyNomad/CMakeFiles/tinynomad.dir/src/Turtlebot.cpp.o] Error 1
CMakeFiles/Makefile2:1191: recipe for target 'TinyNomad/CMakeFiles/tinynomad.dir/all' failed
make[1]: *** [TinyNomad/CMakeFiles/tinynomad.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j12 -l12" failed
start with fixing your constructor:
Turtlebot() {
nomad = Navigation() ;
}
Related
I am facing problem while running gtest for the following code sample.
ignore header includes as its compilable and running fine.
Error:
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: receive(0x7ffcee4fc990, 0x7ffcee4fc900)
Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen. Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details.
/data/home/sipadhy/unit_test_research/gTest/ImplClassTest.cpp:174: Failure
Actual function call count doesn't match EXPECT_CALL(mockImplClass, receive(_, _))...
Expected: to be called at least once
Actual: never called - unsatisfied and active
Sample Code:
// Main Class where function to be mocked
class ImplClass
{
public:
virtual int receive(structX* x, structY* y){ // some logic }
};
// An intermidiate class which calls the main class
class IntermidiateClass
{
std::shared_ptr<ImplClass> implClassPtr = nullptr;
public:
setImplClassptr(std::shared_ptr<ImplClass> ptr)
{
implClassPtr = ptr;
}
int getValue()
{
structX x;
structY y;
return(implClassPtr->receive(x, y));
}
};
// Mock Class
class MockImplClass: public ImplClass
{
public:
MOCK_METHOD2(receive, int(structX, structY));
}
// Test case
TEST(MyTest, TEST1)
{
MockImplClass mockImplClass;
IntermidiateClass intermidiateObj;
intermidiateObj.setImplClassptr(std::make_shared<MockImplClass>());
EXPECT_CALL(mockImplClass, receive(_, _))
.Times(AtLeast(1))
.WillRepeatedly(Return(1));
int retVal = intermidiateObj.getValue();
}
Thanks,
Siva
You create a brand new object of the MockImplClass class here:
std::make_shared<MockImplClass>()
Thus your first created object
MockImplClass mockImplClass;
never gets used to call receive()
I have the following base class:
class node_layer_manager_t : public layer_manager_t
{
protected:
//Devices
trx_t trx;
private:
std::vector<string> trx_dump_labels;
public:
node_layer_manager_t( xml::node_t& params );
~node_layer_manager_t();
virtual bool set_profile(void) override;
}
I created the following derived class:
class node_layer_manager_with_rad_t : public node_layer_manager_t
{
protected:
//Devices
radio_t radio;
public:
node_layer_manager_with_rad_t(xml::node_t& params );
~node_layer_manager_with_rad_t();
virtual bool set_profile(void) override;
virtual void radio_monitoring_job_function(void);
intervalues_t<double> radio_tmp;
ushort duration_seconds_for_radio_monitoring;
};
I want it so that the set profile will execute the set_profile of the base class and in addition some other action.
Can I just write it this way?
bool node_layer_manager_with_rad_t::set_profile(void)
{
bool success;
node_layer_manager_t::set_profile();
try
{
string_t profile_tag = "logs/trx_dump/node:"+get_id();
dev_tx = profile->get_decendant(profile_tag.c_str());
cout<<"sarit id= "<< get_id()<<endl;
success = true;
}
catch(...)
{
cout<<"sarit profile error: "<<endl;
success = false;
}
return success; //**
}
**Or should I reurn the follwing:
return (success && node_layer_manager_t::set_profile());
If you have to call parent set_profile regardless what you have to do in derived class, you should adopt design which take care about this constraint.
Typically, you should mark based class set_porfile as final and manage call of a dedicated derived class method inside based class:
class node_layer_manager_t : public layer_manager_t
{
protected:
....
// set_profile actions of derived class
// proposed a default without side effect implementation if
// derived class doesn't need to overload this.
virtual bool set_profile_child() { return true; };
private:
....
public:
.....
// Manage here call of derived
virtual bool set_profile() override final
{
// actions before derived specific actions
....
// Call specific derived class actions
bool success = set_profile_child();
// actions after derived specific actions
if (success)
{
//do based class action
}
return success;
}
}
and in child:
class node_layer_manager_with_rad_t : public node_layer_manager_t
{
protected:
....
public:
virtual bool set_profile_child() override;
};
// Manage only there own action, regardless of needs of based class
bool node_layer_manager_with_rad_t::set_profile(void)
{
try
{
// Do what you're in charge, and only what you're in charge!
}
catch(...)
{
cout<<"sarit profile error: "<<endl;
success = false;
}
return success; //**
}
With this kind of design, each class do only what it have to manage, and only its. Derived class doesn't have to deal with needs of based class.
If you want to offer to your derived class ability to decided if code is executed before or after generic behavior, you can replace or add to set_profile_child() two methods: bool pre_set_profile() and bool post_set_profile()
At first, you haven't declared success anywhere (so actually, this is not a mcve, the code should not compile as is).
Still I get it - and tThe answer is: it depends on what you actually want to do...
Do you want to call the super class first or after the sub class code? Your example implies the former, your alternative the latter. Do you want to abort if the super class function fails or still execute your code?
Your inital example calls the super class function, ignores the result and does its own stuff afterwards.
This calls the super class function first and continues only on success:
bool success = node_layer_manager_t::set_profile();
if(success)
{
try { /*...*/ } // <- no need to set success to true, it is already
catch(...) { /*...*/ success = false; }
}
This executes both, but combines the result:
bool success = node_layer_manager_t::set_profile();
try { /*...*/ } // <- do not modify success, must remain false if super class failed!
catch(...) { /*...*/ success = false; }
Your alternative hints to executing the sub class code first and only call the super class function, if nothing went wrong.
Any of these approaches might be appropriate, none of them might be. You have to get a clear image of what your requirements are - and then implement the code such that your needs are satisfied...
I'm just trying to convert my code from C# to Haxe NME. I use enums as flags.
[Flags]
enum State
{
StateOne = 1,
StateTwo = 2,
StateThree = 4
}
And use it
if (someObj.HasState(State.StateOne | State.StateTwo))
{
// Contains both the states. Do something now.
}
I had no idea on how to do this in Haxe NME.
Thanks.
In Haxe 3, there is haxe.EnumFlags. This uses Haxe 3 Abstract Types which basically wrap an underlying type, in this case, it uses an Int, just like you have done - but then it wraps it up in a pretty API so you don't have to worry about the details.
Here is some sample code:
import haxe.EnumFlags;
class EnumFlagTest
{
static function main()
{
var flags = new EnumFlags<State>();
flags.set(StateOne);
flags.set(StateTwo);
flags.set(StateThree);
flags.unset(StateTwo);
if (flags.has(StateOne)) trace ("State One active");
if (flags.has(StateTwo)) trace ("State Two active");
if (flags.has(StateThree)) trace ("State Three active");
if (flags.has(StateOne) && flags.has(StateTwo)) trace ("One and Two both active");
if (flags.has(StateOne) && flags.has(StateThree)) trace ("One and Three both active");
}
}
enum State
{
StateOne;
StateTwo;
StateThree;
}
All of this works is stored as a standard Int, and uses integer operators like you have done, so it should be pretty fast (no wrapping in an external object). If you want to see how it works under the box, the source code for EnumFlags can be viewed here.
If you're still on Haxe 2, then you could create an object that is really similar, but of course, it has to create an object as well as the integer, so if you're doing thousands (millions?) of them then you might get a slow down. The equivalent code, that should work with Haxe 2 (though I haven't checked):
class MyEnumFlags<T:EnumValue>
{
var i:Int;
public function new(?i=0)
{
this.i = i;
}
public inline function has( v : T ) : Bool {
return i & (1 << Type.enumIndex(v)) != 0;
}
public inline function set( v : T ) : Void {
i |= 1 << Type.enumIndex(v);
}
public inline function unset( v : T ) : Void {
i &= 0xFFFFFFF - (1 << Type.enumIndex(v));
}
public inline static function ofInt<T:EnumValue>( i : Int ) : MyEnumFlags<T> {
return new MyEnumFlags<T>(i);
}
public inline function toInt() : Int {
return i;
}
}
I've managed to find it. I had trouble using enums but I had been successful using constants. This is the simple test file I used.
package ;
class FlagsTest
{
static inline var FLG_1:Int = 1;
static inline var FLG_2:Int = 2;
public static function main() : Void
{
var flag:Int = FLG_1;
if (hasFlag(flag, FLG_1))
{
trace ("Test 1 passed");
}
flag |= FLG_2;
if (hasFlag(flag, FLG_2))
{
trace ("Test 2 passed");
}
}
public static function hasFlag( flags:Int, flag:Int ) : Bool
{
return ((flags & flag) == flag) ? true : false;
}
}
Output:
FlagsTest.hx line 14: Test 1 passed
FlagsTest.hx line 19: Test 2 passed
first, some definitions from /usr/share/vala/vapi/libarchive.vapi
[CCode (cprefix="ARCHIVE_", lower_case_cprefix="archive_", cheader_filename = "archive.h")]
namespace Archive {...
[CCode (instance_pos = 1.9, cname="archive_open_callback")]
public delegate int OpenCallback (Archive archive);
...
[CCode (cname = "struct archive", free_function="archive_write_finish")]
public class Write : Archive {...
public Result open (
[CCode (delegate_target_pos = 0.9)] OpenCallback ocb,
[CCode (delegate_target_pos = 0.9)] WriteCallback rcb,
[CCode (delegate_target_pos = 0.9)] CloseCallback ccb
);
}
}
then my code
int mOpenCallback (Write archive)
{
return Result.OK;
}
void foo(){...
Write archive = new Write();
archive.open(mOpenCallback,mWriteCallback,mCloseCallback);
...}
results : error: Assignment: Cannot convert from mOpenCallback' toArchive.OpenCallback'
what's wrong with it ?
then I tried this instead
namespace Archive {
int OpenCallback (Write archive)
{
return Result.OK;
}
}
void foo(){...
Write archive = new Write();
archive.open(OpenCallback,mWriteCallback,mCloseCallback);
...}
but it outputs : Archive' already contains a definition forOpenCallback'
void foo(){...
Write archive = new Write();
Archive.OpenCallback t = mOpenCallback;
archive.open(t,mWriteCallback,mCloseCallback);
...}
outputs error: Assignment: Cannot convert from foo.mOpenCallback' toArchive.OpenCallback'
mOpenCallback has takes an argument of type Write, not Archive, so it has a different type from the delegate OpenCallback. If you expect Write, you should perform the cast from Archive to write inside your call back.
I wrote a class method using STL find_if. The code is the following:
void
Simulator::CommunicateEvent (pEvent e)
{
pwEvent we (e);
std::list<pEvent> l;
for (uint32_t i = 0; i < m_simulatorObjects.size (); i++)
{
l = m_simulatorObjects[i]->ProcessEvent (we);
// no action needed if list is empty
if (l.empty ())
continue;
// sorting needed if list comprises 2+ events
if (l.size () != 1)
l.sort (Event::Compare);
std::list<pEvent>::iterator it = m_eventList.begin ();
std::list<pEvent>::iterator jt;
for (std::list<pEvent>::iterator returnedElementIt = l.begin ();
returnedElementIt != l.end ();
returnedElementIt++)
{
// loop through the array until you find an element whose time is just
// greater than the time of the element we want to insert
Simulator::m_eventTime = (*returnedElementIt)->GetTime ();
jt = find_if (it,
m_eventList.end (),
IsJustGreater);
m_eventList.insert (jt, *returnedElementIt);
it = jt;
}
}
}
Unfortunately, I later discovered that the machine that will run the code is equipped with the libstdc++ library version 4.1.1-21, which apparently is lacking find_if. Needless to say, I cannot upgrade the library, nor can I ask someone to do it.
When compiling, the error I get is:
simulator.cc: In member function ‘void sim::Simulator::CommunicateEvent(sim::pEvent)’:
simulator.cc:168: error: no matching function for call to ‘find_if(std::_List_iterator<boost::shared_ptr<sim::Event> >&, std::_List_iterator<boost::shared_ptr<sim::Event> >, sim::Simulator::<anonymous struct>&)’
simulator.cc: In static member function ‘static void sim::Simulator::InsertEvent(sim::pEvent)’:
simulator.cc:191: error: no matching function for call to ‘find_if(std::_List_iterator<boost::shared_ptr<sim::Event> >&, std::_List_iterator<boost::shared_ptr<sim::Event> >, sim::Simulator::<anonymous struct>&)’
make: *** [simulator.o] Error 1
How can I solve the problem?
I thought I could define a find_if function as described here. However, I have some concerns:
What about performance? The function that makes use of find_if needs to be as efficient as possible.
How can I do conditional compilation? I couldn't find a macro telling the version of the libstdc++ installed.
What are your thoughts about it?
TIA,
Jir
References
Source files: simulator.h and simulator.cc
Solution
Defined IsJustGreater outside the Simulator class and declared IsJustGreater_s friend of Simulator:
struct IsJustGreater_s : public std::unary_function<const pEvent, bool> {
inline bool operator() (const pEvent e1) {return (e1->GetTime () > Simulator::m_eventTime);}
} IsJustGreater;
Called IsJustGreater in find_if this way:
jt = find_if (it, m_eventList.end (), sim::IsJustGreater);
From the error, it appears that you're attempting to use an anonymous type as the argument. I do not believe anonymous types are allowed to be template arguments.
From the error, I believe you have something like this:
class Simulator {
struct {
bool operator(const pEvent& p) { ... } ;
} IsJustGreater;
}
what you want is to give it a name and then change the find_if to instantiate the class (see below)
class Simulator {
// class is now an inner named-class
struct IsJustGreater {
bool operator(const pEvent& p) { ... } ;
};
}
// This is how you use the class
jt = std::find_if(it, m_eventList.end(), IsJustGreater() );
I see that you're using the std:: qualifier before std::list but not std::find_if. Try putting the std:: in front so that the compiler can find it within the namespace.