“在类外声明 xxx 不是定义”错误

新手上路,请多包涵

这是错误:

错误:在类之外声明“DataStream::DataStream()”不是定义 [fpermissive]|

||=== 构建完成:1 个错误,0 个警告(0 分钟,0 秒)===|

这是 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

最后这是 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();
}

原文由 M171 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答
DataStream::DataStream();

是类 DataStream 的构造函数声明。它必须在类内声明,而不是在类外。

 class DataStream
{
    public:
        DataStream();
};

此外,您可以在类内部或外部内联定义此构造函数,例如

DataStream::DataStream()
{}

原文由 Alok Save 发布,翻译遵循 CC BY-SA 4.0 许可协议

正如其他人已经提到的,这个错误很可能是因为语法错误。我有一个 Declaration of SomeClass outside of class is not definition 错误。问题是在 for 循环之后缺少 }

 void A::a_function() {
  for (int i = 0; i < 20; ++i) {
    // loop code
  // note the missing } here
  // function code
}

SomeClass* A::getSomeClassInstance() {
  // function code
}

原文由 RedJohn 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