i have been stuck on this for awhile now no matter how i compile it on cygwin i still can't get 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 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
i've tried to compile it but i keep getting the same error each time
/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()'
compiling using cygwin
g++ main.cpp Message.cpp Packet.cpp
Your cpp file is wrong. You are defining a new class, Message, in the cpp file and defining its functions instead of giving definitions to the functions from the Message class in your header file. Your cpp file should look like 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();
}
Related
I was trying to put this code into a class but I couldn't manage to do it. The job of the function is pulling team names from a .txt file and putting them in a vector. I think the main problem is I couldn't select the right function return type.
This is the teams.txt: (The names before the "-" symbol are teams. Other names are unrelated with my question but they are coachs of the teams.)
Trabzonspor-Abdullah Avcı+
Fenerbahçe-Vítor Pereira+
Beşiktaş-Sergen Yalçın+
Galatasaray-Fatih Terim+
İstanbul Başakşehir-Emre Belözeoğlu+
Alanyaspor-Bülent Korkmaz+
Fatih Karagümrük-Francesco Farioli+
Gaziantep-Erol Bulut+
Adana Demirspor-Vincenzo Montella+
Ankara Dinc-Nestor El Maestro+
Antalyaspor-Nuri Şahin+
Kayserispor-Hikmet Karaman+
Yeni Malatyaspor-Marius Sumudica+
Konyaspor-İlhan Palut+
Sivasspor-Rıza Çalımbay+
Hatayspor-Ömer Erdoğan+
Giresunspor-Hakan Keleş+
Kasımpaşa-Hakan Kutlu+
And this is the my code who does the putting them in a vector:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std; //I know that's a bad practice but i just need to do this for while
std::string process(std::string const& s) //A function to seperate teams from the coaches
{
string::size_type pos = s.find('-');
if (pos != string::npos)
{
return s.substr(0, pos);
}
else
{
return s;
}
}
int main() {
ifstream readTeam("teams.txt");
if (!readTeam) { //checking that successfully opened the file.
std::cerr << "Error while opening the file.\n";
return 1;
}
vector<std::string> teams;
string team;
while (getline(readTeam, team)) {
teams.push_back(process(team));
}
readTeam.close();
int g = 1;//for printing the teams, just for displaying it. doesn't have to in a class.
for (const auto& i : teams) {
cout << g;
cout << i << endl;
g++;
}
return 0;
}
And that's what i did(tried) to make it a class:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
std::string process(std::string const& s)
{
string::size_type pos = s.find('-');
if (pos != string::npos)
{
return s.substr(0, pos);
}
else
{
return s;
}
}
class readFile {
public:
void setTxtName(string);
vector<unsigned char> const& getTeam() const{
}
vector<string> teams;
private:
string fileName;
};
int main() {
readFile pullTeams;
pullTeams.setTxtName("teams.txt");
return 0;
}
void readFile::setTxtName(string txtName) {
fileName = txtName;
}
vector<string> const& readFile::getTeam { //problem is defining it(I think). So I couldn't add my main code int it..
return teams;
}
Anything helps, thank you!
I did a little different research based on the Botje's comment. And I manage to create an answer based on here. Thanks.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
std::string process(std::string const& s){
string::size_type pos = s.find('-');
if (pos != string::npos){
return s.substr(0, pos);
}
else{
return s;
}
}
class readFile {
public:
vector<string> getTxt();
bool read(string);
private:
vector<string> teams;
string team;
ifstream txt;
};
int main() {
vector<string> teams;
readFile team;
if (team.read("teaams.txt") == true)
teams = team.getTxt();
int g = 1;//for printing the teams, just for displaying it. doesn't have to in a class.
for (const auto& i : teams) {
cout << g;
cout << i << endl;
g++;
}
return 0;
}
bool readFile::read(string txtName) {
ifstream txt;
string team;
txt.open(txtName.c_str());
if (!txt.is_open())
return false;
while (getline(txt, team))
teams.push_back(process(team));
return true;
}
vector<string> readFile::getTxt() {
return teams;
}
I need to provide a CFG class in a separate file, but I'm unsure how to compile it together with the associated .h and the main program.
I've #includeed the .h file and I've asked for both files at the command line, but I'm not sure why this is wrong for compiling them together.
Thoughts?
CFG.cpp:
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class CFG
{
public:
string code[25];
char startNT;
//private:
CFG(string inCode[], int stringLen)
{
for (int a = 0; a < stringLen; a++)
{
//cout << inCode[a] << endl;
this->code[a] = inCode[a];
}
for (int a = 0; a < stringLen; a++)
{
cout << this->code[a] << endl;
}
}
char getStartNT()
{
return startNT;
}
void setStartNT(char stNT)
{
startNT = stNT;
}
bool processData(string inString, string wkString)
{
//Our recursive function
return true;
}
void garbage()
{
return;
}
};
CFG.h:
#ifndef _cfg_h_
#define _cfg_h_
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class CFG
{
public:
string code[25];
char startNT;
CFG(string inCode[], int stringLen);
char getStartNT();
void setStartNT(char stNT);
bool ProcessData(string inString, string wkString);
void garbage();
};
#endif
cfg_entry.cpp:
#include <stdio.h>
#include <iostream>
#include "cfg.h"
using namespace std;
int main()
{
string inArray[5];
inArray[0] = "test0";
inArray[1] = "test1";
inArray[2] = "test2";
inArray[3] = "test3";
inArray[4] = "test4";
CFG * cfg1 = new CFG(inArray, 5);
cfg1->garbage();
return 0;
}
Compile errors:
art#tv:~/Dropbox/Weber/CS 4110/Individual Assignment 2$ g++ -g -std=c++11 -Wall -o cfg_entry cfg.cpp cfg_entry.cpp
/tmp/ccICQEd0.o: In function `main':
/home/art/Dropbox/Weber/CS 4110/Individual Assignment 2/cfg_entry.cpp:15: undefined reference to `CFG::CFG(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, int)'
/home/art/Dropbox/Weber/CS 4110/Individual Assignment 2/cfg_entry.cpp:16: undefined reference to `CFG::garbage()'
collect2: error: ld returned 1 exit status
I found my issue. In my case, the header file was defining the class and the .cpp file was re-defining it again, trying to create 2 instances of the CFG class. The .h needed to handle the class declaration and variable instantiation while the .cpp handles only the function definitions.
cfg.h:
#ifndef _cfg_h_
#define _cfg_h_
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class CFG
{
private:
string code[25];
char startNT;
public:
CFG(string inCode[], int stringLen);
char getStartNT();
void setStartNT(char stNT);
bool processData(string inString, string wkString);
void garbage();
};
#endif
cfg.cpp:
#include <iostream>
#include <stdio.h>
#include <string>
#include "cfg.h"
using namespace std;
CFG::CFG(string inCode[], int stringLen)
{
for (int a = 0; a < stringLen; a++)
{
//cout << inCode[a] << endl;
this->code[a] = inCode[a];
}
for (int a = 0; a < stringLen; a++)
{
cout << this->code[a] << endl;
}
}
char CFG::getStartNT()
{
return startNT;
}
void CFG::setStartNT(char stNT)
{
startNT = stNT;
}
bool CFG::processData(string inString, string wkString)
{
//Our recursive function
return true;
}
void CFG::garbage()
{
return;
}
I have an error when I print my PrintSount() in the main. The compiler said:
No member named 'PrintSound' in 'animal')
Why is this happening and how I can fix it?
main
#include <iostream>
#include "animal.h"
#include "pat.h"
#include "not_pat.h"
int main()
{
std::string lastName;
std::string sound;
std::string name;
std::string type;
double speed = 0;
animal* animals[2];
for (int i = 0; i < 2; i++)
{
std::string ani;
std::cout << "Enter animal: (dog,fish,lion,monkey ... ): " << std::endl;
std::cin >> ani;
if (ani == "dog" || ani == "cat" || ani == "fish")
{
animals[i] = new pat("test", "test", "test","test");
}
else
{
animals[i] = new not_pat(speed, lastName, "test2", "test2");
}
}
for (int i = 0; i < 2; i++)
{
std::cout << animals[i]->PrintName() << animals[i]->PrintLatName() << animals[i]- >PrintType() << animals[i]->PrintSound() << std::endl;
}
}
animal.h
#ifndef ANIMAL_H
#define ANIMAL_H
#include <iostream>
#include <stdio.h>
#include <string>
class animal{
public:
animal(std::string name, std::string type, std::string lastName);
std::string PrintType();
std::string PrintName();
std::string PrintLatName();
protected:
std::string _name;
std::string _lastName;
std::string _type;
private:
};
#endif
animal.cpp
#include "animal.h"
animal::animal(std::string name , std::string type, std::string lastName)
{
_name = name;
_type = type;
_lastName = lastName;
}
std::string animal::PrintType()
{
return this->_type;
}
std::string animal::PrintName()
{
return this->_name;
}
std::string animal::PrintLatName()
{
if(_lastName == "0")
{
return NULL;
}
else
{
return this->_lastName;
}
}
pat.h
#ifndef PAT_H
#define PAT_H
#include "animal.h"
#include <iostream>
#include <stdio.h>
#include <string>
class pat : public animal
{
public:
pat(std::string lastName, std::string sound, std::string name, std::string type);
std::string PrintSoud(animal *p);
protected:
std::string _sound;
private:
};
#endif
pat.cpp
#include "pat.h"
#include "animal.h"
pat::pat(std::string lastName, std::string sound, std::string name, std::string type) : animal(name,type,lastName)
{
_sound = sound;
}
std::string pat::PrintSoud(animal *p)
{
return this->_sound;
}
not_pat.h
#ifndef NOT_PAT_H
#define NOT_PAT_H
#include <iostream>
#include <stdio.h>
#include <string>
class not_pat : public animal
{
public:
not_pat(double speed,std::string lastName, std::string name, std::string type);
double PrintSpeed();
protected:
double _speed;
private:
};
#endif
not_pat.cpp
#include "animal.h"
#include "not_pat.h"
not_pat::not_pat(double speed, std::string lastName, std::string name, std::string type) : animal(name, type,lastName)
{
if(speed == 0)
{
_speed = NULL;
}
else
{
_speed = speed;
}
}
double not_pat::PrintSpeed()
{
return this->_speed;
}
C++ is a statically-typed language. You use pat using a pointer to animal. So the compiler checks if animal has the member function PrintSound(). It does not have it, so there is a compilation error raised. You need to add the declaration of PrintSound to animal (probably a pure virtual function) to fix this.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I'm trying to test a program and every time I go to compile it, I get the error LNK2019: unresolved external symbol "public: __thiscall Prog3Graph::Prog3Graph(void)" (??0Prog3Graph##QAE#XZ) referenced in function _main. I was wondering what is causing this and how I can fix it. I've tried messing around with the code, but can't figure what is causing it.
Prog3Graph.cpp:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "Prog3Graph.h"
#include "GraphNode.h"
using namespace std;
int main()
{
Prog3Graph *test;
test = new Prog3Graph();
test->buildGraph("Graph.txt");
test->printGraph();
return 0;
}
bool Prog3Graph::buildGraph(char *fileName)
{
int i,j,index,numlinks, link;
char line[24];
ifstream inFile;
inFile.open(fileName, ifstream::in);
if(!inFile.is_open())
{
cout << "Unable to open file " << fileName << ". \nProgram terminating...\n";
return 0;
}
for(i=0;i<10;i++)
{
getNextLine(line,24);
index = atoi(line);
Nodes[i].setNodeID(index);
getNextLine(line,24);
Nodes[i].setNodeData(line);
getNextLine(line,24);
numlinks = atoi(line);
for(j=0;j<numlinks;j++)
{
getNextLine(line,24);
link = atoi(line);
AdjMatrix[i][link]=1;
}
inFile.close();
}
return true;
}
void Prog3Graph::printGraph()
{
int i,j;
cout << "------------------------------------------------------------\n\n";
cout << " Adjacency Matrix:\n\n";
cout << " 0 1 2 3 4 5 6 7 8 9\n";
cout << " +---------------+\n";
for(i=0; i<10; i++)
{
cout << i << "|";
for(j=0; j<10; j++)
{
cout << AdjMatrix[i][j] << "|";
}
cout << "\n +---------------+\n";
}
}
bool Prog3Graph::getNextLine(char *line, int lineLen)
{
int done = false;
ifstream inFile;
while(!done)
{
inFile.getline(line, lineLen);
if(inFile.good())
{
if(strlen(line) == 0)
continue;
else if(line[0] == '#')
continue;
else done = true;
}
else
{
strcpy(line, "");
return false;
}
}
return true;
}
Prog3Graph.h:
#pragma once
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "GraphNode.h"
using namespace std;
class Prog3Graph
{
private:
ifstream inFile; // File stream to read from
int AdjMatrix[10][10];
GraphNode Nodes[10];
public:
Prog3Graph(); // Class constructor
~Prog3Graph(); // Class destructor
bool buildGraph(char *filename); // Read graph file, build graph
void printGraph(); // Print all data in graph
void depthFirstTraversal(); // Perform a depth first traversal
private:
bool getNextLine(char *line, int lineLen); // Read next line from graph file
};
GraphNode.cpp:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "GraphNode.h"
using namespace std;
void GraphNode::setNodeID(int ID)
{
m_iNodeID = ID;
}
int GraphNode::getNodeID()
{
return m_iNodeID;
}
void GraphNode::setNodeData(char *data)
{
int i;
for(i=0;i<24;i++)
{
m_sNodeData[i] = data[i];
}
}
char *GraphNode::getNodeData()
{
return &m_sNodeData[24];
}
void GraphNode::setVisited(bool visited)
{
m_bVisited = visited;
}
bool GraphNode::hasBeenVisited()
{
return m_bVisited;
}
GraphNode.h:
#pragma once
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class GraphNode
{
private:
int m_iNodeID;
char m_sNodeData[24];
bool m_bVisited;
public:
GraphNode();
~GraphNode();
void setNodeID(int ID);
int getNodeID();
void setNodeData(char *data);
char *getNodeData();
void setVisited(bool visited);
bool hasBeenVisited();
};
Read the message carefully:
unresolved external symbol "public: __thiscall Prog3Graph::Prog3Graph(void)" (??0Prog3Graph##QAE#XZ) referenced in function _main.
You've declared a constructor (and a destructor) for your class, but you've never actually defined them.
I have a problem. When I compile the program I don't have any errors, but when I use valgrind:
Uninitialized value was created by a heap allocation (line with new)
Conditional jump or move depends on uninitialized value(s)(line with delete)
I search through the forums however I didn't find much information which could help me.
I would be really grateful for a hint.
My program
#include <cstdlib>
#include <stdint.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <istream>
#include <cstring>
#include <fstream>
#include <sstream>
#include <cctype>
using namespace std;
using std::cout;
using std::endl;
int dlugosc,miejsce;
ifstream file;
class channel
{
public:
int start;
double length;
int bytespix;
int resolution;
channel(double g) : start(g),
length(0),
bytespix(0),
resolution(0)
{
}
};
int fileopen() // opens the file and returns its size
{
file.open ("0_dlc.000", ios::in|ios::binary);
if( file.good() == true )
{
cout << "Uzyskano dostep do pliku!" << endl;
}
else
{
cout<< "File cannot open" <<endl;
}
file.seekg(0, file.end);
dlugosc = file.tellg();
return dlugosc;
}
int findword(const char* slowo,int startplace)
{
int m;
int c=0;
int cur=0;
unsigned int equal=0;
char element=0;
file.seekg (startplace, file.beg);
for(m=0;m<dlugosc;m++)
{
file.get(element);
if(element==slowo[cur])
{
equal++;
cur++;
}
else
{
equal=0;
cur=0;
if(element==slowo[cur])
{
equal++;
cur++;
}
}
if(equal==strlen(slowo))
{
return m+startplace;
}
}
return 0;
}
int findvalue(const char* wartosc,int startpoint)
{
int p;
int g;
char element=0;
char* buffer = new char[9];
miejsce = findword(wartosc,startpoint); // miejsce to global variable
file.seekg (miejsce+1, file.beg);
for(p=0;(int)element<58;p++)
{
file.get(element);
if((int)element>58 || (int)element<48)
break;
else
buffer[p] = element;
}
buffer[p]='\0';
g = atoi(buffer);
delete [] buffer;
return g;
}
int main()
{
int a,h=0,channels,start=0,length=0,resolution=0,bytespix=0,m=0;
const char* slowko="Data offset: ";
dlugosc=fileopen();
channel** kanaly=0;
kanaly = new channel*[9];
miejsce=0;
for(a=0;a<9;a++)
{
kanaly[a] = new channel(4);
start = findvalue("Data offset: ",miejsce+20);
kanaly[a]->start=start;
}
for(m=0;m<9;m++)
{
delete kanaly[m];
}
delete []kanaly;
file.close();
}
The problem is in the constructor of channel. Initialize all member variables, and the problem will go away :
class channel
{
public:
double start;
double length;
int bytespix;
int resolution;
channel(double g) : start(g),
length(0),
bytespix(0),
resolution(0)
{
}
};