对object :: function的未定义引用

时间:2015-04-29 03:37:00

标签: c++ compilation undefined-reference

我现在已经坚持了一段时间,无论我如何在cygwin上编译它我仍然无法通过它

Main.cpp的

#include <iostream>
#include <istream>
#include <fstream>
#include <string>
#include <sstream>
#include <ctype.h>
#include <cstring>
#include <cstdlib>
#include "Message.h"
#include "Packet.h"
#include "main.h"
using namespace std;

void fillList(list<Message> messages, char *argv[]);
void printList(const list<Message> &messages);
int getMID(list<Message> &messages, stringstream &lines);
void create(list<Message> &messages, stringstream &lines);
bool exist(list<Message> &messages, stringstream &lines);
Message getMessage(int ID, list<Message> &messages);
void printList(const list<Message> &messages);

void fillList(list<Message> messages, char *argv[])
{
    string lines;
    ifstream files(argv[1]);
    if(files.is_open())
    {
        int delimc = 0;
        string line;
        stringstream file;
        while(getline(files,lines))
        {
            file << lines;
            if(!exist(messages, file))
                create(messages, file);
            int ID = getMID(messages, file);
            string fields[3];
            while(getline(file, line, ':'), delimc != 3)
            {
                if(delimc != 0)
                {
                    fields[delimc] = line;
                }
                delimc++;
            }
            getMessage(ID, messages).add(atoi(fields[1].c_str()), fields[2]);
            delimc = 0;
        }
    files.close();
    }
}
int getMID(list<Message> &messages, stringstream &lines)
{
    std::string line;
    getline(lines, line, ':');                  //followed standard procedure for defining string (line)
    return atoi(line.c_str());
}
void create(list<Message> &messages, stringstream &lines)
{
    messages.push_back(getMessage(getMID(messages, lines), messages));         //getID takes 2 arguments
}
bool exist(list<Message> &messages, stringstream &lines)
{
    list<Message>::iterator itr;
    for(itr = messages.begin(); itr != messages.end(); itr++)
    {
        if(itr->Message::getID() == getMID(messages, lines))                    //itr is not a pointer and if you want to point it use the correct way
            return true;
    }
    return false;
}
Message getMessage(int ID, list<Message> &messages)
{
    list<Message>::iterator itr;
    for(itr = messages.begin(); itr != messages.end(); itr++)
    {
        if(itr->Message::getID() == ID)                                     //itr is not a pointer and if you want to point it use the correct way                  
            return *itr;
    }
    //return ;                  //no need to return null, even if you want to return still you have to assign memebers of messages as null seperately
}
void printList(list<Message> &messages)
{
    list<Message>::iterator itr;

    for(itr = messages.begin(); itr != messages.end(); itr++)
    {
        cout <<  itr->Message::toString();                              //was stucked at it, you have to redesgn it in a way that you will first use add() and then packect.tostring() in order to print
    }
    cout << '\n';
}
int main(int argc, char *argv[])
{
    cout << "The Program: " << argv[0] << " was created by Newbie";
    list<Message> messages;
    fillList(messages, argv);
    printList(messages);
    return 0;
}
//input argument for the text file
//read the file with limit of 2 splitter
//split it up and create the Message with packets
//display the msg

Message.cpp

#include <iostream>
#include <list>
#include <ctype.h>
#include <sstream>
#include "Packet.h"
using namespace std;

class Message
{
    private:
        int ID;
        list<Packet> packets;

    public:
        Messages(int newID, list<Packet> newPackets)
        {
            ID = newID;
            packets = newPackets;
        }

        int getID()
        {
            return ID;
        }
        void setID(const int &newID)
        {
            ID = newID;
        }

        list<Packet> getPackets()
        {
            return packets;
        }

        void add(int SQN, string text)
        {
            packets.push_back(Packet(SQN, text));
        }

        string toString()
        {
            ostringstream oss;
            oss << "Message " << ID;
            list<Packet>::iterator itr;
            for(itr = packets.begin(); itr != packets.end(); itr++)
            {
                oss << itr->toString();
            }
            return oss.str();
        }
};

Message.h

#ifndef Message_H
#define Message_H
#include <iostream>
#include <list>
#include <ctype.h>
#include <sstream>
#include "Packet.h"
using namespace std;
class Message
{
    int ID;
    list<Packet> packets;

    public:
        Message(int ID, list<Packet> packets);
        int getID();
        list<Packet> getPackets();
        void setID(const int newID);
        void setPackets(list<Packet> newPackets);
        void add(int SQN, string text);
        string toString();
};

#endif

我已经尝试编译它,但每次都会出现同样的错误

/tmp/ccDZ0PKH.o:main.cpp:(.text+0x1f3): undefined reference to `Message::add(int, std::string)'
/tmp/ccDZ0PKH.o:main.cpp:(.text+0x1f3): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Message::add(int, std::string)'
/tmp/ccDZ0PKH.o:main.cpp:(.text+0x4ed): undefined reference to `Message::getID()'
/tmp/ccDZ0PKH.o:main.cpp:(.text+0x4ed): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Message::getID()'
/tmp/ccDZ0PKH.o:main.cpp:(.text+0x592): undefined reference to `Message::getID()'
/tmp/ccDZ0PKH.o:main.cpp:(.text+0x592): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Message::getID()'
/tmp/ccDZ0PKH.o:main.cpp:(.text+0x63d): undefined reference to `Message::toString()'
/tmp/ccDZ0PKH.o:main.cpp:(.text+0x63d): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Message::toString()'

使用cygwin进行编译 g ++ main.cpp Message.cpp Packet.cpp

1 个答案:

答案 0 :(得分:0)

您的cpp文件错误。您正在cpp文件中定义一个新类Message并定义其函数,而不是从头文件中的Message类为函数定义。您的cpp文件应如下所示:

Message::Message(int newID, list<Packet> newPackets)
{
    ID = newID;
    packets = newPackets;
}

int Message::getID()
{
    return ID;
}

void Message::setID(const int &newID)
{
    ID = newID;
}

list<Packet> Message::getPackets()
{
    return packets;
}

void Message::add(int SQN, string text)
{
    packets.push_back(Packet(SQN, text));
}

string Message::toString()
{
    ostringstream oss;
    oss << "Message " << ID;
    list<Packet>::iterator itr;
    for(itr = packets.begin(); itr != packets.end(); itr++)
    {
        oss << itr->toString();
    }
    return oss.str();
}