c++ - Undefined reference to object::function -
i have been stuck on awhile no matter how compile on cygwin still can't through it
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 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 not pointer , if want point use 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 not pointer , if want point use correct way return *itr; } //return ; //no need return null, if want return still have assign memebers of messages 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 @ it, have redesgn in way first use add() , packect.tostring() in order print } cout << '\n'; } int main(int argc, char *argv[]) { cout << "the program: " << argv[0] << " created newbie"; list<message> messages; filllist(messages, argv); printlist(messages); return 0; } //input argument text file //read file limit of 2 splitter //split , create message packets //display 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
i've tried compile keep getting same error each time
/tmp/ccdz0pkh.o:main.cpp:(.text+0x1f3): undefined reference `message::add(int, std::string)' /tmp/ccdz0pkh.o:main.cpp:(.text+0x1f3): relocation truncated fit: r_x86_64_pc32 against undefined symbol `message::add(int, std::string)' /tmp/ccdz0pkh.o:main.cpp:(.text+0x4ed): undefined reference `message::getid()' /tmp/ccdz0pkh.o:main.cpp:(.text+0x4ed): relocation truncated fit: r_x86_64_pc32 against undefined symbol `message::getid()' /tmp/ccdz0pkh.o:main.cpp:(.text+0x592): undefined reference `message::getid()' /tmp/ccdz0pkh.o:main.cpp:(.text+0x592): relocation truncated fit: r_x86_64_pc32 against undefined symbol `message::getid()' /tmp/ccdz0pkh.o:main.cpp:(.text+0x63d): undefined reference `message::tostring()' /tmp/ccdz0pkh.o:main.cpp:(.text+0x63d): relocation truncated fit: r_x86_64_pc32 against undefined symbol `message::tostring()'
compiling using cygwin g++ main.cpp message.cpp packet.cpp
your cpp file wrong. defining new class, message
, in cpp file , defining functions instead of giving definitions functions message
class in header file. cpp file should this:
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(); }
Comments
Post a Comment