0

これらは、ファイル main.cpp をコンパイルしようとしたときに発生したエラーです。

 C:\Users\student\Desktop\C++ Solution Framework (1)\AssignmentSolution\AssignmentSolution\main.o:main.cpp|| undefined reference to   `MsgPacket::MsgPacket(int, int, int, int, std::string)'|
 C:\Users\student\Desktop\C++ Solution Framework (1)\Assignment Solution\AssignmentSolution\main.o:main.cpp|| undefined reference to `DataStream::DataStream()'|
 ||=== Build finished: 2 errors, 0 warnings (0 minutes, 1 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

最後に、これは 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();
}
4

1 に答える 1

3

Undefined reference toリンクできないものを参照しようとしていることを意味します。2 つの cpp ファイルをリンクしていますか?

于 2013-05-09T16:12:28.833 に答える