2013-05-09 192 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]。你會得到一個[徽章](http://stackoverflow.com/badges)(c: – 2013-05-09 13:53:50

+0

模糊標題不太可能對未來訪問者的網站有用 – 2013-05-09 14:27:51

回答

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

預計char

您正在向其傳遞字符數組。其衰變爲char*這裏

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

我想你應該改變

char data[50]; 

std::string data; 

和更改構造函數聲明

MsgPacket(int port, int source, int destination, int type, std::string data); 

編輯:這個具體的構造函數的定義

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

但是你可以也跳過data部分完全因爲你data主不包含任何東西(不用任何東西初始化。)

+0

嗨,姓名,謝謝你的快速回復!如你所建議的那樣改變了代碼,並且你正確的解決了這個問題......但是現在當試圖編譯時,它顯示出另一個錯誤......我不明白爲什麼它說undefined reference ....這些是發生新的錯誤... 對'MsgPacket :: MsgPacket(int,int,int,std :: string)'的未定義引用' 對'DataStream ::〜DataStream()'的未定義引用 對'DataStream: :DataStream()' 未定義的引用'DataStream :: DataStream()' 再次,我的道歉,如果這是一個noob錯誤修復 – Xiah 2013-05-09 14:02:02

+0

@Xiah你也必須改變定義的構造函數。 'MsgPacket :: MsgPacket(...)'部分 – stardust 2013-05-09 14:09:12

+0

@Xiah這裏沒有真正的文檔。更像是一個教程更好。但至於字符串,你總是可以檢查文檔這裏http://en.cppreference.com/w/cpp/string – stardust 2013-05-09 14:21:11

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); 
                   ^^^^^ 

data只會衰減到char *

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

您的構造函數接受一個單獨的char。使用string代替:

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

你是在你main傳遞char data[]。數組名稱就像指針一樣。你的函數簽名採用一個char char data作爲第五個參數。編譯器看到你通過char*(指向char數組的指針)而不是char。因此,錯誤。

你需要做的

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