Overloaded functions, redefinitions, C2371 and C2556 C++ - 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;

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!

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.

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

Combining C codes in the same R package

Say I have two independent cpp codes in two different R packages:
(please do not take these examples literally, these are meant to be
a minimal version of my question).
#include <algorithm>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
using Eigen::VectorXf;
using Eigen::VectorXi;
using Eigen::RowVectorXf;
float Fmedian(VectorXf& x){
const int n=x.rows();
const int half=(n+1)/2-1;
float med;
std::nth_element(x.data(),x.data()+half,x.data()+x.size());
if((n%2)==1){
med=x(half);
} else {
float tmp0=x(half);
float tmp1=x.segment(half+1,half-1).minCoeff();
med=0.5*(tmp0+tmp1);
}
return med;
}
extern "C"{
void R_Fastmedian(int* n,float* X,int* fMet){
MatrixXf xi=Map<VectorXf>(X,*n);
float Um=Fmedian(xi);
*fMet=Um;
}
}
then, in another file, i have:
#include <algorithm>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
using Eigen::VectorXf;
using Eigen::VectorXi;
using Eigen::RowVectorXf;
float Fmedian(VectorXf& x){
const int n=x.rows();
const int half=(n+1)/2-1;
float med;
std::nth_element(x.data(),x.data()+half,x.data()+x.size());
if((n%2)==1){
med=x(half);
} else {
float tmp0=x(half);
float tmp1=x.segment(half+1,half-1).minCoeff();
med=0.5*(tmp0+tmp1);
}
return med;
}
float Fmad(VectorXf& x,float& med){
const int n=x.rows();
const int half=(n+1)/2-1;
float mad;
x-=med;
x=x.cwiseAbs();
std::nth_element(x.data(),x.data()+half,x.data()+x.size());
if((n%2)==1){
mad=x(half);
} else {
float tmp0=x(half);
float tmp1=x.segment(half+1,half-1).minCoeff();
mad=0.5*(tmp0+tmp1);
}
return(mad*1.4826);
}
extern "C"{
void R_Fastmad(int* n,float* X,int* fMet){
MatrixXf xi=Map<VectorXf>(X,*n);
float U1=Fmedian(xi);
float U2=Fmad(xi,U1);
*fMet=U2;
}
}
Now, i want to combine these two functions in a package. When i will compile
the second code using R CMD, i will get an error for Fmedian to the effect
that this function is already defined in the first file. What is the most
straightforward way to link these two files together?
If I'm reading correctly, your Fmedian implementations are exactly the same in both files. But the compiler doesn't actually know that, for it they may be different and cause ambiguity, hence the error. To fix it, you should unify both implementations into one.
One way to do that would be this:
// the first file
#include <algorithm>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
using Eigen::VectorXf;
using Eigen::VectorXi;
using Eigen::RowVectorXf;
// Forward-declare Fmedian as an external function.
// This means that its definition will be found
// in some other file (C++ translation unit) during compilation.
extern float Fmedian(VectorXf& x);
extern "C" {
void R_Fastmedian(int* n,float* X,int* fMet){
MatrixXf xi=Map<VectorXf>(X,*n);
float Um=Fmedian(xi);
*fMet=Um;
}
}
And your second file remains the same.
See also Externing functions in C++, How does the linker know where is the definition of an extern function?