I'm trying to execute this code, but I'm getting the symbol error of the title:
configfile.cpp:
#include "configFile.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <stdexcept>
#include <map>
configFile* configFile::getInstance(){
pinstance = new configFile();
return pinstance;
}
configFile::configFile(){
filename = "/Users/myfolder/NetBeansProjects/Oier_2/config.cfg";
}
void configFile::setConfigFileName(std::string s){
filename = s;
}
float* getConfiguration(std::string type, int size) {
std::string data[size];
std::string line;
std::ifstream myfile("/Users/myfolder/NetBeansProjects/Oier_2/config.cfg");
while (std::getline(myfile, line)) {
std::istringstream is_line(line);
std::string key;
if (std::getline(is_line, key, '=')) {
if(key.compare(type) == 0){
for(int i=0; i<size;i++){
std::getline(is_line,data[i],',');
}
}
}
}
float *fdata;
for(int i=0;i<size;i++){
fdata[i] = (float)atof(data[i].c_str());
}
return fdata;
}
And configFile.h:
#include <string>
#ifndef CONFIGFILE_H
#define CONFIGFILE_H
class configFile {
private:
static configFile* pinstance;
static std::string filename;
public:
static configFile* getInstance();
void setConfigFileName(std::string s);
float* getConfiguration(std::string type, int size);
protected:
configFile();
configFile(const configFile& orig);
};
#endif /* CONFIGFILE_H */
The symbols error I'm having:
Undefined symbols: "configFile::filename", referenced from:
configFile::configFile()in configFile.o
configFile::configFile()in configFile.o
configFile::setConfigFileName(std::basic_string, std::allocator >)in configFile.o
"configFile::pinstance", referenced from:
configFile::getInstance() in configFile.o
configFile::getInstance() in configFile.o "configFile::getConfiguration(std::basic_string
std::char_traits, std::allocator >, int)", referenced
from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
If it's necessary: main.cpp:
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <algorithm>
#include "configFile.h"
using namespace std;
int main(int argc, char** argv) {
configFile* cfg = configFile::getInstance();
string type = "tiempo";
float* tiem = cfg->getConfiguration(type,3);
for(int i=0; i< 3;i++){
printf( " %f ", tiem[i]);
}
}
I'm running a MaxOSX 10.6.8. Thanks in adavance
These two areas are the issue:
configFile::configFile(){
filename = "/Users/myfolder/NetBeansProjects/Oier_2/config.cfg";
}
void configFile::setConfigFileName(std::string s){
filename = s;
}
You've declared filename as a static data member, so its name must always be configFile::filename. If you meant to use a data member, you can simply remove the static definition.
The difference between static and non-static data members is that with static, only one variable exists, whereas with a non-static data member there is an instance of this variable per-class. So in this case, the consequence is that with static, each instance of configFile would reference the same file path, whereas removing static would make each configFile own its own filename and so have its own file path.
Related
I am making something in c++, it doesn't have any errors visible in Visual Studio code, but when I use g++ to be able to execute it, I get this error:
In file included from Main.cpp:6: In file included from ./Filechange/Filechange.hpp:1: ./Filechange/Filechange.cpp:14:24: error: expected expression
std::thread first ([&wtime,&f,&fn]() mutable {
^ Main.cpp:16:33: error: expected expression
OnFilechange("FileEvent", 0.5, [](char* txt){
^ 2 errors generated.
These are the files:
Main.cpp:
#include <lua.hpp>
#include <iostream>
#include <chrono>
#include <thread>
#include <stdio.h>
#include "Filechange/Filechange.hpp"
void wait(int seconds)
{
std::this_thread::sleep_for(std::chrono::seconds(seconds));
}
int main(int argc, char const *argv[])
{
lua_State *State = luaL_newstate();
OnFilechange("FileEvent", 0.5, [](char* txt){
std::cout << txt << std::endl;
});
lua_close(State);
return 0;
}
Filechange.cpp:
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
char* StringToChar(std::string str){
char* Array = new char[str.length() + 1];
strcpy(Array,str.c_str());
return Array;
}
void OnFilechange(const char *f, float wtime, void (*fn)(char* txt)){
std::thread first ([&wtime,&f,&fn]() mutable {
std::ifstream file(f);
std::string str;
std::string filecontents;
while (std::getline(file,str)){
filecontents += str;
filecontents.push_back('\n');
}
char* LastContents = StringToChar(filecontents);
char* CurrentContents = StringToChar(filecontents);
while (true){
if (wtime != 0){
std::this_thread::sleep_for(std::chrono::milliseconds(int(wtime*1000)));
}
filecontents = "";
while (std::getline(file,str)){
filecontents += str;
filecontents.push_back('\n');
}
CurrentContents = StringToChar(filecontents);
if (strcmp(LastContents, CurrentContents) != 0){
LastContents = StringToChar(filecontents);
fn(StringToChar(filecontents));
}
}
});
}
Filechange.hpp:
#include "Filechange.cpp"
#ifndef FILECHANGE_HPP
#define FILECHANGE_HPP
#include <thread>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <string>
#include <fstream>
void OnFilechange(const char *f,float wtime,void (*fn)(char txt));
#endif
There's also a extension less file named FileEvent which will change in the runtime using other code files.
The Filechange.cpp and Filechange.hpp are in a folder named "Filechange"
This function:
void OnFilechange(const char *f, float wtime, void (*fn)(char* txt))
expects a function pointer, and a lambda in g++ is not implemented as a function pointer. Instead, you should declare the function to take a std::function, as in:
void OnFilechange(const char *f, float wtime, std::function<void(char *)> fn)
You may also need #include <functional> to get the declaration of std::function.
use -std=c++17 in g++ if possible as g++ defaulted to c++98
My code gives:
undefined reference to `NgramTree::generateTree(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
collect2.exe: error: ld returned 1 exit status
error and I dont understand why.
Here is a sample of my code.
NgramTree.cpp
#include "NgramTree.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
void generateTree(string fileName, int n)
{
string line;
string ngram;
bool isWord = 1;
bool firstTime = 1;
ifstream myFile(fileName);
if (!myFile.is_open())
return;
...
NgramTree.h
#include <string>
class NgramTree {
public :
NgramTree (){ };
~NgramTree(){ };
void addNgram (std::string ngram );
int getTotalNgramCount ();
void printNgramFrequencies ();
bool isComplete ();
bool isFull ();
void generateTree(std::string fileName, int n);
};
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "NgramTree.h"
using namespace std;
int main(){
NgramTree tree;
tree.generateTree("example.txt", 3);
return 0;
}
You need to add the class name in the cpp file. Write this, so that the compiler knows which class the method belongs to:
void NgramTree::generateTree(string fileName, int n) {
...
Building target: Lab1
Invoking: MacOS X C++ Linker
g++ -o "Lab1" ./src/EclipseR.o ./src/EclipseR_test.o ./src/Row.o ./src/Rows.o
Undefined symbols for architecture x86_64:
"Row::AddColumn(int, std::__1::basic_string, std::__1::allocator >)", referenced from:
_main in EclipseR.o
Row.h
#fndef ROW_H_
#define ROW_H_
#include <iostream>
using namespace std;
Class Row {
public:
Row();
Row(std::string id);
string id;
string columns[24]
void AddColumn(int index, std::string value);
};
#endif
Row.cpp
#include "Row.h"
#include <iostream>
#include <string>
using namespace std;
string columns[24];
Row::Row(){
// TODO
}
Row::Row(string id) {
this->id = id;
cout << "ID: " << this->id;
}
Row::~Row()
{
//Deconstructor
}
void AddColumn(int index, std::string value)
{
columns[index] = value;
}
I figured out my issue.
in my Row.cpp class I need to change the following
void AddColumn(int index, std::string value){...}
to
void Row::AddColumn(int index, std::string value){...}
I transformed my program in a modular program becahse all was in one header.
But now there is a problem I don't know ..
Undefined symbols for architecture x86_64:
"Vehicule::createVehicule(std::string)", referenced from:
_main in main-aV2PNG.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
main.cpp
#include <iostream>
#include "Vehicule.h"
int main()
{
Vehicule *v=Vehicule::createVehicule("fr");
presenter(v);
return 0;
}
vehicule.h
#include <iostream>
class Vehicule
{
public:
virtual std::string getMarque() const =0;
friend std::ostream &operator<<(std::ostream &o,const Vehicule *v);
static Vehicule* createVehicule(std::string origine);
};
void presenter(const Vehicule *v)
{
std::cout << "Vehicule " << v << std::endl;
}
std::ostream &operator<<(std::ostream &o,const Vehicule *v)
{
return o << v->getMarque();
}
vehicule.cpp
#include "Vehicule.h"
#include "CreateurConcretRenault.h"
#include "CreateurConcretFiat.h"
Vehicule* Vehicule::createVehicule(std::string origine)
{
if(origine=="fr") return new CreateurConcretRenault();
else if(origine=="ita") return new CreateurConcretFiat();
else return new CreateurConcretRenault();
}
CreateurConcretRenault.h
#include <iostream>
#include "Vehicule.h"
class CreateurConcretRenault : public Vehicule
{
public:
std::string getMarque() const;
};
CreateurConcretRenault.cpp
#include "CreateurConcretRenault.h"
std::string CreateurConcretRenault::getMarque() const
{
return "Renault";
}
CreateurConcretFiat.h
#include <iostream>
#include "Vehicule.h"
class CreateurConcretFiat : public Vehicule
{
public:
std::string getMarque() const;
};
CreateurConcretFiat.cpp
#include "CreateurConcretFiat.h"
std::string CreateurConcretFiat::getMarque() const
{
return "Fiat";
}
You are using std::string, but you never included <string>.
I tried to put all in one header and it works ..
So I thought i forgot something
I am creating an interface called Index that will allow me to Index using either a hash table or a AVL tree. I am getting the following error:
Undefined symbols for architecture x86_64:
"IndexHash::IndexHash(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
_main in main.o
"IndexHash::IndexHash()", referenced from:
Parser::createIndex() in Parser.o
These are coming from the lines:
Index* mainIn;
// creates a new index pointer
if (indexType_ == 1) {
mainIn = new IndexAVL();
} else if (indexType_ == 2) {
mainIn = new IndexHash();
}
This worked when it was just the AVL tree.
Index.H
#ifndef INDEX_H
#define INDEX_H
#include <iostream>
#include <fstream>
#include <vector>
#include "WordEntry.h"
#include "Document.h"
class Index {
public:
virtual WordEntry* search(WordEntry*&)=0;
virtual void insert(WordEntry*&)=0;
virtual void insert(string)=0;
virtual void printOut(ofstream&)=0;
virtual void createDocument(string, string, string)=0;
virtual int getNumberDocs()=0;
virtual void trim(string&)=0;
private:
};
#endif
IndexHash.h
#ifndef INDEXHASH_H
#define INDEXHASH_H
#include <iostream>
#include <fstream>
#include <vector>
#include "WordEntry.h"
#include "IndexHash.h"
#include "Document.h"
#include <tr1/unordered_map>
class IndexHash : public Index{
public:
IndexHash();
IndexHash(string);
IndexHash(const IndexHash&);
~IndexHash();
virtual WordEntry* search(WordEntry*&);
virtual void insert(WordEntry*&);
virtual void insert(string);
virtual void printOut(ofstream&);
virtual int getNumberDocs();
virtual void trim(string&);
virtual void createDocument(string, string, string);
int searchDocs(string);
Document* binarySearch(vector<Document*>*, int, int, int);
void quickSort(vector<Document*>*, int, int);
private:
tr1::unordered_map< WordEntry*, vector<Document*> >* hashtable_;
vector<Document*>* seenDocs;
};
#endif /* INDEXHASH_H */
IndexHash.cpp
#include "IndexHash.h"
#include "Document.h"
#include <vector>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <istream>
using namespace std;
IndexHash::IndexHash() {
hashtable_ = new tr1::unordered_map< WordEntry*, vector<Document*> >;
seenDocs = new vector<Document*>;
}
... it defines everything, it is just hard for me to copy and paste it in here and I am assuming the problems is in includes.