2013-05-09 2 views
-2

컴파일 할 때 다음과 같은 오류가 발생합니다. 그 이유를 이해할 수 없으며, 간단하고 쉽게 해결할 수있는 경우 사과드립니다. 그러나 C++에 익숙 그 주위에 내 머리를 쓰고있어.컴파일시 C++ 오류가 발생했습니다.

오류 :

main.cpp|20|error: invalid conversion from 'char*' to 'char' [-fpermissive]| 

msgpacket.h|15|error: initializing argument 5 of 'MsgPacket::MsgPacket(int, int, int, int, char)' [-fpermissive]| 

파일 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 a 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 a stringstream 
output << "[" << showbase << hex << this << "] S:[" << _source << "] D:[" << _dest << "] P:[" << _type << "] T:[" << _port << "]" << " DATA[" << _data << "]"; 
return output.str(); 
} 
+1

에 오신 것을 환영합니다 할 필요가있다. 시간을내어 [faq]을 읽으십시오. [badge] (http://stackoverflow.com/badges)를 받게 될 것입니다. (c : –

+0

막연한 제목은 장래 방문자들에게 유용하지 않을 것 같습니다.) –

답변

1
MsgPacket(int port, int source, int destination, int type, char data); 
                  ^^^^ 

char입니다.

문자 배열을 전달 중입니다. 이는 당신이

MsgPacket(int port, int source, int destination, int type, std::string data); 
에 생성자 선언을

std::string data; 

char data[50]; 

을 변경하고 변경해야합니다 여기 char* 내가 생각

MsgPacket * Packet = new MsgPacket(source,destination,type,port,data); 
                   ^^^^ 

에 붕괴

편집 :이 특정 생성자의 정의

MsgPacket::MsgPacket(int source, int destination, int type, int port, std::string data): 
                     ^^^^^^^^^^ 
PacketAddress(source, destination) 
{ 

당신은 또한 data 부분을 건너 뛸 수 있습니다 그러나 완전히 아무것도 (를 포함하지 않는 주에 data하지 않습니다 당신 이후 무엇이든 초기화 된.)

+0

안녕하세요, 빠른 답장을 보내 주셔서 감사합니다! 당신이 제안한대로 코드를 변경했는데 바로 이것이 문제를 해결할 수 있습니다 ... 그러나 이제는 컴파일 할 때 또 다른 오류가 발생합니다 ... 왜 정의되지 않은 참조를 말하는지 이해할 수 없습니다. 새로운 오류가 발생했습니다 ... 'MsgPacket :: MsgPacket (int, int, int, std :: string)'에 대한 정의되지 않은 참조 'DataStream :: ~ DataStream()'에 대한 정의되지 않은 참조 'DataStream : : DataStream() ' 'DataStream :: DataStream() '에 대한 정의되지 않은 참조 다시 사과하십시오. 수정해야 할 멍청한 오류가 있다면 – Xiah

+0

@Xiah 정의를 변경해야합니다. 생성자의. 'MsgPacket :: MsgPacket (...)'part – stardust

+0

@Xiah이 문서는 실제로 없습니다. 더 많은 튜토리얼이 더 좋습니다. 하지만 문자열에 대해서는 항상 설명서를 확인할 수 있습니다. http://en.cppreference.com/w/cpp/string – stardust

0

const ructor는 다음과 같이 선언한다 :

MsgPacket(int port, int source, int destination, int type, char data); 
                  ^^^^^^^^^ 

하지만 당신은 여기 char * 전달됩니다

char data[50]; 
MsgPacket * Packet = new MsgPacket(source,destination,type,port,data); 
                   ^^^^^ 

datachar * 단지 붕괴 것이다.

1
MsgPacket(int port, int source, int destination, int type, char data); 

귀하의 생성자는 하나의 char을 허용합니다.string 대신 사용 : 당신은 당신의 mainchar data[]을 전달하는

MsgPacket(int port, int source, int destination, int type, const string& data); 
0
MsgPacket(int port, int source, int destination, int type, char data); 

. 배열 이름은 포인터와 같습니다. 함수 서명은 다섯 번째 인수로 단일 문자 char data을 사용합니다. 컴파일러에서는 char 대신 char* (char 배열에 대한 포인터)을 전달합니다. 따라서 오류.

당신은 스택 오버플로

MsgPacket(int port, int source, int destination, int type, char* data); 
관련 문제