Friend ostream operator << wont acces private fields - private

It just wont acces private fields
.hpp:
private:
std::string model;
public:
Piekarnik &operator << (const Ciasto &ciasto);
friend std::ostream &operator<<(std::ostream &os, const Ciasto &ciasto);
.cpp:
Piekarnik &Piekarnik::operator<<(const Ciasto &ciasto)
{
append(ciasto);
return *this;
}
ostream &operator<<(ostream &os, const Piekarnik &piekarnik) // dwuargumentowy;
{
os<<"Piekarnik (model: "<<piekarnik.model<<", temperatura: " << piekarnik.obecnyProgram.temperatura <<", pieczenie: " << EnumToString(piekarnik.obecnyProgram.pieczenie) << ", wlozone ciasto: " << EnumToString(piekarnik.wlozoneCiasto.rodzaj);
return os<<")";
}
error: 'std::string Piekarnik::model' is private

Change your friend declaration to
friend std::ostream &operator<<(std::ostream &os, const Piekarnik&);

Related

Populate a list with Modules and display it in a Dialogbox in DOORS

I need a DXL script that fill a DialogBox with returned Module names'. I have several modules within a project.
Below is my current progress. My idea is to put all Module names in a List and then display the list in a DialogBox.
Can you help me write ´´´fillModulelist()´´´
// This DXL script iterates through all formal modules of the folder
DB dbMain = null
DBE dbeModuleList = null
DBE dbeExport = null
DBE dbeExportPath = null
Folder currFolder = null
string startFolder="/Project/Folder";
int moduleCount=0;
void forAllModulesInFolder(Folder f)
{
Item itemRef;
string shType;
string sItemNameFull;
string sItemName;
Module moduleReference;
for itemRef in f do
{
shType = type(itemRef);
print shType "\t";
sItemNameFull = fullName(itemRef);
print sItemNameFull "\t";
sItemName = name(itemRef);
print sItemName "\n";
if(shType=="Folder")
{
string selectedFolder = sItemNameFull;
Folder f = folder selectedFolder;
forAllModulesInFolder(f);
}
if(shType=="Formal")
{
moduleReference = read(sItemNameFull,false,true);
filtering off;
// do s.th. with the moduleReference
close(moduleReference);
moduleCount++;
}
}}
void fillModuleList()
{
//........... HELP NEEDED........
}
// Main-Method
void main(void)
{
string selectedFolder = startFolder;
Folder f = folder selectedFolder;
forAllModulesInFolder(f);
print "Affected Modules: " moduleCount "\n";
}
main();
Any help provided I would be very thankful.
As the list of modules is for displaying it to the user (and perhaps let them select from that list), it is best to display the full name of the modules, thus I would not store the module reference. You can open the module later, when the user has selected/double clicked the module (supposing that this is what you want). Thus, I fill a Skip list with sItemNameFull, sItemNameFull, but if it suits your script better, you can also fill it with moduleReference, sItemNameFull (use create instead of createString in this case). The changes to your script are marked with //->> and //<<-
// This DXL script iterates through all formal modules of the folder
DB dbMain = null
DBE dbeModuleList = null
DBE dbeExport = null
DBE dbeExportPath = null
Folder currFolder = null
string startFolder="/testtrunk";
int moduleCount=0;
//->>
Skip skModules = createString()
//<<-
void forAllModulesInFolder(Folder f)
{
Item itemRef;
string shType;
string sItemNameFull;
string sItemName;
Module moduleReference;
for itemRef in f do
{
shType = type(itemRef);
print shType "\t";
sItemNameFull = fullName(itemRef);
print sItemNameFull "\t";
sItemName = name(itemRef);
print sItemName "\n";
if(shType=="Folder")
{
string selectedFolder = sItemNameFull;
Folder f = folder selectedFolder;
forAllModulesInFolder(f);
}
if(shType=="Formal")
{
//->>
put (skModules, sItemNameFull, sItemNameFull)
//moduleReference = read(sItemNameFull,false,true);
//filtering off;
// do s.th. with the moduleReference
//close(moduleReference);
//<<-
moduleCount++;
}
}}
//->>
void fillModuleList(Skip skContent, DBE dbeList)
{
empty dbeList
int cnt=0
string sLine
for sLine in skContent do {
insert (dbeList, cnt, sLine)
cnt++
}
}
//<<-
//->>
void DoSomethingWithDoubleClickedModule (DBE x) {
string sModName = get(x)
print "doing something with " sModName "<---\n"
}
void canvasDummyCB( DBE dummy ) { }
void doNothing(DBE x) {}
void prepareGui()
{
const string sArEmpty[] = {}
dbMain = create ("mytitle", styleCentered)
DBE spaceLeft = canvas(dbMain, 0, 0, canvasDummyCB)
spaceLeft->"top"->"form"; spaceLeft->"left"->"form"
spaceLeft->"right"->"unattached"; spaceLeft->"bottom"->"unattached"
DBE spaceRight = canvas(dbMain, 0, 0, canvasDummyCB)
spaceRight->"top"->"form"; spaceRight->"right"->"form"
spaceRight->"left"->"unattached"; spaceRight->"bottom"->"unattached"
DBE dInfoTextLabel = label(dbMain, "choose a module")
dInfoTextLabel->"top"->"form"
dInfoTextLabel->"left"->"flush"->spaceLeft
dInfoTextLabel->"right"->"flush"->spaceRight
dbeModuleList = list( dbMain, "Modules", 200, 15, sArEmpty)
dbeModuleList->"top"->"flush"->dInfoTextLabel
dbeModuleList->"left"->"flush"->spaceLeft
dbeModuleList->"right"->"flush"->spaceRight
realize dbMain
set( dbeModuleList, doNothing, DoSomethingWithDoubleClickedModule)
}
//<<-
// Main-Method
void main(void)
{
//->>
prepareGui()
//<<-
string selectedFolder = startFolder;
Folder f = folder selectedFolder;
setempty(skModules)
forAllModulesInFolder(f);
//->>
fillModuleList (skModules, dbeModuleList)
//<<-
print "Affected Modules: " moduleCount "\n";
}
main();

