Undefined reference error in C++ code - c++

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.

Related

Undefined symbols for architecture x86_64 in cpp

I have a programme that I am writing in Qt Creator, and I am having some compilation issues. This is the error it gives:
Undefined symbols for architecture x86_64:
"VehicleSizer::VehicleSizer(double, double, double, double, double, double, double, double, int)", referenced from:
_main in MSSTO_SimulationTester.cpp.o
Reading other questions I thought it would be due to the mismatch between the declared function definitions in the header and the source file, or that the static variables must be initialised, but I don't think that is the case here. Here is my main file:
#include "Vehicle/vehicleSizer.h"
#include "Vehicle/vehicleSizerAbstract.h"
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <string>
#include <boost/bind.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
int main( )
{
{
double totalBurnTimeUp = 100.;
double massFlowUpper = 80.;
double chamberPressure = 200000.;
double exitDiameterUp = 1.2;
double oxidizerOverFuel = 3.4;
double upperStageDiameter = 6;
double payloadMass = 1000;
double __landingburn = 1.0;
int __engines = 5;
UpperStage = std::shared_ptr< VehicleSizerAbstract >(
new VehicleSizer(totalBurnTimeUp,
__landingburn,
massFlowUpper,
chamberPressure,
exitDiameterUp,
oxidizerOverFuel,
upperStageDiameter,
payloadMass,
__engines));
std::shared_ptr<VehicleSizerAbstract> Vehicle;
}
return 0;
}
Here is my abstract header file
#ifndef VEHICLESIZERABSTRACT_H
#define VEHICLESIZERABSTRACT_H
#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <cmath>
class VehicleSizerAbstract
{
public:
// some declared virtual functions that return doubles
virtual ~VehicleSizerAbstract(){}
protected:
// some declared variables
};
#endif // VEHICLESIZERABSTRACT_H
Here is my header file
#ifndef VEHICLESIZER_H
#define VEHICLESIZER_H
#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <cmath>
#include "vehicleSizerAbstract.h"
class VehicleSizer : public VehicleSizerAbstract
{
public:
VehicleSizer(double m_ascentBurnTime, double m_landingBurnTime,
double m_massFlow, double m_chamberPressure,
double m_exhaustDiameter, double m_mixtureRatio,
double m_vehicleDiameter, double m_payloadMass,
int m_mainEngineAmount);
// some declared functions that return doubles
private:
// some declared void functions
};
#endif // VEHICLESIZER_H
Here is my cpp file:
#include "vehicleSizer.h"
VehicleSizer::VehicleSizer(double m_ascentBurnTime, double m_landingBurnTime,
double m_massFlow, double m_chamberPressure,
double m_exhaustDiameter, double m_mixtureRatio,
double m_vehicleDiameter, double m_payloadMass,
int m_mainEngineAmount)
{
// some functions
}
What am I missing? I have been looking through all of the similar questions and corresponding answers here but nothing seems to apply or to work. Thank you so much!

