对 object::function 的未定义引用

Undefined reference to object::function

提问人:adam danial 提问时间:4/29/2015 更新时间:4/29/2015 访问量:1032

问:

我已经坚持了一段时间,无论我如何在 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

消息.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();
        }
};

消息.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 消息.cpp 数据包.cpp

C++ 编译 未定义引用

评论


答:

0赞 dwcanillas 4/29/2015 #1

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

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

评论

0赞 adam danial 4/29/2015
即使在那之后 Message.cpp:15:3:错误:成员 'Messages' 上的额外限定 'Message::' [-fpermissive] Message::Messages(int newID, list<Packet> newPackets) ^ Message.cpp:21:7: error: extra qualification 'Message::' on member 'getID' [-fpermissive] int Message::getID() ^
0赞 dwcanillas 4/29/2015
删除 CPP 文件中的周围声明,并且不要添加头文件:stackoverflow.com/questions/5642367/...class MessageMessage::
0赞 adam danial 4/29/2015
如果我删除了类消息,我会收到此错误 main.cpp:81:1:警告:控件到达非无效函数的末尾 [-Wreturn-type] } ^ 消息.cpp:8:2:错误:在“private”之前预期为不合格的 id private:^ Message.cpp:12:2:错误:在“public”之前预期的不合格 id:public:^
0赞 dwcanillas 4/29/2015
你显然没有按照我的吩咐去做。我真的建议你读一两本关于 c++ 的书。cpp 文件中不应包含 和 限定符。public:private:
0赞 adam danial 4/29/2015
我不知道如何从我所拥有的参考资料中进一步评论,他们的示例都有公共和私人限定符,如果删除了类声明和限定符,那么我只会收到未声明的消息