No predefined constructor existing C++ - 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)
{ }

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!

vector<string> declaration in c++

I'm very new to c++, so I'm sorry, if this solution is very trivial. I was searching this site for a solution, but didn't find something.
I need to do some thermodynamic calculus and therefore initialice my class with some information. This code breaks when I compile it because of my components_i.
I'm looking for a way to pass a vector to my class or an other way to provide my calculus in the class with the information about the mixture I want to simulate.
h-file:
#ifndef BASE_H
#define BASE_H
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Base
{
public:
vector<string> components_i;
int nComp,nPhase;
long double T,P,N_p_i,x_p_i;
Base( vector<string>,int,int,long double,long double,long double );
};
#endif
and as cpp:
#include "Base.h"
Base::Base( vector<string> components_i,int nComp, int nPhase, long double T, long double P, long double N_p_i ) {
Base::components_i = components_i //e.g. H2O and Ethanol
Base::nComp = nComp;
Base::nPhase = nPhase;
Base::T = T;
Base::P = P;
Base::N_p_i = N_p_i;
}
This doesnt work at all, but I think it makes more clear, what I want to do
Any suggestions?
You did not declare your static member. Static members need to be declared explicitly outside of class declaration, add this:
vector<string> Base::components_i;
Full code should be something like:
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Base
{
public:
static vector<string> components_i;
int nComp,nPhase;
long double T,P,N_p_i,x_p_i;
Base( vector<string>,int,int,long double,long double,long double );
};
vector<string> Base::components_i;
Base::Base(vector<string> components_i,int nComp, int nPhase, long double T, long double P, long double N_p_i ) {
Base::components_i = components_i; //e.g. H2O and Ethanol
Base::nComp = nComp;
Base::nPhase = nPhase;
Base::T = T;
Base::P = P;
Base::N_p_i = N_p_i;
};
int main() {
vector<string> vec;
vec.push_back(string("hello"));
Base base = Base(vec, 1, 1, 2.0, 2.0, 3.0);
printf("%s", Base::components_i[0].c_str());
}
Here is a working example:
http://cpp.sh/9xkfk

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"

c++ Class Members Returning Odd values

I will start this out by saying c++ is my first programming language and I am a beginner at best. And I am sure this has some sort of obvious answer.
But for some reason this simple program with a single custom class is returning very odd values from the Get functions that are calling the private members of the one custom class.
The program is separated into three file as follows.
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include "Shapes.h"
using namespace std;
int main ()
{
double test=20.0;
Cube D(test);
cout<< D.GetSA()<<endl<<D.GetSide();
return 0;
}
then as header files for the one Classes called Shapes.h
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
using namespace std;
class Cube
{
public:
Cube();
Cube(double);
double GetSA() const;
double GetSide() const;
private:
double SA;
double V;
double Side;
};
And another file Called Shapes.cpp that contains the Constructor.
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include "Shapes.h"
Cube::Cube()
{
V=0.0;
SA=0.0;
Side=0.0
}
Cube::Cube(double Side2)
{
Side=Side2;
}
double Cube::GetSA() const
{
return SA;
}
double Cube::GetSide() const
{
return Side;
}
for some reason when this program is run it returns a value of 6.95293e-310 for the GetSA accessor function and returns a value of 200 for the side function.
Any ideas on why this is happening and how to fix it?
Try invoking the default constructor. It seems to initialize the data members correctly. Your parameterized constructor is only initializing the Side data member, and not any other data members.

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;