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) {
...
Related
I'm starting with C++ and I'm trying to create a class program, however, when I try to run it appears the error: C++: Undefined reference to 'NameSpace::Class::Constructor. The code is shown below:
Header for ClassA:
//ClassA.h
#ifndef CLASSA_H
#define CLASSA_H
#include <string>
namespace ClassANameSpace
{
class ClassA
{
private:
std::string attribute1;
double attribute2;
public:
ClassA();
ClassA(std::string pAttribute1, double pAttribute2);
void setClassA(std::string pAttribute1, double pAttribute2);
std::string getAttribute1() { return attribute1; }
double getAttribute2() { return attribute2; }
};
}
#endif
Class A:
//ClassA.cpp
#include "ClassA.h"
using namespace ClassANameSpace;
ClassA::ClassA()
{
}
ClassA::ClassA(std::string pAttribute1, double pAttribute2)
{
setClassA(pAttribute1, pAttribute2);
}
void ClassA::setClassA(std::string pAttribute1, double pAttribute2)
{
attribute1 = pAttribute1;
attribute2 = pAttribute2;
}
Main:
// main.cpp
#include <iostream>
#include "ClassA.h"
using namespace std;
using namespace ClassANameSpace;
int main(int argc, char const *argv[])
{
ClassA classA ("string", 1.0);
return 0;
}
When I try to run it appears: undefined reference to `ClassANameSpace::ClassA::ClassA(std::__cxx11::basic_string<char, std::char_traits, std::allocator >, double)'
Someone can guide me to undestand what is going on?
Hello I am getting this Error when I am compiling my code:
main.cpp:(.text.startup+0xfc): undefined reference to `CMyMath::melFilterBank(std::vector<double, std::allocator<double> >, int, int, int)'
collect2: error: ld returned 1 exit status
make: *** [bin/main.elf] Error 1
my .h file:
#ifndef _MYMATH_H_
#define _MYMATH_H_
#define _USE_MATH_DEFINES
#include <vector>
#include <stdio.h>
#include <cmath>
#include <stdint.h>
#include <complex>
class CMyMath
{
public:
CMyMath();
~CMyMath();
std::vector<double> melFilterBank(std::vector<double> signal, int frequency, int band_num, int coef_num);
};
#endif
my .cpp file:
#include "MyMath.h"
CMyMath::CMyMath()
{
printf("constructor called\n");
}
CMyMath::~CMyMath()
{
printf("destructor called\n");
}
std::vector<double> melFilterBank(std::vector<double> ourSignal, int frequency, int bandNum, int coefNum)
{
std::vector<double> output; //ck in matlab code
/*
DO SOME STUFF
*/
return output;
}
main:
#include <stdio.h>
#include <vector>
#include <cmath>
#include "MyMath.h"
int main()
{
class CMyMath a;
std::vector<double> mel {0.0000001,0.0000005,0.0000004,0.0000005};
a.melFilterBank(mel,8000,6,5);
return 0;
}
What do you think where should be a mistake? I am new in C++ and I have really no idea what`s wrong. What do you suggest?
The definition (in the .cpp file) needs to specify that you're defining the member function, not a separate non-member function:
std::vector<double> CMyMath::melFilterBank(std::vector<double> ourSignal, int frequency, int bandNum, int coefNum)
^^^^^^^^^
std::vector<double> CMyMath :: melFilterBank(std::vector<double> ourSignal, int frequency, int bandNum, int coefNum)
Member Funtion while defining needs to be prefixed with class name.
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.
I am using forward declaration and now I am getting an error referring to the class that uses the forward declaration...so fInstance forward declares fConfig and then the Helper class (a namespace - used for global access to functions) - getting t
fConfig.h
#ifndef FCONFIG_H
#define FCONFIG_H
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <fcntl.h>
#include "JWDSFork.h"
//#include "fInstance.h"
using namespace std;
class fConfig
{
private:
pid_t pid, w;
public:
pid_t cPid;
string name;
int group;
int instanceId;
int numInstance;
int tries;
bool reply;
bool debug;
bool service;
bool currentlyRunning;
time_t startTime;
time_t endTime;
string path;
fConfig();
virtual ~fConfig();
void start();
string intToString(int);
char* stringToChar(string);
};
#endif // FCONFIG_H
fInstance.h
#ifndef FINSTANCE_H
#define FINSTANCE_H
//#include "fConfig.h"
#include <vector>
#include <sys/types.h>
#include <string>
using namespace std;
class fConfig;
class fInstance
{
public:
fConfig* config;
pid_t pid;
vector<string> notes;
vector<time_t> times;
fInstance();
virtual ~fInstance();
};
#endif // FINSTANCE_H
Helper.h
#ifndef HELPER_H
#define HELPER_H
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <sstream>
#include <limits.h>
#include "fInstance.h"
using namespace std;
namespace Helper
{
extern string APPDIR;
bool errorCheck(int, char*);
string charToString(char*, int);
string longToString(unsigned long);
bool Contains(vector<fInstance>, fInstance);
string convertInt(int);
string convertDouble(double);
bool Read(int, char*, size_t);
bool Write(int, char*, size_t);
};
#endif // HELPER_H
Helper.cpp
//Helper.cpp - function that causes a problem
#include "Helper.h"
namespace Helper
{
bool Contains(vector<fInstance> a, fInstance b)
{
for(unsigned int i= 0; i < a.size(); i++ )
{
if(a[i].config.name == b.config.name)
{
return true;
}
}
return false;
}
}
I am getting these errors
error: request for member ‘name’ in ‘a.std::vector<_Tp, _Alloc>::operator[] [with _Tp = fInstance, _Alloc = std::allocator<fInstance>](((long unsigned int)i))->fInstance::config’, which is of non-class type ‘fConfig*’
That's a pretty unfriendly error message, but what it means is that the config member is a pointer, so you need to use the -> operator instead, ie.
if(a[i].config->name == b.config->name)
Assuming that you have an operator== overloaded for your type fInstance, you can write your function as (note also that you should pass your parameters a and b by reference-to-const)
#include<algorithm>
bool fInstance::operator==(const fInstance& other)
{
return config->name == other.config->name;
}
bool Contains(const vector<fInstance>& a, const fInstance& b)
{
return std::find_if(a.begin(), a.end(), b);
}
If you don't have an operator== in your fInstance class, you can use a C++11 lambda expression
bool Contains(const vector<fInstance>& a, const fInstance& b)
{
return std::find_if(a.begin(), a.end(),
[](const fInstance& i) { return i.config->name == b.config->name; });
}
And even better yet, you should encapsulate the name member into a member function of fInstance:
std::string fInstance::name() { return config->name; };
bool Contains(const vector<fInstance>& a, const fInstance& b)
{
return std::find_if(a.begin(), a.end(),
[](const fInstance& i) { return i.name() == b.name(); });
}
This increases encapsulation, decreases compilation times and makes the implementation of the fInstance class opaque to its clients. Your current implementation leaves the fConfig implementation transparant to clients. This decrease in encapsulation is called a violation of the Law of Demeter.
I am getting an error when I declare a class:
#include <iostream>
#include "testing/test.h"
#include <string>
using namespace std;
int main(void)
{
test links;
string content="this is an string";
links.getcont(content);
}
test.h
#ifndef TEST_H_
#define TEST_H_
#include<string>
using namespace std;
class test {
public:
string getcont(string content);
};
#endif /* TEST_H_ */
test.cpp
#include "test.h"
#include <iostream>
using namespace std;
string getcont(string content)
{
cout << content;
return content;
}
When I run this I get this error:
undefined reference to test::getcont(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
Well , in your test.cpp file replace the getcont function for this
string test::getcont(string content){ //code here; }
The problem is that you are not saying that getcont is a member function of the test class.
Also, consider making it a const function and passing a const string reference
string
test::getcont( const string& content) const
{
return content;
}