undefined reference to `NgramTree...'

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) {
...

C++: Working with Header and Source File, Source File does not compile

As in the title
I have a program containing of 3(5) files
main.cpp, Kessel.h, Kessel.cpp, other two are non important
My problem: When I change code in Kessel.cpp it does not get registered when i compile the program ... so I tried making an obvious error by deleting a ';' and it just says "Target is up to date."
I'm using Code::Blocks btw.
Header Kessel.h:
#ifndef _KESSEL_H_
#define _KESSEL_H_
class Kessel {
private:
double KesselTemperatur;
double KesselInhalt;
int XKoord;
int YKoord;
int Breite;
int Hoehe;
char *Name;
public:
Kessel(const char *Name, int X, int Y, int B=150, int H=150, double
Inhalt=0, double Temperatur=0);
~Kessel();
void Fuellen(double T2, double V2);
};
#endif // KESSEL_H
Source Kessel.cpp:
#include "Kessel.h"
#include "WinAdapt.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sstream>
#include <iostream>
void Kessel::Fuellen(double T2, double V2){
double T1 = Kessel::KesselTemperatur;
double V1 = Kessel::KesselInhalt;
KesselTemperatur = (T1*V1+T2*V2)/(V1+V2);
KesselInhalt+=V2;
}
Kessel::Kessel(const char *Name, int X, int Y, int B=150, int H=150, double
Inhalt=0, double Temperatur=0)
: XKoord{X}, YKoord{Y}, Breite{B}, Hoehe{H}, KesselInhalt{Inhalt},
KesselTemperatur{Temperatur}
{
Kessel::Name = new( char[ strlen( Name )+1 ] );
strcpy( Kessel::Name, Name );
}
Kessel::~Kessel(){
delete []Name;
}
and main.cpp:
#include "Kessel.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <ctime>
//Kessel k1("Kessel1",10,10,130,100,10,30);
//Kessel k2("Kessel2",10,200,130,130,10,70);
//Kessel k3("Kessel3",200,10,10,70);
//Kessel k4("Kessel4",400,10);
When I un-comment Kessel k1 I get the error:
undefined reference to 'Kessel::~Kessel()'
Needs help pls :/
I solved the problem by right clicking on the Kessel.cpp Headline in Code::Blocks then
properties -> Build -> checking the "DebugWindows" under "Belongs in targets"

pthread_create() "class not declared in the scope?

I have a class called pos... I am trying to poll a method from this class. I used pthread_create(pthread_t thread, pos::Pirnt_data,this);
I get an error that pos is not declared in the scope... I included the h file of pos but I don't understand. I think I am using a wrong format can somebody help me
#include "position.h"
#include "pthread.h"
#include "pos.h"
void position::tick(schedflags_t flags)
{
if(pthread_create(&thread,NULL,pos::Print_data,this)!=0) {
stringstream bad;
bad << "OPIMex: Could not create listener thread: "
}
this class position has method tick that runs every 1 second with the data. I am trying to poll a method Print data from the class pos but it gave me that error any ideas why ?
this is class pos.h
#ifndef POS_H_
#define POS_H_
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <gps.h>
#include <string.h>
#include <pthread.h>
#include <string>
#include <vector>
#include <strings.h>
#include <math.h>
using namespace std;
namespace herpderp {
namespace modules {
int UBX_step =0;
long data;
int UBX_class;
int UBX_id=0;
int UBX_payload_length_hi;
int UBX_payload_length_lo;
int UBX_payload_counter =0;
int ck_a;
int ck_b;
int GPS_timer;
int fd;
unsigned int UBX_buffer[35];
int payload_data;
long lat=0;
long lon=0;
long alt_MSL=0;
long iTOW=0;
long alt=0;
unsigned long LastMS;
int UBX_Read;
vector <float> v;
fstream myfile;
int Open_port(void);
int read_tofile();
long join_4_bytes( unsigned int Buffer[]);
void parse_ubx_gps(void);
void checksum(char ubx_data);
void Print_data();
int push_data_into_vector();
int decode_gps();
int Configure_gps();
int test();
int Close_NEMA();
int Open_UBX();
}
}
#endif //POS_H_
pthread_kill is not on pthread.. It is on signal.h
#include <signal.h>
1) you can provide some code snippet/additional information to help you better.
2) If you are getting linkage error, check if you have linked with -lpthread library.
Form the pos.h it seems that there is no class called pos and you just need to call the function name:
if(pthread_create(&thread,NULL,Print_data,this)!=0) {

Linking error in C++

Problem fixed. Thanks a lot!
I am having the following error in the code shown below:
Error is as follows:
$ g++ main.cpp Neighbor.cpp Graph.cpp
/tmp/ccclDcUN.o: In function main':
main.cpp:(.text+0xc1): undefined reference toGraph::add(int, Neighbor&)'
main.cpp:(.text+0xd3): undefined reference to `Graph::add(int, Neighbor&)'
collect2: ld returned 1 exit status
what could be going wrong?
// FILENAME: Graph.cpp
#include "Neighbor.h"
#include "Graph.h"
template <typename NS>
void Graph<NS>::add(int id,NS& n){
if(id>=adj_list.size())
while(adj_list.size()<id+1)
adj_list.push_back(list<NS>());
adj_list[id].push_back(n);
}
template <typename NS>
void Graph<NS>::remove(int id,NS& n){
if(id<adj_list.size()){
adj_list[id].remove(n);
}
}
// FILENAME: Graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include "utils.h"
#include <vector>
#include <list>
class Neighbor;
template <typename NS>
class Graph {
private:
std::vector<std::list<NS> > adj_list;
public:
void add(int,NS&);
void remove(int,NS&);
inline typename std::vector<std::list<NS> >::iterator begin() { return adj_list.begin(); }
inline typename std::vector<std::list<NS> >::iterator end() { return adj_list.end(); }
};
#endif
// FILENAME: Neighbor.cpp
#include "Neighbor.h"
#include <iostream>
Neighbor::Neighbor(int id,float e,float p):id(id),edge_cost(e),price(p){}
bool operator==(const Neighbor& n1,const Neighbor& n2) {
if(&n1==&n2) return true;
return false;
}
ostream& operator<<(ostream& ostr,const Neighbor& n1) {
ostr<<"["<<n1.id<<","<<n1.price<<","<<n1.edge_cost<<"]";
return ostr;
}
// FILENAME: Neighbor.h
#ifndef NEIGHBOR_H
#define NEIGHBOR_H
#include <iosfwd>
class Neighbor {
private:
int id;
float edge_cost;
float price;
public:
Neighbor(int,float,float p=0.0);
friend bool operator==(const Neighbor&,const Neighbor&);
friend std::ostream& operator<<(std::ostream&,const Neighbor&);
};
#endif
// FILENAME: utils.h
#ifndef UTILS_H
#define UTILS_H
#include <iostream>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
namespace utility {
typedef std::pair<int,int> ii;
typedef std::vector<int> vi;
typedef std::vector<ii> vii;
typedef std::vector<vii> vvii;
typedef std::stack<int> si;
typedef std::queue<int> qi;
}
#define UTILITY_TR(c,i) for(typeof((c).begin()) i = (c).begin() ; i!=(c).end() ; ++i )
#define UTILITY_ALL(c) (c).begin(),(c).end()
#define UTILITY_CPRESENT(c,x) (find(all(c),x) != (c).end())
#endif
// FILENAME: main.cpp
#include "utils.h"
#include "Neighbor.h"
#include "Graph.h"
using namespace std;
int main() {
Graph<Neighbor> graph;
Neighbor n1(1,10);
Neighbor n2(0,10);
graph.add(0,n1);
graph.add(1,n2);
cout<<"Printing graph"<<endl;
cout<<"--------------"<<endl;
UTILITY_TR(graph,it) {
UTILITY_TR(*it,n) {
cout<<*n<<endl;
}
}
};
What I usually do is manually verify the symbol exists in the library:
objdump --syms foo.o
This will output a list of symbols contained in the .o file... (since it's a link error, you should have .o files... (make sure you pass -c to g++ to get it to stop after compilation))... Then you can just visually verify the object has the symbols you think it does...
You need to have the definition of Graph's functions (add and remove) in the .h file so that the linker can find it.
I try to think of templates like envelopes. It's nonsensical to send it (compile) before you put in a letter (defined type). Seeing as cpp files are what is compiled, it makes sense that there shouldn't be cpp files for templated types.
HTH!