Home > Back-end >  How to add custom data types to the queue?
How to add custom data types to the queue?

Time:12-03


Add the regular data types, is simple:

 
STD: : queue ReqQueue;
ReqQueue. Push (1);


But when I try to insert the data types defined themselves when I was puzzled:

Such as my object:

 
The class ReqMsg
{
Public:
ReqMsg (string & amp; M, string & amp; P, int ver, int id)
{
Method=m;
Params=p;
_ver=ver;
_id=id;
}
~ ReqMsg ();
String method;//the method name
String params.//parameter
Int _ver;//version
Int _id.//id
}


I can't directly add it like this:
 
STD: : queue ReqQueue;
ReqQueue. Push (new ReqMsg);



CodePudding user response:

STD: : queue

A=new ReqMsg ReqMsg * ();
ReqQueue. Push (a);

ReqQueue deleted elements, remember to delete;

CodePudding user response:

STD: : queue ReqQueue;
ReqQueue. Push (ReqMsg ());

CodePudding user response:

refer to the second floor truth is right or wrong response:
STD: : queue ReqQueue;
ReqQueue. Push (ReqMsg ());
you can't

CodePudding user response:

reference bdview reply: 3/f
Quote: refer to the second floor truth is right or wrong response:
STD: : queue ReqQueue;
ReqQueue. Push (ReqMsg ());
you no

Your class does not have a default constructor.

CodePudding user response:

Since want to reference your class constructor parameters, the best transfer const string&
 # include & lt; Iostream> 
#include
#include
using namespace std;
The class ReqMsg
{
Public:
ReqMsg (const string& M, const string& P, int ver, int id)
{
Method=m;
Params=p;
_ver=ver;
_id=id;
}
~ ReqMsg () {};
String method;//the method name
String params.//parameter
Int _ver;//version
Int _id.//id
};
Int main () {
Queue q;
Q.p ush (ReqMsg (string (" ABC "), string (edf), 1, 4));
return 1;
}

CodePudding user response:

refer to fifth floor truth is right or wrong response:
you e]
you really work this way!

Have a question want to ask you

Why is my constructor is the following:
 ReqMsg (string & amp; M, string & amp; P, int ver, int id) 
{
Method=m;
Params=p;
_ver=ver;
_id=id;
}


Such use
MsgQueue. Push (ReqMsg (" name1 ", "param1", 2, 1));
Why error:
severity code shows the project file line prohibits display status
Error C2440 "& lt; The function - style - cast>" : can't transfer from ", initializer list "to" ReqMsg easyTest "e: \ svndir 15 - learning c + + \ \ c + + exercise \ easyTest \ easyTest \ easyTest CPP 185


To this also not line:
MsgQueue. Push (ReqMsg (string (" name1 "), string (" param1 "), 2, 1));


The constructor to:

 ReqMsg (const string& M, const string& P, int ver, int id) 
{
Method=m;
Params=p;
_ver=ver;
_id=id;
}


Then relief can be like this:

MsgQueue. Push (ReqMsg (" name1 ", "param1", 2, 1));
  • Related