2013-05-09 31 views
0

這是錯誤:「類的XXX之外的聲明不是定義」 錯誤

error: declaration of 'DataStream::DataStream()' outside of class is not definition [ fpermissive]| 
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===| 

這是的main.cpp文件:

#include <iostream> 
#include <iomanip> 
#include "DataStream.h" 
#include "MsgPacket.h" 


using namespace std; 

DataStream * Packet = new DataStream(); 
DataStream::DataStream(); 



int main() { 

int source; 
int destination; 
int type; 
int port; 
int input; 
std::string data; 

cout << "My Assignment" << endl;; 


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

}

這是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 source, int destination, int port, int type, std::string data); 
    MsgPacket(int ,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 

最後, s是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, std::string  data): 
PacketAddress(source, destination) 
{ 
_source = source; 
_dest = destination; 
_type = type; 
_data = data; 
_port = port; 
} 

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(); 
} 
+0

@taocp這不是非常有幫助。我相信這是OP首先想到的。 – Adam 2017-05-15 20:14:15

回答

2
DataStream::DataStream(); 

是構造爲DataStream類的聲明,必須在類中沒有外部聲明。

class DataStream 
{ 
    public: 
     DataStream(); 
}; 

此外,您可以定義在類內部還是外部的這種構造內聯,就像

DataStream::DataStream() 
{} 
+0

這是放在文件中包括類。它已經在一個名爲datastream.h(頭文件)的文件中的類中聲明過了。如果沒有這樣做,那麼會出現一個不同的錯誤:「未定義的對MsgPacket :: MsgPacket的引用(int,int,int,int, std :: string)'| 未定義引用DataStream :: DataStream()'| || ===構建完成:2個錯誤,0個警告(0分鐘,1秒)=== | – M171 2013-05-09 15:44:50

1

萬一有人得到,因爲語法錯誤,在這裏,請注意,你可以有報同樣的錯誤初始化參數時出現語法錯誤:

class A 
{ 
    A(); 
    char b; 
}; 

A::A() 
, b(0) // wrong, should be ':' instead of ',' 
{ 
} 
27

另一種可能導致此錯誤的方式類似於以下代碼。

class A 
{ 
    void a_function(); 
}; 

A::a_function(); // Note the semicolon here 
{ 
    // function contents... 
} 

額外的分號可以說是相當難以察覺,如果你沒有足夠的咖啡因還...

+1

是的,那是我的問題,我的咖啡因很多,我... – Ludwik 2014-07-02 19:12:56

+1

我有同樣的問題,謝謝你的後續答覆。 – 2015-03-09 14:42:02

+0

同樣的問題,謝謝! – 2016-04-23 19:00:14

相關問題