Undefined symbols for architecture x86_64 in cpp - c++

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!

Related

Included Class Type is apparently not declared, why?

I have the following issue thrown by the compiler:
include/FlowChannel.h:14:21: error: ‘LatticeCell’ was not declared in this scope
std::vector grid;
when having these 3 header files (LatticeCell.h, FlowChannel.h and Utilities.h) and 2 cpp files including them(lbm.cpp and Utilities.cpp):
LatticeCell.h
#ifndef LATTICECELL_H
#define LATTICECELL_H
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
/* Single cell */
using namespace std;
class LatticeCell{
private:
std::vector<double> matrix = {0,0,0,0,0,0,0,0,0};
unsigned int type; //fluid, no-slip, velocity or density
public:
//Constructor
LatticeCell(unsigned int inType){
type = inType;
}
};
#endif
FlowChannel.h
#ifndef FLOWCHANNEL_H
#define FLOWCHANNEL_H
#include <vector>
#include <string>
#include <cmath>
#include <iostream>
#include "LatticeCell.h"
using namespace std;
class FlowChannel{
private:
std::vector<LatticeCell> grid; //ERROR LINE
unsigned int dimX = -1;
unsigned int dimY = -1;
public:
FlowChannel(unsigned int nx, unsigned int ny){
dimX = nx+2;
dimY = ny+2;
unsigned int gridSize = dimX*dimY;
grid.reserve(gridSize);
initGrid(/*TODO Params*/);
}
};
#endif
lbm.cpp
#include <string>
#include <vector>
#include "LatticeCell.h"
#include "FlowChannel.h"
#include "Utilities.h"
int main(int argc, char** argv){
printsomething();
return 0;
}
Utilities.cpp
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <string>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
void printsomething(){
cout << "something" << std::endl;
}
double calcRelaxationTime(unsigned int ny , double reynolds, double uin){
return 3.0 * (uin * ny / reynolds) - 0.5;
}
Utilities.h
#ifndef UTILITIES_H
#define UTILITIES_H
#include "LatticeCell.h"
#include "FlowChannel.h"
#include <vector>
#include <cmath>
void printsomething();
#endif
Further my compiler flags are:
-Wall -std=c++17 -pedantic
For some reason I can't figure out, why LatticeCell wouldnt be a declared class in FlowChannel, due to it being included. Do you guys know whats wrong?
Edit: I added lbm.cpp, Utilities.cpp and Utilities.h so you guys see the full scope of the problem
You should check if the files are in the same directory.
I copy and paste your code in VS 2019 and it work for me,
here are the pictures
FlowChannel LatticeCell
It seems, that deleting #include 'LatticeCell.h' everywhere but in FlowChannel.h. I dont get the error 100% to be honest, as this wouldn't execatly cause an include loop that would induce such an error, but it works.

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"

Undefined reference error in C++ code

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.

No predefined constructor existing C++

I have been spending hours on this but I can't seem to find a solution to this problem.
I have am working with two header files, one is Load.h and one is Source.h.
This is my load.h:
#ifndef LOAD_H
#define LOAD_H
#include <string>
#include "Complexnumbersfrompreviousweek.h"
#include "Otherfunctionsfrompreviousweek.h"
#include "Source.h"
class Load : public Source //I'm doing this to inherit the vs term
{
private:
double load;
double vload;
double ApparentP;
public:
Load (double, double, double, double);
double Calcvload (double, double, double, double);
};
#endif LOAD_H
This is my Source.h:
#ifndef SOURCE_H
#define SOURCE_H
#include <string>
#include "Complexnumbersfrompreviousweek.h"
#include "Otherfunctionsfrompreviousweek.h"
class Source {
public:
double vs;
Source(double);
double Ret(double);
};
#endif SOURCE_H
And this is my second .cpp file:
#include "Line.h"
#include "Load.h"
#include "Source.h"
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
Source::Source(double VoltageS)
{
VoltageS = vs;
};
double Source::Ret(double vs)
{
return vs;
}
Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor
{
Z = load;
Sl = ApparentP;
Vl = vload;
VoltageS = vs;
};
The error I get is error C2512: 'Source' no predefined appropriate constructor available.
This is what I am doing in my main():
Source Sorgente(VoltageS);
Load loadimpedance(VoltageS, Sl, Z, Vl);
So basically I am creating the "Sorgente" object using VoltageS as an argument (selected by the user, I didn't put that bit of code in) and I am trying to assign it to Vs in order to use it in the constructor for the Load afterwards...
Thank you in advance for your help!
Since Load inherits from Source, it has to construct the Source base in its constructor:
Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor
{
Since you don't explicitly specify one, the compiler will automatically insert the default:
Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor
: Source() // implicitly inserted by compiler
{
But that constructor doesn't exist - hence the error. To fix this, you need to explicitly call the correct constructor:
Load::Load(double VoltageS, double Sl, double Z, double Vl)//Constructor
: Source(VoltageS) // explicitly construct the base
{
Unrelatedly, in your Source constructor you are assigning the wrong element:
Source::Source(double VoltageS)
{
VoltageS = vs; // you are assigning to the temporary instead of your member
}
That should be:
Source::Source(double VoltageS)
: vs(VoltageS)
{ }

Overloaded functions, redefinitions, C2371 and C2556 C++

Ok so I've got 3 files:
definitions.h which contains
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
char type; //polar or rectangular
double real; //real value
double imaginary; //imaginary value
double length; //length if polar
double angle; //angle if polar
public:
//constructors
Complex();
~Complex();
void setLength(double lgth){ length=lgth;}
void setAngle(double agl){ angle=agl;}
double topolar(double rl, double img, double lgth, double agl);
#endif
functions.cpp which contains
#include "Class definitions.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#include <math.h>
#include <cmath>
#include <vector>
using namespace std;
Complex::topolar(double rl, double img, double lgth, double agl)
{
real=rl;
imaginary=img;
lgth = sqrt(pow(real,2)+pow(imaginary,2));
agl = atan(imaginary/real);
Complex::setLength(lgth);
Complex::setAngle(agl);
return rl;
return img;
return lgth;
return agl;
}
and the main programme contains:
#include "Class definitions.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#include <cmath>
#include <vector>
using namespace std;
int main(){
vector<Complex> v;
Complex *c1;
double a,b,d=0,e=0;
c1=new Complex;
v.push_back(*c1);
v[count].topolar(a,b,d,e);
But the I keep getting error C2371: redefinition; different basic types
and C2556: overloaded function differes only by return type
everything i have found online says to make sure the function.cpp file isnt included in the main but as I haven't made that mistake I'm running out of ideas, especially seeing as all my other functions that are set up in the same way (with seperate definition and declaration) do work.
Any help would be great!
Thanks
H
x
As declared topolar function should return double, but the definition in functions.cpp doesn't says that
Complex::topolar(double rl, double img, double lgth, double agl)
{
try changing this to
double Complex::topolar(double rl, double img, double lgth, double agl)
{
Your topolar function is defined as returning double but the implementation has no return type. I am not sure if this is the error, but it certainly is an error. You need
double Complex::topolar(double rl, double img, double lgth, double agl)
in the implementation.
Furthermore, you seem to have many return statements in the implementation. This is also an error. Only the first one will have effect:
return rl; // function returns here. The following returns are never reached.
return img;
return lgth;
return agl;