schedule an event with std::cout as an argument to callback function failed

193 views
Skip to first unread message

JTL

unread,
Aug 24, 2016, 8:06:29 PM8/24/16
to ns-3-users
Hi,

I modified mesh.cc to try to pass std::cout to the scheduler as the argument to the call back function. The code failed to compile with the error messages:

[1909/2606] Compiling src/mesh/examples/mesh.cc
../src/mesh/examples/mesh.cc: In member function ‘int MeshTest::Run()’:
../src/mesh/examples/mesh.cc:228:74: error: lvalue required as unary ‘&’ operand
   Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report(std::cout), this);
                                                                          ^
../src/mesh/examples/mesh.cc: In member function ‘void MeshTest::Report(std::ostream&)’:
../src/mesh/examples/mesh.cc:242:84: error: ‘std::ostream’ has no member named ‘str’
       std::cerr << "Printing mesh point device #" << n << " diagnostics to " << os.str () << "\n";
                                                                                    ^
../src/mesh/examples/mesh.cc:244:19: error: ‘std::ostream’ has no member named ‘str’
       of.open (os.str ().c_str ());
                   ^
../src/mesh/examples/mesh.cc:247:56: error: ‘std::ostream’ has no member named ‘str’
           std::cerr << "Error: Can't open file " << os.str () << "\n";

The following changes are made to mesh.cc:

line 102: void Report (std::ostream & os);
line 228: Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report(std::cout), this);
line 235: MeshTest::Report (std::ostream &os)
comment out line 240


as oppose to the original lines:

line 102: void Report();
line 228: Simulator::Schedule (Seconds(m_totalTime), &MeshTest::Report(), this);
line 235: MeshTest::Report()

The original mesh.cc can be access here: https://www.nsnam.org/doxygen-release/mesh_8cc_source.html

Please let me know what I have done wrong and/or how to pass std::cout as an argument to the callback function for Scheduler. Thanks

JTL

Konstantinos

unread,
Aug 25, 2016, 3:55:53 AM8/25/16
to ns-3-users
Hi,

Peter was probably confused and posted the solution to another thread.. (https://groups.google.com/d/msg/ns-3-users/4t2vwE3ldIg/z5vv1S8QBgAJ)

I can help weigh the first error. It should be
Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this, std::cout);
However, the second error will still occur. Are you sure &MeshTest::Report() can write directly to std::cout?  Check the usage in the examples.
Peter

pdbarnes

unread,
Aug 25, 2016, 9:41:10 AM8/25/16
to ns-3-users
Whoops! Don't offer to help late at night!

JTL

unread,
Aug 25, 2016, 12:48:14 PM8/25/16
to ns-3-users
Hi, Peter,

Thank you very much for your help. You are right about the MeshTest::Report() is not able to handle the std::cout. I had to comment out most of the function body to avoid errors when compile. 

I changed the code according to your suggestion but it seems that the compiler invokes the wrong template:  Schedule(const ns3::Time&, MEM, OBJ, T1) instead of Schedule(const ns3::Time&, void(*)(U1), OBJ, T1) or something similar to this. On top of that, the copy constructor is called when passing std::cout as the 4th argument. and the following error message popped up. Could you help take a look into the problem? I have attached my version of the mesh.cc for your reference.

Here are the error messages:

In file included from /usr/include/c++/4.8/ios:42:0,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from ./ns3/fatal-error.h:24,
                 from ./ns3/abort.h:24,
                 from ./ns3/core-module.h:10,
                 from ../src/mesh/examples/mesh.cc:47:
/usr/include/c++/4.8/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.8/bits/ios_base.h:792:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
     ios_base(const ios_base&);
     ^
In file included from /usr/include/c++/4.8/ios:44:0,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from ./ns3/fatal-error.h:24,
                 from ./ns3/abort.h:24,
                 from ./ns3/core-module.h:10,
                 from ../src/mesh/examples/mesh.cc:47:
/usr/include/c++/4.8/bits/basic_ios.h:66:11: error: within this context
     class basic_ios : public ios_base
           ^
In file included from /usr/include/c++/4.8/iostream:39:0,
                 from ./ns3/fatal-error.h:24,
                 from ./ns3/abort.h:24,
                 from ./ns3/core-module.h:10,
                 from ../src/mesh/examples/mesh.cc:47:
