Static non static member functions, what am i doing wrong

480 views
Skip to first unread message

feroz...@gmail.com

unread,
Nov 27, 2013, 3:48:03 AM11/27/13
to ns-3-...@googlegroups.com
Hey everyone,

Within a class function am trying to schedule another function this way;

void TestOnOffM()
{
...
....
Simulator::Schedule (Seconds (5), &update,ipv41, ipv4ifIndex1);  //ipv41 is a pointer to a node, and ipv4ifindex1 is an interface of it which has failed
}

*Now if i place the definition of update function outside the class at the top, i get errors during build.
*If i place it within the class, i get this error;

../scratch/now.cc: In member function ‘void FATTopology::TestOnOffM()’:
../scratch/now.cc:605: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say ‘&FATTopology::update

In file included from ./ns3/core-module.h:42,
                 from ../scratch/now.cc:57:
./ns3/make-event.h: In member function ‘void ns3::MakeEvent(MEM, OBJ, T1)::EventMemberImpl1::Notify() [with MEM = void (FATTopology::*)(ns3::Ptr<ns3::Ipv4>, uint32_t), OBJ = ns3::Ptr<ns3::Ipv4>, T1 = unsigned int]’:
../scratch/now.cc:942:   instantiated from here
./ns3/make-event.h:124: error: pointer to member type ‘void (FATTopology::)(ns3::Ptr<ns3::Ipv4>, uint32_t)’ incompatible with object type ‘ns3::Ipv4’


*if i try to include it outside the class at the end to get rid of building errors, i still get the above errors,
What should i try?


Message has been deleted

Feroza Naseem

unread,
Dec 21, 2013, 8:43:57 AM12/21/13
to ns-3-...@googlegroups.com
hey peter,

thanks for the response.
well i figured out what i am doing wrong, and tried
Simulator::Schedule (Seconds (5), &FATTopology::update, ipv41, ipv4ifIndex1);

but i got the error as follow

:pointer to member type ‘void (FATTopology::)(ns3::Ptr<ns3::Ipv4>, uint32_t)’ incompatible with object type ‘ns3::Ipv4’

now as per my understanding of the usage of definition of the schedule function i am supposed to use an object of FATTopology here, but i am already using one in main which actually calling all these functions.

Still if i use an object 'ft' of the class FATTopology, as FATTopology ft;
Simulator::Schedule (Seconds (5), &FATTopology::update,ft, ipv41, ipv4ifIndex1);

i get this error,
error: incomplete type ‘ns3::EventMemberImplObjTraits<FATTopology>’ used in nested name specifier




On Fri, Dec 20, 2013 at 3:06 AM, pdbarnes <pd...@mac.com> wrote:
Sounds like you have the class method FATTopology::update() declared, which is hiding your definition of update outside the class, at the top or bottom of the file.  If you want to use the class method, do what the error message says:

Simulator::Schedule (Seconds (5), &FATTopology::update, ipv41, ipv4ifIndex1);

If you want the free function, try

Simulator::Schedule (Seconds (5), &::update, ipv41, ipv4ifIndex1);

Peter

On Wednesday, November 27, 2013 12:48:03 AM UTC-8, feroz...@gmail.com wrote:
Simulator::Schedule (Seconds (5), &update,ipv41, ipv4ifIndex1);  //ipv41 is a pointer to a node, and ipv4ifindex1 is an interface of it which has failed

../scratch/now.cc: In member function ‘void FATTopology::TestOnOffM()’:
../scratch/now.cc:605: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say ‘&FATTopology::update

In file included from ./ns3/core-module.h:42,
                 from ../scratch/now.cc:57:
./ns3/make-event.h: In member function ‘void ns3::MakeEvent(MEM, OBJ, T1)::EventMemberImpl1::Notify() [with MEM = void (FATTopology::*)(ns3::Ptr<ns3::Ipv4>, uint32_t), OBJ = ns3::Ptr<ns3::Ipv4>, T1 = unsigned int]’:
../scratch/now.cc:942:   instantiated from here
./ns3/make-event.h:124: error: pointer to member type ‘void (FATTopology::)(ns3::Ptr<ns3::Ipv4>, uint32_t)’ incompatible with object type ‘ns3::Ipv4’


*if i try to include it outside the class at the end to get rid of building errors, i still get the above errors,
What should i try?


--
You received this message because you are subscribed to a topic in the Google Groups "ns-3-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ns-3-users/qzgPr0kKY3U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ns-3-users+...@googlegroups.com.
To post to this group, send email to ns-3-...@googlegroups.com.
Visit this group at http://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/groups/opt_out.

Tommaso Pecorella

unread,
Dec 21, 2013, 11:12:22 AM12/21/13
to ns-3-...@googlegroups.com
Hi,

without the full code of the FATTopology class it's hard to give a definitive answer, however we can guess what's going on.

The call Simulator::Schedule (Seconds (5), &FATTopology::update, ipv41, ipv4ifIndex1);
is valid if FATTopology::update is a static function, i.e., a static member function. See for example http://www.learncpp.com/cpp-tutorial/812-static-member-functions/

If the function isn't static, the above call will not work. The correct call is:
Simulator::Schedule (Seconds (5), &FATTopology::update, *pointer to the object*, ipv41, ipv4ifIndex1);
Where *pointer to the object* is the pointer to the object of type FATTopology you're using.

As a matter of fact, the Simulator::Schedule can be confusing, but it's quite logical once you understand it.
1st argument: the function to call
2nd argument: the pointer to the object instance to call. <- this is omitted if the function is static or it isn't a member function.
Other arguments: the arguments to pass to the called function.

Hope this helps,

T.

Feroza Naseem

unread,
Dec 21, 2013, 11:43:14 AM12/21/13
to ns-3-...@googlegroups.com
Thanks Peter and Tommaso.

I tried playing around with the pointer to the class object and then including 'this' has worked for me and i am able to access the update function this way,

Simulator::Schedule (Seconds (5), &FATTopology::update, this, ipv41, ipv4ifIndex1);


Thanks again for your responses.
Reply all
Reply to author
Forward
0 new messages