C++ errors when compiling -
i'm getting following errors when trying compile , can't understand why, apologies if it's simplistic , easy solve i'm pretty new c++ , having abit of trouble getting head around it.
errors:
main.cpp|20|error: invalid conversion 'char*' 'char' [-fpermissive]| msgpacket.h|15|error: initializing argument 5 of 'msgpacket::msgpacket(int, int, int, int, char)' [-fpermissive]|
files main.cpp
#include <iostream> #include <iomanip> #include "datastream.h" #include "msgpacket.h" using namespace std; datastream name; datastream * mypackets = new datastream(); int main() { int source; int destination; int type; int port; int input; char data[50]; msgpacket * packet = new msgpacket(source,destination,type,port,data); cout << "my assignment" << endl;; }
msgpacket.h
#ifndef msgpacket_h #define msgpacket_h #include <string> #include "packetaddress.h" using namespace std; class msgpacket : public packetaddress { public: msgpacket(); msgpacket (const msgpacket & rhs); msgpacket(string datain); msgpacket(int port, int source, int destination, int type, char data); string tostring(); string getdata() const {return _data;}; void setdata(string indata) {_data = indata;}; string dataoutput(); virtual ~msgpacket(); virtual msgpacket * clone() { return new msgpacket(*this); } protected: string _data; }; #endif // msgpacket_h
msgpacket.cpp
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <string> #include <iomanip> #include "msgpacket.h" using namespace std; msgpacket::msgpacket(): packetaddress(0,0) { } msgpacket::msgpacket (const msgpacket & rhs): packetaddress(rhs), _data(rhs.getdata()) { } msgpacket::msgpacket(string datain): packetaddress(0,0){ string temp; temp = datain.substr (0,4); _source = atoi(temp.c_str()); temp = datain.substr (5,4); _dest = atoi(temp.c_str()); temp = datain.substr (10,4); _type = atoi(temp.c_str()); temp = datain.substr (15,4); _port = atoi(temp.c_str()); _data = datain.substr (20,datain.length()); #ifdef debug cout << "create packet: " << this->tostring() << endl; #endif } msgpacket::msgpacket(int source, int destination): packetaddress(source,destination) { } msgpacket::msgpacket(int source, int destination, int port): packetaddress(source,destination) { _port = port; } msgpacket::msgpacket(int source, int destination, int type, int port, char data): packetaddress(source, destination) { _source = source; _dest = destination; _type = type; _data = data; _port = port; } msgpacket::~msgpacket() { //dtor } string msgpacket::dataoutput() { stringstream output;//create stringstream output << setw(4) << setfill('0') << _source << ":" << setw(4) << setfill('0') << _dest << ":" << setw(4) << setfill('0') << _type << ":" << setw(4) << setfill('0') << _port << ":" << _data; return output.str(); } string msgpacket::tostring() { stringstream output;//create stringstream output << "[" << showbase << hex << << "] s:[" << _source << "] d:[" << _dest << "] p:[" << _type << "] t:[" << _port << "]" << " data[" << _data << "]"; return output.str(); }
msgpacket(int port, int source, int destination, int type, char data); ^^^^
expects char
.
you passing character array it. decays char*
here
msgpacket * packet = new msgpacket(source,destination,type,port,data); ^^^^
i think should change
char data[50];
to
std::string data;
and change constructor declaration to
msgpacket(int port, int source, int destination, int type, std::string data);
edit: , definition of specific constructor
msgpacket::msgpacket(int source, int destination, int type, int port, std::string data): ^^^^^^^^^^ packetaddress(source, destination) {
however can also skip data
part completely since data
in main doesn't contain (is not initialized anything.)
Comments
Post a Comment