Linked list: stored elements are not displaying completely

I am trying to store 5 elements in linked, the input code is working fine but the display code is not showing the 5th element "RollNumber".
I can't find where I did the mistake.
class Node
{
friend class LinkedList;
private:
string BookName;
string BookNo;
string Price;
string StudentName;
string RollNumber;
Node *next;
public:
Node(string bname = "", string bno = "", string pr = "", string sname = "", string rn = "", Node *nxt = NULL)
{
BookName=bname;
BookNo=bno;
Price=pr;
StudentName=sname;
RollNumber:rn;
next=nxt;
}
};
class LinkedList
{
private:
Node *head;
Node *tail;
int count;
public:
LinkedList()
{
head = NULL;
tail = NULL;
count = 0;
}
bool isEmpty()
{
if (head == NULL)
{
return true;
}
return false;
}
void add_input_head(string bname = "", string bno="", string pr="", string sname="", string rn="")
{
if (isEmpty())
{
head = new Node(bname, bno, pr, sname, rn, NULL);
tail = head;
count++;
}
else
{
Node *p = new Node(bname, bno, pr, sname, rn, head);
head = p;
count++;
}
}
void traverse()
{
Node *t = head;
for (int i = 1; i <= count; i++) {
cout <<endl<<endl<<endl<<endl<<"Book Name: "<< t->BookName << endl;
cout <<endl<<endl<<"Book No: "<< t->BookNo << endl;
cout <<endl<<endl<<"Price: "<< t->Price << endl;
cout <<endl<<endl<<"Stuent Name: "<< t->StudentName << endl;
cout <<endl<<endl<<"Roll No: "<< t->RollNumber << endl;
t = t->next;
}
}
void Exit()
{
cout<<"\t\t\tThank You for using this Program";
}
//Destructor for Linked List
~LinkedList()
{
Node *t;
while (head != NULL)
{
t = head;
head = head->next;
delete t;
}
head = NULL;
count = 0;
}
};
int main()
{
LinkedList l;
int ch;
string a,b,c,d,e;
do
{
cout<<"\n\t\t\t Press 1 for Addition";
cout<<"\n\t\t\t Press 2 for Display";
cout<<"\n\t\t\t Press 3 for Exit";
cout<<"\n\t\t\t Enter Option=";
cin>>ch;
switch(ch)
{
case 1:
cout<<endl<<endl<<endl<<endl<<"Enter Book Name: ";
cin>>a;
cout<<endl<<"Enter Book No: ";
cin>>b;
cout<<endl<<"Enter Price: ";
cin>>c;
cout<<endl<<"Enter Student Name: ";
cin>>d;
cout<<endl<<"Enter Roll No: ";
cin>>e;
l.add_input_head(a,b,c,d,e);
break;
case 2:
l.traverse();
case 3:
l.Exit();
break;
}
}while(ch!=6);
}
There is no error while compiling but it still misses the last element. I don't know if the error is in input or output function.
I am making a class project and I am unable to resolve this issue.
Can somebody help me to find the mistake?

