highInterestChecking Header:
#ifndef H_highInterestChecking
#define H_highInterestChecking
#include "noservicechargechecking.h"
#include <string>
class highInterestChecking: public noServiceChargeChecking
{
public:
highInterestChecking(std::string =" ",int = 0, double = 0.00, double = 0.00, double = 0.00);
};
#endif
highInterestChecking cpp:
#include "highInterestChecking.h"
using std::string;
highInterestChecking::highInterestChecking(string name, int acct, double bal, int numCheck, double min, double i)
{
bankAccount::setAcctOwnersName(name);
bankAccount::setAcctNum(acct);
bankAccount::setBalance(bal);
checkingAccount::setChecks(numCheck);
noServiceChargeChecking::setMinBalance(min);
noServiceChargeChecking::setInterestRate(i);
}
I have the error "No instance of overloaded function." under the constructor name highInterestChecking in the cpp file not sure what is causing it ive looked at it for a while now can't seem to find an error. maybe someone will help?
In the header you have:
highInterestChecking(std::string =" ",int = 0, double = 0.00, double = 0.00, double = 0.00);
Which takes 5 arguments, In the source file you have:
highInterestChecking::highInterestChecking(string name, int acct, double bal, int numCheck, double min, double i)
^^^^^^^^^^^
which takes 6 arguments. It seems like int numCheck does not match the header signature.
You have this constructor in the class declaration:
highInterestChecking(std::string =" ",int = 0, double = 0.00, double = 0.00, double = 0.00);
and this one in the class definition:
highInterestChecking::highInterestChecking(string name, int acct, double bal, int numCheck, double min, double i)
The parameter types from both parameter lists must match.
highInterestChecking::highInterestChecking(string name, int acct,
double bal, int numCheck, double min, double i)
//^^^
does not exist in your class's header file, header file has 5 parameters, but you have 6 in cpp file, parameter type seems mismatched,
Related
I find a solution to the equation using the bisection method.
And I need to find the value of a and b on some iterations. Therefore, I made two arrays for these points, respectively.
in order to "pull out" the number of iterations from the function, I had no problems. They are displayed on the screen. But how do I "pull out" these two arrays?Please tell me how to do it. Thank you in advance!
double f1(double x){
return x*x-5*sin(x);
}
double f2(double x){
return exp(x)-pow(10,x);
}
double f3(double x){
return sin(x)-x+0.15;
}
double f4(double x){
return x-pow(9+x,0.5)+x*x-4;
}
double dihotom (double a , double b , double e , double(*fp)(double),int &iter,double &points_a[],double &points_b[]){
double c , fc , fa = fp(a);
iter=(log10((b-a)/e))/log10(2);
int step = iter/3;
int step_current = step;
int it=0;
int k=0;
do{
c=(a+b)/2;
fc=fp(c);
if (fa*fc<=0) b = c ; else a = c;
it++;
if(it==step_current){
points_a[k]=a;
points_b[k]=b;
k++;
step_current=step_current+step;
}
fa=fp(a);
printf ("it %d: a = %lf,b = %lf\n",iter,a,b);
}while (fabs(a-b)>=e);
return c;
}
int main(int argc, char *argv[]) {
int int_s=0;
double points_a[3];
double points_b[3];
double k3= dihotom (0.5,1,0.0001,f3,int_s,points_a[3],points_b[3]);
printf("For F3 - root = %lf, F3(%.2lf)=%lf ;iter =%d\n", k3, k3 ,f3(k3),int_s);
int i=0;
for(i=0;i<3;i++){
printf("step : %d , a: %lf, b: %lf ", i,points_a[i],points_b[i]);
}
return 0;
}
In your case, you should take the arrays by reference:
double dihotom(double a, double b, double e, double (*fp)(double), int &iter,
double (&points_a)[3], double (&points_b)[3]) {
// ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^
You could also let the arrays decay into pointers:
double dihotom(double a, double b, double e, double (*fp)(double), int &iter,
double points_a[], double points_b[]) {
or
double dihotom(double a, double b, double e, double (*fp)(double), int &iter,
double* points_a, double* points_b) {
But by taking them by reference, you make sure that you only accept arrays of the correct size.
In either case, the call to the function will simply be:
double k3 = dihotom(0.5, 1, 0.0001, f3, int_s, points_a, points_b);
Demo
If you want to return two arrays from a function then make a struct with two vectors. Something like this. The way you are doing it is harder to maintain, who will allocate the arrays and delete them for example?
// todo for you : create better names then points_a and points_b
struct my_result_t
{
std::vector<double> points_a;
std::vector<double> points_b;
};
my_result_t function();
{
my_result_t result;
result.points_a.push_back(1.0);
result.points_b.push_back(2.0);
return result;
}
int main()
{
auto result = function();
std::cout << result.points_a[0];
}
I am writing a small utility class that transfor latitude and longitude coordinated into a UTM local system. For this task I am using this source. I created some struct to help me manage the majority of the data, but something is wrong if I pass data in a speciific way. It does work if I clearly re-state the values. See below the example:
zoneconverter.h
#ifndef ZONE_CONVERTER_H
#define ZONE_CONVERTER_H
#include <string>
#include <math.h>
#include <cmath>
#include <ctgmath>
#include <stdlib.h>
#include <stdio.h>
#include <stdexcept>
#define PI 3.14159265358979323846 /* pi */
#define SMaxA 6378137.0 /* semi major axis */
#define SMinA 6356752.314245 /* sdmi minor axis */
#define grid_size 100000.0 /* 100 km grid*/
struct Deg2Rad {
double D2R = PI/180.0;
};
struct Rad2Deg {
double R2D = PI*180.0;
};
// definition of the World Geodetic System 84
struct WGS84_DATA
{
double semi_major_axis_a = 6378137.0; // by definition
double semi_minor_axis_b = 6356752.314245; // by definition
const double flattening = (SMaxA-SMinA)/SMaxA; // by definition
const double first_eccentricity = 0.081891909; // by calculation
double second_eccentricity = 0.0820944377; // by calculation
double angular_velocity_earth = 72.92115e-6; // rad/s
double gravitational_constant = 3986004.418e8; // by definition
};
struct UTM_DATA
{
double point_scale_factor = 0.9996; // by convention
double equatorial_radius = 6378137.0; // meters also semi_major_axis_a
double inverse_flattening = 1/((SMaxA-SMinA)/SMaxA); // by convention
double northen_emisphere = 0.0; // meter
double southern_hemisphere = 10000000.0; // meter
double false_esting = 500000.0; // meter by convention
double first_eccentricity_power2 = 0.081891909*0.081891909;
double first_eccentricity_power4 = 0.081891909*0.081891909*0.081891909*0.081891909;
double first_eccentricity_power6 = 0.081891909*0.081891909*0.081891909*0.081891909*0.081891909*0.081891909;
};
enum UTMidentifierLeter {
X, W, V, U, T, S, R, Q, P, N,
M, L, K, J, H, G, F, E, D, C, Z
};
struct UTM_LETTER_ZONE { UTMidentifierLeter utmLetterZone; };
enum UTMIdentifierZone { NORWAY, SVALBARD };
struct UTM_ZONE { UTMIdentifierZone utmZone; };
class ZONE_converter
{
public:
ZONE_converter();
WGS84_DATA wgs84_data;
UTM_DATA utm_data;
Deg2Rad degreeToRad_reader;
Rad2Deg radToDeg_reader;
void UTM(double lat, double lon, double eastingUtmzone, double northingUtmzone);
char adjustForNorway(double lat);
char adjustForSvalbard(double lat, double lon);
char allOtherZones(double lat);
private:
UTM_LETTER_ZONE letter;
UTM_ZONE zone;
double latitude;
double longitude;
int current_zone;
};
#endif // ZONE_CONVERTER_H
zoneconverter.cpp is as following
#include "zone_converter.h"
ZONE_converter::ZONE_converter(){}
void ZONE_converter::UTM(double lat, double lon, double eastingUtmzone, double northingUtmzone)
{
double m0_a11 = (std::pow(wgs84_data.first_eccentricity, 4)/4);
double m0_a12 = (std::pow(wgs84_data.first_eccentricity, 4)/64);
double m0_a13 = (std::pow(wgs84_data.first_eccentricity, 6))/256;
double m0 = 1 - m0_a11 - 3*m0_a12 - 5*m0_a13;
double m1_a11 = (std::pow(wgs84_data.first_eccentricity, 2))/8;
double m1_a12 = (std::pow(wgs84_data.first_eccentricity, 4))/32;
double m1_a13 = (std::pow(wgs84_data.first_eccentricity, 6))/1024;
double m1 = -(3*m1_a11 + 3*m1_a12 + 45*m1_a13);
double m2_a11 = (std::pow(wgs84_data.first_eccentricity, 4))/256;
double m2_a12 = (std::pow(wgs84_data.first_eccentricity, 6))/1024;
double m2 = 15*m2_a11 + 45*m2_a12;
double m3_a11 = (std::pow(wgs84_data.first_eccentricity, 6))/3072;
double m3 = -35*m3_a11;
// calculation of the central meridian
int centralMeridian = ((lon >= 0.0)
? (static_cast<int>(lon) - (static_cast<int>(lon)) % 6 + 3)
: (static_cast<int>(lon) - (static_cast<int>(lon)) % 6 - 3));
double rlat = degreeToRad_reader.D2R;
double rlon = degreeToRad_reader.D2R;
double rlon0 = centralMeridian*degreeToRad_reader.D2R;
double slat = std::sin(rlat);
double clat = std::cos(rlat);
double tlat = std::tan(rlat);
double fn = (lat > 0) ? utm_data.northen_emisphere : utm_data.southern_hemisphere;
double T = tlat*tlat;
double C = (wgs84_data.first_eccentricity*wgs84_data.first_eccentricity)*clat*clat;
double A = (rlon - rlon0)*clat;
double M = (wgs84_data.semi_major_axis_a)*(m0*rlat + m1*std::sin(2*rlat) + m2*std::sin(4*rlat) + m3*std::sin(6*rlat));
// radius of curvature on the plane of the prime vertical
double Rn = wgs84_data.semi_major_axis_a/(std::sqrt(1 - std::pow((wgs84_data.first_eccentricity), 2)*slat*slat));
// radius of Curvature in the plane os the meridian
double Rc = ((wgs84_data.semi_major_axis_a)*(1 - ((wgs84_data.first_eccentricity)*(wgs84_data.first_eccentricity))))/(1 - ((wgs84_data.first_eccentricity)*(wgs84_data.first_eccentricity))*std::pow(std::sin(rlat), 2));
// computation of the easting-northing coordinate
eastingUtmzone = utm_data.point_scale_factor*Rn*(A + ((1-T+C)*(std::pow(A, 3)/6))+(5-18*T + std::pow(T,2) + 72*C - 58*(std::pow(wgs84_data.second_eccentricity, 2)))*(std::pow(A, 5))/120);
northingUtmzone = utm_data.point_scale_factor*((M - 0.0)+Rn*tlat*(((A*A)/2) + (((std::pow(A, 4))/24)*(5-T+9*C+4*C*C)) + (61 - 58*T + T*T + 600*C - 330*(std::pow(wgs84_data.second_eccentricity, 2))*((std::pow(A, 6))/720))));
(void) Rc;
(void) fn;
return;
}
main.cpp
#include <iostream>
#include "zone_converter.h"
using namespace std;
int main()
{
ZONE_converter convert;
double lat = 26.281742;
double lon = 92.142683;
double eastingUtmzone;
double northingUtmzone;
convert.UTM(lat, lon, eastingUtmzone, northingUtmzone);
std::cout<< lat << lon<< northingUtmzone<< eastingUtmzone<< std::endl;
return 0;
}
But I am trying to understand why if I write the function in the following way accessing the struct I created in header file I get a SIGSEV segmentation error:
void ZONE_converter::UTM(double lat, double lon, double eastingUtmzone, double northingUtmzone)
{
double m0_a11 = (std::pow(wgs84_data.first_eccentricity, 2)/4);
double m0_a12 = (std::pow(wgs84_data.first_eccentricity, 4)/64);
double m0_a13 = (std::pow(wgs84_data.first_eccentricity, 6))/256;
double m0 = 1 - m0_a11 - 3*m0_a12 - 5*m0_a13;
// ... additional operation
}
Can anyone shed light on this matter?
One issue is that you're using uninitialized variables here:
double eastingUtmzone; // uninitialized
double northingUtmzone; // uninitialized
convert.UTM(lat, lon, eastingUtmzone, northingUtmzone);
std::cout<< lat << lon<< northingUtmzone<< eastingUtmzone<< std::endl;
So there are at least two points of failure -- within the convert.UTM that uses these variables, and in the std::cout after the call.
Since utilizing uninitialized variables is undefined behavior, expect anything to happen, where one of those things seemingly is a SIGSEGV occurring.
Hello I am a student learning c++ and I am just starting to learn OOP. The problem is in my MAIN however I am showing all of my files in case it is from another file.
I have written my hpp and cpp file and now I am just working on my main for testing. The class is called Box and when I create an object box1 or box 2 and attempt to access my functions it says there are two few arguments. It says this regardless of whether I put box1.calcVolume(double h, double w, double l) or box1.calcVolume();
So the issue is on the line(s) that say:
double volume2 = box2.calcVolume();
double volume1 = box1.calcVolume();
double surfaceArea1 = box1.calcSurfaceArea();
If anyone can spot something that I missing our may not understand please let me know.
This is the header file:
#pragma once
#include <iostream>
#ifndef BOX_HPP
#define BOX_HPP
class Box
{
private:
double height;
double width;
double length;
public:
void setHeight(double h);
void setWidth(double w);
void setLength(double l);
double calcVolume(double h, double w, double l);
double calcSurfaceArea(double h, double w, double l);
Box();
Box(double height, double width, double length);
};
#endif
This is the CPP file
#include <iostream>
#include "Box.hpp"
Box::Box()
{
setHeight(1);
setWidth(1);
setLength(1);
}
Box::Box(double h, double w, double l)
{
setHeight(h);
setWidth(w);
setLength(l);
}
void Box::setHeight(double h)
{
height = h;
}
void Box::setWidth(double w)
{
width = w;
}
void Box::setLength(double l)
{
length = l;
}
double Box::calcVolume(double h, double w, double l)
{
double volume;
volume = h * w * l;
return volume;
}
double Box::calcSurfaceArea(double h, double w, double l)
{
double surfaceArea;
surfaceArea = 2 * (h*w) + 2 * (h*l) + 2 * (l*w);
return surfaceArea;
}
my BoxMain file:
#include <iostream>
#include "Box.hpp"
using std::cout;
using std::cin;
using std::endl;
int main()
{
Box box1(1.1, 2.4, 3.8);
Box box2;
box2.setHeight(12);
box2.setWidth(22.3);
box2.setLength(2.3);
double volume2 = box2.calcVolume();
double volume1 = box1.calcVolume();
double surfaceArea1 = box1.calcSurfaceArea();
cout << box1.calcVolume(); << endl; //testing different methods
return 0;
}
Your method takes three parameters:
double Box::calcVolume(double h, double w, double l)
{
double volume;
volume = h * w * l;
return volume;
}
So, you would call it like so:
Box b;
double volume = b.calcVolume(1, 2, 3);
But that's not quite right. An instance of Box knows how big it is, because you pass a size to the constructor, which stores the sizes in the fields width, height, and length. You probably want something like this:
double Box::calcVolume()
{
volume = height * width * length;
return volume;
}
You have the code wrong. The dimensions of the box are known during the creation of box object. The length, width and height are already available - updated in the member variables.
Functions calcVolume and calcSurfaceArea should not take arguments but return the computed value. Modified code below:
double Box::calcVolume()
{
return height*width*length;
}
double Box::calcSurfaceArea()
{
return 2*((height*width) + (height*length) + (length*width));
}
Also, remember to modify the .hpp file with the declarations corresponding to the code above.
Declaration in the .hpp file should be
double calcVolume();
double calcSurfaceArea();
I have solved the problem. I removed the arguments in calcVolume and calcSurfaceArea everywhere in my code and it resolved the error.
I'm trying to create a program to numerically integrate a function between two limits. I've created a minimal example (where the "integral" is always 1.0) to illustrate an error I get. The code below tries to use a function whose arguments are two doubles and a function from doubles to doubles:
#include <iostream>
#include <math.h>
double cons(double a, double b, double c(double d))
{
return 1.0;
}
double f(double x)
{
return x*x;
}
int main()
{
double x;
double I = cons(0, 1, f(x));
std::cout << I << "";
}
This results in an error on cpp.sh:
14:31: error: cannot convert 'double' to 'double ()(double)' for argument '3' to 'double cons(double, double, double ()(double))'
Obviously, the difference between doubles and double-valued functions is causing a problem here. How can I get this working?
You need to pass the function, not call it.
#include <iostream>
#include <math.h>
double cons(double a, double b, double c(double d))
{
return 1.0;
}
double f(double x)
{
return x*x;
}
int main()
{
double x;
double I = cons(0, 1, f);
std::cout << I << "";
}
You didn't pass a function but the result of a function, a double. Second, you didn't correctly declared a function pointer as argument.
If you want to pass a double then declare a double as argument:
double cons(double a, double b, double c)
{
return 1.0*a*b*c;
}
double f(double x)
{
return x*x;
}
int main()
{
double x;
double I = cons(0, 1, f(x));
std::cout << I << "";
}
If you want to pass a function (aka function pointer as C++ is not a functional language):
double cons(double a, double b, double (*c)(double d))
{
return 1.0*c(a);
}
double f(double x)
{
return x*x;
}
int main()
{
double x;
double I = cons(0, 1, f);
std::cout << I << "";
}
Your problem in this case is that you are calling the function with:
double I = cons(0, 1, f(x));
so you actually give cons the return value of f() instead of the actual function. So you need to write this insteed:
double I = cons(0, 1, f);
As an alternativ you could also use lambda expressions for your problem. For example something like this:
#include <iostream>
#include <math.h>
#include <functional>
double cons(double a, double b, const std::function<double(double)>& callback)
{
// do something
}
int main()
{
double x;
double I = cons(0, 1, [](double x){ return x*x});
std::cout << I << "";
}
When I try to compile this code I get these Errors:
Error 1 error C2061: syntax error : identifier 'stammdaten'
Error 2 error C2660: 'Test_Lohnab::Gehaltsrechner' : function does not take 1 arguments
Error 3 error C2511: 'int Test_Lohnab::Gehaltsrechner(stammdaten &)' : overloaded member function not found in 'Test_Lohnab'
It would be a big help, if someone could explain me what i did wrong.
This is my code:
//Test_Lohnab.h
#pragma once
#include <iostream>
#include <string>
class Test_Lohnab
{
public:
Test_Lohnab();
~Test_Lohnab();
int Gehaltsrechner(stammdaten &st);//, abrechnung &ab);
}
class stammdaten
{
public:
std::string name;
std::string strasse;
std::string ort;
….
public:
stammdaten();
~stammdaten();
stammdaten(std::string n, std::string s, std::string o, int st, int fa, int k, int a, double z, double kver, double pk, int mi, int pv, int os, int ps, int ki, double geb,
double jf, double jh, int G_c, int G_a_c, int r_c, int A_L_c, double U_1, double U_2, double bb_g);
}
//Test_Lohnab.cpp
#include "Test_Lohnab.h"
#include <iostream>
Test_Lohnab::Test_Lohnab()
{
}
Test_Lohnab::~Test_Lohnab()
{
}
stammdaten::stammdaten()
{
}
stammdaten::stammdaten(std::string n, std::string s, std::string o, int st, int fa, int k, int a, double z, double kver, double pk, int mi, int pv, int os, int ps, int ki, double geb,
double jf, double jh, int G_c, int G_a_c, int r_c, int A_L_c, double U_1, double U_2, double bb_g)
: name(n), strasse(s), ort(o), stkl(st), f(fa), krv(k), abv(a), zkf(z), kv(kver), pkpv(pk), mitag(mi), pvz(pv), ost(os), pvs(ps), kist(ki), gebjahr(geb), jfreib(jf), jhinzu(jh),
Gleitzone_check(G_c), Gleitzone_alt_check(G_a_c), rente_check(r_c), AN_Lst_check(A_L_c), U1(U_1), U2(U_2), bbg(bb_g)
{
}
int main()
{
system("PAUSE");
stammdaten Personstamm1{ "Klaus Müller", "Parkstrasse 12", "78578 Monopoly", 1, 1000, 1, 1, 0, 15.5, 0.0, 0, 1, 0, 0, 9, 1982, 0, 0, 0, 0, 0, 0, 1.7, 0.38, 0 };
/**/
Test_Lohnab Ablauf;
Ablauf.Gehaltsrechner(Personstamm1); //, Abrechnung1);
system("PAUSE");
/**/
}
int Test_Lohnab::Gehaltsrechner(stammdaten &Test1)//, abrechnung &Abrech1)
{
}
Your classes Test_Lohnab function int Gehaltsrechner needs an object of the type stammdaten which the compiler does not yet know about because it is defined afterwards. You can use forward declaration to inform him about it. Simply add the following line after your includes
class stammdaten;