【问题标题】:Undefined reference to object::function对 object::function 的未定义引用
【发布时间】:2015-07-08 03:46:56
【问题描述】:

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

【问题讨论】:

    标签: c++ compilation undefined-reference


    【解决方案1】:

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

    【讨论】:

    • 即使在那之后 Message.cpp:15:3: 错误: 成员 'Messages' [-fpermissive] 上的额外限定 'Message::' Message::Messages(int newID, list newPackets ) ^ Message.cpp:21:7: 错误:成员 'getID' [-fpermissive] int Message::getID() 上的额外限定 'Message::' ^
    • 在你的cpp文件中删除周围的class Message声明,并且不要在你的头文件中添加Message::stackoverflow.com/questions/5642367/…
    • 如果我删除了类 Message 我得到这个错误 main.cpp:81:1: 警告:控制到达非无效函数的结尾 [-Wreturn-type] } ^ Message.cpp:8: 2:错误:“private”之前的预期 unqualified-id 私有:^ Message.cpp:12:2:错误:“public”public 之前的预期 unqualified-id:^
    • 你显然没有按照我说的去做。我真的建议你读一两本关于 C++ 的书。您的 cpp 文件中不应包含 public:private: 限定符。
    • 我不知道如何从我的参考文献中进一步评论他们的例子都有公共和私有限定符如果删除类声明和限定符然后我只是得到未声明的消息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 2022-01-05
    • 2013-03-20
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多