Is it right way to make text-protocol interpretation program?

I'm trying to make a program for parsing text protocol.
(I selected text protocol cause I heard that binary packet parsing is more difficult).
Currently, there are really few command and parameters.
each packet can be splited by delimiter(';')
[packet1];[packet2];
Let's break packet1 down.
[Action],[Param1],[Param2],...;
Action : [SET]
Params : [DELAY]
if you send "SET,DELAY,300;" to server,
server will change 'delay' parameter and send "SET,DELAY,300;" to client.
Action : [GET]
Params : [DELAY] [MODE]
if you send "GET,DELAY,MODE;" to server,
server will send "GET,DELAY,300,MODE,2;" to client.
Any way I suceed to make it.
(The code is here. because it is long, I couldn't add it here)
But even if there are only few params and actions, the code is very long and complicated.
I used 'boost::algorithm::split' to split packets.
And I only used 'if','else if','else' to invoke right task corresponding 'action' and 'parameter'.
But I will add more actions and parameters.
But at this rate, I cannot debug or modify code because the comlexity of the code will be more severe.
Is it wrong way to make protocol translation program?
If you know better way, please share with me.
Yes. The better way is to make a grammar, write a parser for it and parse into an AST (abstract syntax tree, or simply strong typed representation of the packets).
A Spirit grammar for this looks like:
I always start out with the AST types:
namespace ast {
struct nil {
friend std::ostream& operator<<(std::ostream& os, nil) { return os << "<nil>"; }
};
using value = boost::variant<nil, double, std::string>;
struct parameter {
std::string _key;
value _val;
};
enum class action {
get,
set,
};
using parameters = std::vector<parameter>;
struct packet {
action _action;
parameters _params;
};
using packets = std::vector<packet>;
}
For simplicity I've
assumed parameters (mode/delay) will have numeric or string values.
used the same packet definition for GET and SET requests (GET requests will just us nil values for the parameters listed)
Next we define a grammar using Boost Spirit Qi:
template <typename It, typename Skipper=qi::space_type>
struct grammar : qi::grammar<It, ast::packets(), Skipper> {
grammar():grammar::base_type(start) {
using qi::raw;
using qi::no_case;
param_key_.add
("delay")
("mode");
start = *(packet_ >> ';');
packet_ =
(no_case["get"] >> qi::attr(ast::action::get) >> *(',' >> get_param_))
| (no_case["set"] >> qi::attr(ast::action::set) >> *(',' >> set_param_))
;
get_param_ = raw[no_case[param_key_]] >> qi::attr(ast::nil());
set_param_ = raw[no_case[param_key_]] >> "," >> value_;
value_ = qi::double_ | string_;
string_ = '"' >> *~qi::char_('"') >> '"';
BOOST_SPIRIT_DEBUG_NODES((start)(packet_)(get_param_)(set_param_)(value_)(string_))
}
// ... field declarations
};
There's a little bit of a learning curve here, but the key point to observe is that it it's possible to create maintainable code that is also debuggable (see here for BOOST_SPIRIT_DEBUG enabled output).
Finally, because the AST is simple we can make a fake request processor that uses a request context (in this case a map to contain the current values of the parameters) to actually process the requests:
struct request_context {
std::map<std::string, ast::value> properties;
request_context()
: properties { { "MODE", 2 }, { "DELAY", 300 } } // defaults
{
}
boost::optional<ast::packet> process_request(ast::packet packet) {
switch (packet._action) {
case ast::action::get:
for(auto& param : packet._params) {
param._val = properties[param._key];
}
return packet;
case ast::action::set:
for(auto& param : packet._params) {
std::cout << "DEBUG: setting property '" << param._key << "' to value '" << param._val << "'\n";
properties[param._key] = param._val;
}
return boost::none;
default:
throw std::runtime_error("bad packet"); // TODO proper exception type
};
}
};
Imagine who much messier this was if you had it mixed with the parsing code, or everything stringly typed
Live On Coliru
//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <map>
namespace qi = boost::spirit::qi;
namespace ast {
struct nil {
friend std::ostream& operator<<(std::ostream& os, nil) { return os << "<nil>"; }
};
using value = boost::variant<nil, double, std::string>;
struct parameter {
std::string _key;
value _val;
};
enum class action {
get,
set,
};
using parameters = std::vector<parameter>;
struct packet {
action _action;
parameters _params;
};
using packets = std::vector<packet>;
static std::ostream& operator<<(std::ostream& os, action a) {
switch(a) {
case action::get: return os << "GET";
case action::set: return os << "SET";
}
return os << "(other)";
}
}
BOOST_FUSION_ADAPT_STRUCT(ast::parameter,(std::string,_key)(ast::value,_val))
BOOST_FUSION_ADAPT_STRUCT(ast::packet,(ast::action,_action)(ast::parameters,_params))
template <typename It, typename Skipper=qi::space_type>
struct grammar : qi::grammar<It, ast::packets(), Skipper> {
grammar():grammar::base_type(start) {
using qi::raw;
using qi::no_case;
param_key_.add
("delay")
("mode");
start = *(packet_ >> ';');
packet_ =
(no_case["get"] >> qi::attr(ast::action::get) >> *(',' >> get_param_))
| (no_case["set"] >> qi::attr(ast::action::set) >> *(',' >> set_param_))
;
get_param_ = raw[no_case[param_key_]] >> qi::attr(ast::nil());
set_param_ = raw[no_case[param_key_]] >> "," >> value_;
value_ = qi::double_ | string_;
string_ = '"' >> *~qi::char_('"') >> '"';
BOOST_SPIRIT_DEBUG_NODES((start)(packet_)(get_param_)(set_param_)(value_)(string_))
}
private:
qi::symbols<char, std::string> param_key_;
qi::rule<It, ast::parameter(), Skipper> set_param_, get_param_;
qi::rule<It, ast::packets(), Skipper> start;
qi::rule<It, ast::packet(), Skipper> packet_;
qi::rule<It, ast::value(), Skipper> value_;
qi::rule<It, std::string()> string_;
};
struct request_context {
std::map<std::string, ast::value> properties;
request_context()
: properties { { "MODE", 2 }, { "DELAY", 300 } } // defaults
{
}
boost::optional<ast::packet> process_request(ast::packet packet) {
switch (packet._action) {
case ast::action::get:
for(auto& param : packet._params) {
param._val = properties[param._key];
}
return packet;
case ast::action::set:
for(auto& param : packet._params) {
std::cout << "DEBUG: setting property '" << param._key << "' to value '" << param._val << "'\n";
properties[param._key] = param._val;
}
return boost::none;
default:
throw std::runtime_error("bad packet"); // TODO proper exception type
};
}
};
int main()
{
std::string const input =
"GET,DELAY,MODE;"
"SET,DELAY,0,MODE,\"we can have string values too\";GET,MODE;SET,MODE,42;GET,MODE,DELAY;";
using It = std::string::const_iterator;
It f(input.begin()), l(input.end());
grammar<It> p;
ast::packets parsed;
bool ok = qi::phrase_parse(f,l,p,qi::space,parsed);
if (ok) {
std::cout << parsed.size() << " packets successfully parsed\n";
request_context ctx;
for(auto& packet : parsed)
{
auto response = ctx.process_request(packet);
if (response) {
std::cout << "response: " << response->_action;
for(auto& kv : packet._params) {
std::cout << "," << kv._key << "," << kv._val;
}
std::cout << ";\n";
}
}
} else {
std::cout << "Parse error\n";
}
if (f!=l)
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
Prints:
5 packets successfully parsed
response: GET,DELAY,300,MODE,2;
DEBUG: setting property 'DELAY' to value '0'
DEBUG: setting property 'MODE' to value 'we can have string values too'
response: GET,MODE,we can have string values too;
DEBUG: setting property 'MODE' to value '42'
response: GET,MODE,42,DELAY,0;

Incoming sms listener on blackberry

i've used below code for notify the sms .
Its working on two blackberry simulator.
I've install the app on my device and send sms from android device.
The sms listener not working on device.
Incoming message received on device. but my app not notify the listener .
What is the problem how to resolve it.
What port number need to give for device?
class BackgroundApplication extends Application implements MessageListener
{
int i=0;
static String suffix;
MessageConnection _mc ;
public BackgroundApplication()
{
try {
_mc = (MessageConnection)Connector.open("sms://:0");
_mc.setMessageListener(this);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void notifyIncomingMessage(MessageConnection conn) {
try {
Message m = _mc.receive();
String address = m.getAddress();
String msg = null;
if ( m instanceof TextMessage )
{
TextMessage tm = (TextMessage)m;
msg = tm.getPayloadText();
}
else if (m instanceof BinaryMessage) {
StringBuffer buf = new StringBuffer();
byte[] data = ((BinaryMessage) m).getPayloadData();
// convert Binary Data to Text
msg = new String(data, "UTF-8");
}
else
System.out.println("Invalid Message Format");
System.out.println("Received SMS text from " + address + " : " + msg);
showDialog("Msg: "+msg);
} catch (Exception e) {
// TODO: handle exception
}
}
private void showDialog(String string) {
synchronized (UiApplication.getEventLock())
{
Status.show(""+string,Bitmap.getPredefinedBitmap(Bitmap.INFORMATION), 5000,
Status.GLOBAL_STATUS, true, false, 1);
}
}
}
Check this
http://supportforums.blackberry.com/t5/Java-Development/Different-ways-to-listen-for-SMS-messages/ta-p/445062
DatagramConnection _dc =
(DatagramConnection)Connector.open("sms://");
for(;;)
{
Datagram d = _dc.newDatagram(_dc.getMaximumLength());
_dc.receive(d);
byte[] bytes = d.getData();
String address = d.getAddress();
String msg = new String(bytes);
System.out.println( "Received SMS text from " + address + " : " + msg);
}

How to store a Lua function into C++ with SLB ( Simple Lua Binder )?

I need to store a Lua function using the library "Simple Lua Binder"
typedef ? TLuaFunction;
class Foo
{
public:
void SetCallback( TLuaFunction f )
{
mCallback = f;
}
void ExecuteCallback()
{
f(); // Calls Lua function
}
private:
TLuaFunction mCallback;
};
// Manager initialization
SLB::Manager scriptManager;
// SetCallback registration into lua
SLB::Class<Foo>( "Foo", &scriptManager )
.constructor()
.set( "SetCallback", &Foo::SetCallback )
.set( "ExecuteCallback", &Foo::ExecuteCallback )
;
char* luaCode =
"SLB.using( SLB )\n"
"o = Foo()\n"
"o:SetCallback( function() \n"
" print( 'Callback executed' ) \n"
" end \n"
") \n"
"o:ExecuteCallback() \n";
SLB::Script script( &scriptManager );
script.doString( luaCode );
// The result is
> "Callback executed"
I don't know if there is a type in SLB library that I can put instead of ? ( see the first line of code ).
Or if I have to do the things different.

Resources