C++ 对object::function的未定义引用
我已经在这上面呆了一段时间了,不管我如何在cygwin上编译它,我仍然无法通过它 Main.cppC++ 对object::function的未定义引用,c++,compilation,undefined-reference,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 <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
使用cygwin编译
g++main.cpp Message.cpp Packet.cpp您的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::Message(int newID,list newPackets)
{
ID=newID;
数据包=新数据包;
}
int Message::getID()
{
返回ID;
}
无效消息::setID(const int&newID)
{
ID=newID;
}
列表消息::getPackets()
{
返回数据包;
}
无效消息::添加(int-SQN,字符串文本)
{
packets.push_back(Packet(SQN,text));
}
字符串消息::toString()
{
ostringstream oss;
oss即使在该消息之后也是如此。cpp:15:3:error:extra-qualification'消息::'on member'Messages'[-fppermissive]消息::消息(int-newID,list-newPackets)^Message.cpp:21:7:error:extra-qualification'消息::'on member'getID'[-fppermissive]int-Message::getID()^删除cpp文件中周围的类消息
声明,不要在头文件中添加消息::
:如果我删除了类消息,我会收到此错误消息。cpp:81:1:警告:控件到达非无效函数结尾[-Wreturn type]}^Message.cpp:8:2:错误:在“private”private之前应为非限定id:^Message.cpp:12:2:错误:在“public”public之前应为非限定id:^您显然没有执行我告诉您的操作。我真的建议你读一本或两本关于C++的书。您的cpp文件中不应该有public:
和private:
限定符。我不知道如何进一步评论我的引用,它们的示例都有public和private限定符如果删除了类声明和限定符,那么我只会收到未声明的消息
#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()'
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();
}