cannot call member function

511 views
Skip to first unread message

pwj

unread,
Nov 2, 2015, 3:47:34 AM11/2/15
to ns-3-users
Hi,

When I use "OnOffApplication::SendPacket()" to send a packet, the result shows as follows:
error: cannot call member function 'void ns3::OnOffApplication::SendPacket()' without object

Can someone answer my question?

Thanks

Konstantinos

unread,
Nov 2, 2015, 3:57:08 AM11/2/15
to ns-3-users
Hi,

Please study a C++/OOP primer to understand more about classes/objects.
Also, you can search before you ask, most of the times you can find the solution out there.
e.g. http://stackoverflow.com/questions/1127738/cannot-call-member-function-without-object

Furthermore, even if you create an object and try to call SendPacket() on that, you would get another error saying that the method is private.
Again by studying a C++ book you would find out that in order to use/call private member functions, you have to be 'inside' that class.

Regards,
K

pwj

unread,
Nov 8, 2015, 4:06:20 PM11/8/15
to ns-3-users
Hi,

I've modified the code as follows:


class OnOffApplication
{
  public:
    void SendPacket ();
};

void OnOffApplication::SendPacket ()
{
  std::cout << "Send a packet." << std::endl;
}

int main (int argc, char *argv[])
{

...

OnOffApplication application;
application.SendPacket ();

...
}

The result shows like that:

Waf: Entering directory `/root/ns3/ns-allinone-3.22/ns-3.22/build'
[2083/2427] cxx: examples/wireless/wifi-sleep.cc -> build/examples/wireless/wifi-sleep.cc.18.o
../examples/wireless/wifi-sleep.cc:113:6: error: reference to ‘OnOffApplication’ is ambiguous
 void OnOffApplication::SendPacket ()
      ^
../examples/wireless/wifi-sleep.cc:107:7: note: candidates are: class OnOffApplication
 class OnOffApplication 
       ^
In file included from ./ns3/on-off-helper.h:31:0,
                 from ./ns3/applications-module.h:13,
                 from ../examples/wireless/wifi-sleep.cc:56:
./ns3/onoff-application.h:86:7: note:                 class ns3::OnOffApplication
 class OnOffApplication : public Application 
       ^
../examples/wireless/wifi-sleep.cc:113:6: error: reference to ‘OnOffApplication’ is ambiguous
 void OnOffApplication::SendPacket ()
      ^
../examples/wireless/wifi-sleep.cc:107:7: note: candidates are: class OnOffApplication
 class OnOffApplication 
       ^
In file included from ./ns3/on-off-helper.h:31:0,
                 from ./ns3/applications-module.h:13,
                 from ../examples/wireless/wifi-sleep.cc:56:
./ns3/onoff-application.h:86:7: note:                 class ns3::OnOffApplication
 class OnOffApplication : public Application 
       ^
../examples/wireless/wifi-sleep.cc: In function ‘int main(int, char**)’:
../examples/wireless/wifi-sleep.cc:1046:3: error: reference to ‘OnOffApplication’ is ambiguous
   OnOffApplication application;
   ^
../examples/wireless/wifi-sleep.cc:107:7: note: candidates are: class OnOffApplication
 class OnOffApplication 
       ^
In file included from ./ns3/on-off-helper.h:31:0,
                 from ./ns3/applications-module.h:13,
                 from ../examples/wireless/wifi-sleep.cc:56:
./ns3/onoff-application.h:86:7: note:                 class ns3::OnOffApplication
 class OnOffApplication : public Application 
       ^
../examples/wireless/wifi-sleep.cc:1046:20: error: expected ‘;’ before ‘application’
   OnOffApplication application;
                    ^
../examples/wireless/wifi-sleep.cc:1047:3: error: ‘application’ was not declared in this scope
   application.SendPacket ();
   ^
Waf: Leaving directory `/root/ns3/ns-allinone-3.22/ns-3.22/build'
Build failed

Please help to advise on it.

Thank you.



Konstantinos於 2015年11月2日星期一 UTC+8下午4時57分08秒寫道:

Konstantinos

unread,
Nov 8, 2015, 6:14:49 PM11/8/15
to ns-3-users
Hi, 

My previous comments still stand. Please study OOP to understand what you are doing.
The fact that you changed one private method to public does not solve the problem you have, and as you experienced, it creates more.
I would recommend not to change the source code unless absolutely necessary. Create your own application if OnOff (or other) does not meet your needs.
Also, by studying the tutorial you can see example applications such as "MyApp"

This part in your code 
OnOffApplication application;
application
.SendPacket ();
is totally wrong, as the application has not started yet and you can see what functionality does "StartApplication" performs by studying the documentation.

You need to understand how NS-3 works, what are "events' and scheduler, and all these are explained in the documentation. 
See the examples to understand how you setup OnOffApplication, e.g. /examples/wireless/multirate.cc

358  // Equipping the source node with OnOff Application used for sending
359  OnOffHelper onoff ("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address ("10.0.0.1"), port)));
360  onoff.SetConstantRate (DataRate (60000000));
361  onoff.SetAttribute ("PacketSize", UintegerValue (packetSize));
362  onoff.SetAttribute ("Remote", AddressValue (InetSocketAddress (ipv4AddrServer, port)));
363 
364  ApplicationContainer apps = onoff.Install (client);
365  apps.Start (Seconds (start));
366  apps.Stop (Seconds (stop));

The above code setup the OnOffApplication, and you configure when it will send a packet. Then after you call Simulator::Run(), the scheduler will call the "StartApplication" and it will start generating packets and call "SendPacket" automatically, there is no need to call it by yourself to send a packet. 

Finally, please update to the latest NS-3 release.

Regards,
K.
...
Reply all
Reply to author
Forward
0 new messages