/usr/include/c++/4.8/ostream: In copy constructor ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’:
/usr/include/c++/4.8/ostream:58:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here 
     class basic_ostream : virtual public basic_ios<_CharT, _Traits>
           ^
../src/mesh/examples/mesh.cc: In member function ‘int MeshTest::Run()’:
../src/mesh/examples/mesh.cc:228:81: note: synthesized method ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’ first required here 
   Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this, std::cout);
                                                                                 ^
In file included from ./ns3/event-garbage-collector.h:25:0,
                 from ./ns3/core-module.h:29,
                 from ../src/mesh/examples/mesh.cc:47:
./ns3/simulator.h:1224:9: error:   initializing argument 4 of ‘static ns3::EventId ns3::Simulator::Schedule(const ns3::Time&, MEM, OBJ, T1) [with MEM = void (MeshTest::*)(std::basic_ostream<char>&); OBJ = MeshTest*; T1 = std::basic_ostream<char>]’
 EventId Simulator::Schedule (Time const &delay, MEM mem_ptr, OBJ obj, T1 a1) 

Thank you very much!

JT
mesh.cc

pdbarnes

unread,
Aug 26, 2016, 4:33:17 PM8/26/16
to ns-3-users
The error message has the hint you need:

error:   initializing argument 4 of 
static ns3::EventId ns3::Simulator::Schedule(const ns3::Time&, MEM, OBJ, T1)
[with MEM = void (MeshTest::*)(std::basic_ostream<char>&); OBJ = MeshTest*; T1 = std::basic_ostream<char>]

The argument to the MeshTest::Report should be a reference "&":

Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this, &std::cout);


HTH,
Peter

JTL

unread,
Aug 28, 2016, 3:37:26 PM8/28/16
to ns-3-users
Hi Peter,

Thank you very much for your feedback. I did what you suggested and the error message below popped up

In file included from ./ns3/simulator.h:26:0,
                 from ./ns3/event-garbage-collector.h:25,
                 from ./ns3/core-module.h:29,
                 from ../src/mesh/examples/mesh.cc:47:
./ns3/make-event.h: In instantiation of ‘void ns3::MakeEvent(MEM, OBJ, T1)::EventMemberImpl1::Notify() [with MEM = void (MeshTest::*)(std::basic_ostream<char>&); OBJ = MeshTest*; T1 = std::basic_ostream<char>*]’:
./ns3/make-event.h:358:3:   required from ‘ns3::EventImpl* ns3::MakeEvent(MEM, OBJ, T1) [with MEM = void (MeshTest::*)(std::basic_ostream<char>&); OBJ = MeshTest*; T1 = std::basic_ostream<char>*]’
./ns3/simulator.h:1226:56:   required from ‘static ns3::EventId ns3::Simulator::Schedule(const ns3::Time&, MEM, OBJ, T1) [with MEM = void (MeshTest::*)(std::basic_ostream<char>&); OBJ = MeshTest*; T1 = std::basic_ostream<char>*]’
../src/mesh/examples/mesh.cc:228:82:   required from here
./ns3/make-event.h:353:7: error: invalid initialization of reference of type ‘std::basic_ostream<char>&’ from expression of type ‘TypeTraits<std::basic_ostream<char>*>::ReferencedType {aka std::basic_ostream<char>*}’
       (EventMemberImplObjTraits<OBJ>::GetReference (m_obj).*m_function)(m_a1);
       ^
&std::cout seems to be treated as pointer by the compiler. This is puzzling to me as this simple C++ code snippet works just fine:

#include <iostream>
using namespace std;
class myClass{
public:
  void  myPrint(ostream& os)
    {
        os<<"test";
    };
};
int main() {
myClass r;
r.myPrint(cout);
return 0;
}

Don't know how to intepret this.... any feedback is highly appreciated. Thank you very much.

JT

Craig Dowell

unread,
Aug 30, 2016, 1:28:22 AM8/30/16
to ns-3-...@googlegroups.com
Look for OutputStreamWrapper in the documentation.
--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, 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 https://groups.google.com/group/ns-3-users.
For more options, visit https://groups.google.com/d/optout.

JTL

unread,
Aug 30, 2016, 3:51:53 PM8/30/16
to ns-3-users
Hi Craig, 

It works perfectly with OutputStreamWrapper. Thanks

JT
Reply all
Reply to author
Forward
0 new messages