Linking class header, class implementation, and driver issue - c++

I can't seem to get this to work without the driver including the quadratic.cpp file. I have it set up as a project in Dev C++ but keep getting errors saying undefined reference to all of my functions called in the driver.
quadratic.h
#include <iostream>
using namespace std;
class Quadratic{
private:
float a,b,c;
public:
//Quadratic();
Quadratic();
Quadratic(float a,float b,float c);
float x;
float get_a();
float get_b();
float get_c();
float evaluate(float);
void set();
int numRoots(Quadratic &qTest);
float roots1(int numRoots, Quadratic &qRoots);
float roots2(int numRoots, Quadratic &qRoots);
};
Quadratic operator+(const Quadratic &q1,const Quadratic &q2);
Quadratic operator*(double r, const Quadratic& q);
quadratic.cpp
#include<iostream>
#include "quadratic.h"
#include <cmath> // std::sqrt(double)
using namespace std;
//Default Constructor
Quadratic::Quadratic():a(0),b(0),c(0){}
//Had to add this to get the overloaded + operator to work
//(mainly because of the way I had it return the new instance)
Quadratic::Quadratic(float aSum, float bSum, float cSum)
{
this->a = aSum;
this->b = bSum;
this->c = cSum;
}
float Quadratic::get_a()
{
return a;
}
float Quadratic::get_b()
{
return b;
}
float Quadratic::get_c()
{
return c;
}
float Quadratic::evaluate(const float x)
{
return ((a*(x*x))+(b*x)+c);
}
void Quadratic::set()
{
float aNew=0.0,bNew=0.0,cNew=0.0;
cout<<"Enter a new a: ";
cin>>aNew;
cout<<"Enter a new b: ";
cin>>bNew;
cout<<"Enter a new c: ";
cin>>cNew;
this->b = bNew;
this->c = cNew;
this->a = aNew;
}
int Quadratic::numRoots(Quadratic &qTest)
{
float a= qTest.get_a();
float b= qTest.get_b();
float c= qTest.get_c();
int numRoots=0;
if ((a==0)&&(b==0)&&(c==0))
{
//every value of x = real root, so infinity
numRoots=3;
}
else if((a==0)&&(b==0)&&(c!=0))
{
numRoots=0;
}
else if((a==0)&&(b!=0))
{
numRoots=1;
//root is x= -c/b
}
else if((a!=0)&&( (b*b)<(4*a*c) ))
{
numRoots=0;
}
else if((a!=0)&&( (b*b)==(4*a*c) ))
{
numRoots=1;
//root is x= -b/2a
}
else if((a!=0)&&( (b*b)>(4*a*c) ))
{
numRoots=2;
//root is x= +/- quadratic formula
}
return numRoots;
}
float Quadratic::roots1(int numRoots, Quadratic &qRoots)
{
float root1=0.0;
float rootPlus=0.0;
float rootMinus=0.0;
if (numRoots==1)
{
if((a==0)&&(b!=0))
{
root1 = (((0-c)/b));// + ((0-c)%b));
}
if((a!=0)&&( (b*b)==(4*a*c) ))
{
root1 = (((0-b)/(2*a)));// + ((0-b)%(2*a)));
}
}
else if (numRoots == 2)
{
rootMinus= (((0-b)-sqrt((b*b)-(4*a*c)))/(2*a));
rootPlus= (((0-b)+sqrt((b*b)-(4*a*c)))/(2*a));
if (rootMinus > rootPlus) root1=rootPlus;
else root1=rootMinus;
}
else if (numRoots == 3) root1=0;
return root1;
}
float Quadratic::roots2(int numRoots, Quadratic &qRoots)
{
float root2=0.0;
float rootPlus=0.0;
float rootMinus=0.0;
if (numRoots==1)
{
if((a==0)&&(b!=0))
{
root2 = (((0-c)/b));// + ((0-c)%b));
}
if((a!=0)&&( (b*b)==(4*a*c) ))
{
root2 = (((0-b)/(2*a)));// + ((0-b)%(2*a)));
}
}
else if (numRoots == 2)
{
rootMinus= (((0-b)-sqrt((b*b)-(4*a*c)))/(2*a));
rootPlus= (((0-b)+sqrt((b*b)-(4*a*c)))/(2*a));
if (rootMinus < rootPlus) root2=rootPlus;
else root2=rootMinus;
}
else if (numRoots == 3) root2=0;
return root2;
}
Quadratic operator+(Quadratic &q1,Quadratic &q2)
{
float tempa,tempb,tempc=0.0;
tempa= q1.get_a() + q2.get_a();
tempb= q1.get_b() + q2.get_b();
tempc= q1.get_c() + q2.get_c();
Quadratic temp(tempa, tempb,tempc);
return temp;
// return Quadratic( q1.get_a() + q2.get_a(),
// q1.get_b() + q2.get_b(),
// q1.get_c() + q2.get_c());
}
Quadratic operator*(double r, Quadratic& q)
{
return Quadratic( q.get_a() * r,
q.get_b() * r,
q.get_c() * r);
}
main.cpp
#include<iostream>
//#include "quadratic.cpp""
#include "quadratic.h"
using namespace std;
int main()
{
//Default constructors called, all 3 coefficients set to 0
Quadratic q1;
Quadratic q2;
cout<<"Default constructor called\n"
<<"q1 a = "<<q1.get_a()<<"\n"
<<"q1 b = "<<q1.get_b()<<"\n"
<<"q1 c = "<<q1.get_c()<<"\n"
<<"q2 a = "<<q2.get_a()<<"\n"
<<"q2 b = "<<q2.get_b()<<"\n"
<<"q2 c = "<<q2.get_c()<<"\n";
//Call the set function to set all 3 coefficents of existing
//quadratic expression to new values
q1.set();
q2.set();
cout<<"\nSet function called. New values:\n"
<<"q1 a = "<<q1.get_a()<<"\n"
<<"q1 b = "<<q1.get_b()<<"\n"
<<"q1 c = "<<q1.get_c()<<"\n"
<<"q2 a = "<<q2.get_a()<<"\n"
<<"q2 b = "<<q2.get_b()<<"\n"
<<"q2 c = "<<q2.get_c()<<"\n";
//calls function to evaluate the quadratic expression for a given calue of x
cout<<"\nEnter x: ";
float x=0.0;
cin>>x;
cout<<"for q1, ax^2 + bx +c = "<<q1.evaluate(x)
<<"\nfor q2, ax^2 + bx +c = "<<q2.evaluate(x);
//add q1+q2 with overloaded + operator
Quadratic quad_sum = q1 + q2;
cout<<"\n\nAdding q1+_q2\n"
<<"\nq1_a + q2_a= "<<quad_sum.get_a()
<<"\nq1_b + q2_b= "<<quad_sum.get_b()<<" "
<<"\nq1_c + q2_c= "<<quad_sum.get_c();
//multiply the coefficients by r using overloaded * operator
cout<<"\n\nEnter a number to multiply the coefficeints by: ";
double r=0.0;
cin>>r;
Quadratic quad_prod=r*quad_sum;
cout<<"\nMultiplying q1 by r results in "
<<"\nnew a = "<<quad_prod.get_a()
<<"\nnew b = "<<quad_prod.get_b()
<<"\nnew c = "<<quad_prod.get_c();
//calculate number of and display roots
int numroots=q1.numRoots(q1);
cout<<"\n\nNumber of Roots for q1 = "<<numroots;
if (numroots != 0) cout<<"\nThey are: "<<q1.roots1(numroots, q1)<<" and "<<q1.roots2(numroots, q1);
}
Added
#ifndef QUAD_H_
#define QUAD_H_
and
#endif
to the header file, but still get "undefined reference" for every function call in the driver program.

Related

LawOfCosines solving for c, but getting odd answer

I have been trying to code a program that can solve for c using the Law Of Cosines. The program runs correctly, but the answer I get is ridiculously big, noted by how it was in scientific notation.
Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
class TrigMath
{
private:
double a;
double b;
double y;
public:
double LawOfCos()
{
return sqrt(pow(a,2) + pow(b,2) - 2*a*b*cos(y));
}
void seta(double A)
{
A = a;
}
void setb(double B)
{
B = b;
}
void sety(double Y)
{
Y = y;
}
};
int main()
{
TrigMath triangle1;
triangle1.seta(3);
triangle1.setb(4);
triangle1.sety(60);
cout << "c is equal to " << triangle1.LawOfCos() << endl;
return 0;
}
The cos() function there takes input as radians not as degrees.
Try to convert degrees to radians and then supply it as input.
In the class functions seta, setb and sety you have written A = a, B = b and Y = y.
You have to change them to a = A, b = B and Y = y.
So after applying all the changs the code should be like
#include <iostream>
#include <cmath>
using namespace std;
class TrigMath
{
private:
double a = 0;
double b = 0;
double y = 0;
public:
double LawOfCos()
{
return sqrt(pow(a,2) + pow(b,2) - 2*a*b*cos(y));
}
void seta(double A)
{
a = A;
}
void setb(double B)
{
b = B;
}
void sety(double Y)
{
y = Y*3.14/180;
}
};
int main()
{
TrigMath triangle1;
triangle1.seta(3.0);
triangle1.setb(4.0);
triangle1.sety(60.0);
cout << "c is equal to " << triangle1.LawOfCos() << endl;
return 0;
}

delete object from vector c++ attempting to reference deleted function

I've been looking around to see if anyone has a similar issue to me and i couldn't find anything. I am prompting the user to type in the name of the planet they wish to delete. My function will locate the position of the planet within the vector objects.
The purpose of this function is to delete a object based on the position i pass through the function parameters.
Planet Class
class Planet {
private:
string name;
double diameter;
double mass;
public:
const double G = 6.67408e-11;
void setName(string n);
bool setDiameter(double d);
bool setMass(double m);
string getName();
double getDiameter();
double getMass();
double CalcSa();
double CalcV();
double CalcDensity();
double CalcG();
string InputS(string x);
double InputD(string x);
Planet();
};
double ReadDouble(double input) {
//Verifys that that user entered in a correct number
while (cin.fail() != 0) {
cerr << "Enter a valid number: ";
cin.clear();
cin.ignore(255, '\n');
cin >> input;
}
return input;
}
string Planet::InputS(string x) {
string user_input;
cout << x;
cin >> user_input;
return user_input;
}
double Planet::InputD(string x) {
double user_input;
cout << x;
cin >> user_input;
user_input = ReadDouble(user_input);
return user_input;
}
Planet::Planet() {
name;
diameter = 0.0;
mass = 0.0;
}
void Planet::setName(string n) {
name = n;
}
bool Planet::setDiameter(double d) {
bool rv = false;
if (d > 0.0) {
rv = true;
diameter = d;
}
return rv;
}
bool Planet::setMass(double m) {
bool rv = false;
if (m > 0.0) {
rv = true;
mass = m;
}
return rv;
}
string Planet::getName() {
return name;
}
double Planet::getMass() {
return mass;
}
double Planet::getDiameter() {
return diameter;
}
double Planet::CalcSa() {
double sa = 4.0 * M_PI * pow((diameter / 2.0), 2.0);
return sa;
}
double Planet::CalcV() {
double v = (4.0 / 3.0) * M_PI * pow((diameter / 2.0), 3.0);
return v;
}
double Planet::CalcDensity() {
double den = mass / CalcV();
return den;
}
double Planet::CalcG() {
double r = diameter / 2.0;
double grav = (G * mass) / (pow(r, 2.0));
return grav;
}
My issue is with this line of code:
l.erase(l.begin() + n);
void DeleteVector(vector<Planet>& l, int n) {
if (int len = l.size() > 0) {
cout << l[n].getName() << " was removed from the list.\n";
l.erase(l.begin() + n);
}
}
i pass in the vector of planets which is a class. and then i pass in the position "n" which i wish to remove from the vector of objects.
I get the following error:
Error C2280 'Planet &Planet::operator =(const Planet &)': attempting to reference a deleted function
Any help or guidance would be appreciated.
The copy assignment operator for your class Planet is implicitly deleted by your compiler, because it has a const member.
See Deleted implicitly-declared copy assignment operator:
A defaulted copy assignment operator for class T is defined as deleted if any of the following is true:
T has a non-static data member of non-class type (or array thereof) that is const;
You probably meant G to be static const:
static const double G;
and then outside of your class:
const double Planet::G = 6.67408e-11;

Set method in a simple c++ class file returning strange values

I am having trouble using a set function in a class file. So far I have the following. I am trying to write a quadratic class that has three private data members and can calculate both the value of a quadratic and the number of real roots in the quadratic. I'm not stuck on the math part as much as I am getting the set methods to not give me weird values. When I test using main, the values for a, b, and c are numbers that I didn't input when I created the object.
Quadratic.hpp
#ifndef QUADRATIC_HPP
#define QUADRATIC_HPP
class Quadratic
{
private:
double a;
double b;
double c;
public:
Quadratic();
Quadratic(double, double, double);
void setA(double);
void setB(double);
void setC(double);
double getA();
double getB();
double getC();
double valueFor(double);
int numRealRoots();
};
#endif
Quadratic.cpp
#include <cmath>
#include <iostream>
Quadratic::Quadratic()
{
setA(1.0);
setB(1.0);
setC(1.0);
}
Quadratic::Quadratic(double A, double B, double C)
{
a = A;
b = B;
c = C;
}
void Quadratic::setA(double A)
{
a = A;
}
void Quadratic::setB(double B)
{
a = B;
}
void Quadratic::setC(double C)
{
c = C;
}
double Quadratic::getA()
{
return a;
}
double Quadratic::getB()
{
return b;
}
double Quadratic::getC()
{
return c;
}
double Quadratic::valueFor(double x)
{
return (a*(pow(x,2)) + b*x + c);
}
int Quadratic:: numRealRoots()
{
double discriminant = pow(b,2) - (4*a*c);
double epsilon = 0.00001;
int realRoots;
if (discriminant <= epsilon && discriminant > 0)
realRoots = 1;
else if (discriminant > epsilon)
realRoots = 2;
else
realRoots = 0;
return realRoots;
}
Your setB method is wrong - it updates a instead of b:
void Quadratic::setB(double B)
{
b = B; // Was "a = B;" in the original code
}

c++ how to conserve vector items and double-precision

I'm trying to fill a vector of an object Point 3D. My app read a csv file to load the vector by the three cordinate x, y, z. I use the type float.
This is my code.
main.cpp
int main(int argc, char** argv) {
char *theFileName = "file.csv"; //[100];
vector<Point> v = getPointCloud(theFileName);
for (int i = 0; i < v.size(); ++i) {
v.at(i).print(cout);
}
}
getPointCloud
vector<Point> getPointCloud(char *fileName) {
string line;
string token;
vector<Point> v;
double tab[3];
ifstream file(fileName);
if (file.is_open()) {
while (getline(file, line)) {
int cpt = 0;
stringstream stream(line);
while (getline(stream, token, ',')) {
tab[cpt] = ::atof(token.c_str());
cpt++;
}
Point p(tab[0], tab[1], tab[2]);
p.print(cout); <-- the display works
p.setColor(255, 0, 0);
v.push_back(p);
}
file.close();
} else {
cout << "Unable to open " << fileName << '\n';
exit(0);
}
return v;
}
I have two problems:
1 - when I try to display points in the main method, I found that the three coordinates are null ( == 0) but in the displaying in the getPointCloud method works very well.
2 - Can someone give a simple method to conserve my coordinates without loss precision after mathematical operations. I have searched in the net but I don't understand haw to solve it. I'm newbie with c++.
Point.h
#ifndef POINT_H
#define POINT_H
#include <math.h>
#include <iostream>
class Point {
protected:
float x;
float y;
float z;
// color RGB
float r;
float g;
float b;
public:
// Constructors
Point();
// Point(const Point& orig);
Point(std::ostream &strm);
Point(float x, float y, float z);
Point(const Point& orig);
virtual ~Point();
//getters
float getX() const {
return this->x;
}
float getY() const {
return this->y;
}
float getZ() const {
return this->z;
}
float getR() const {
return this->r;
}
float getG() const {
return this->g;
}
float getB() const {
return this->b;
}
//setters
void setX(float x) {
this->x = x;
}
void setY(float y) {
this->y = y;
}
void setZ(float z) {
this->z = z;
}
void setR(float r) {
this->r = r;
}
void setG(float g) {
this->g = g;
}
void setB(float b) {
this->b = b;
}
void setColor(float r, float g, float b) {
this->r = r;
this->g = g;
this->b = b;
}
/**
* Print the point
* #param strm
*/
void print(std::ostream &strm);
//Other methods
float dist2D(Point &other);
float dist3D(Point &other);
Point swap(Point p);
// Point operator-(const Point &other) const;
};
#endif /* POINT_H */
Point.cpp
#include <iostream>
#include <math.h>
#include <ostream>
using namespace std;
#include "Point.h"
Point::Point(const Point& orig) {
}
Point::Point(ostream &strm) {
strm << "Type the abscissa: ", cin >> this->x;
strm << "Type the ordinate: ", cin >> this->y;
strm << "Type the applicate: ", cin >> this->z;
}
Point::Point(float x, float y, float z) : x(x), y(y), z(z) {
// The default point color is blue
this->r = 0;
this->g = 0;
this->b = 255;
}
/**
* Destructor
*/
Point::~Point() {
}
//Other methods
float Point::dist2D(Point &other) {
float xd = x - other.x;
float yd = y - other.y;
return sqrt(xd * xd + yd * yd);
}
float Point::dist3D(Point &other) {
float xd = x - other.x;
float yd = y - other.y;
float zd = z - other.z;
return sqrt(xd * xd + yd * yd + zd * zd);
}
Point Point::swap(Point p) {
Point aux(x, y, z);
x = p.x;
y = p.y;
z = p.z;
return aux;
}
//Point Point::operator-(const Point &other) const {
// return Point(other.getX() - this->x, other.getY() - this->y, other.getZ() - this->z);
//}
void Point::print(ostream &strm) {
strm << "Point(" << this->x << "," << y << "," << z << ")" << endl;
}
Thanks in advance.
Point::Point(const Point& orig) {
}
is incorrect.
It does not copy data from orig to *this
Please copy each of the member in this constructor.
This would look like this:
Point::Point(const Point& orig) {
x = orig.x ;
y = orig.y ;
x = orig.z ;
r = orig.r ;
g = orig.g ;
b = orig.b ;
}

Weird behaviour of overloaded + operator

I have made class which represents Line Linia (aX+bY=c) and I overloaded + operator so now it now returns new Linia object which has c = c + b/argument. But the problem is that when I use this operator all the fields of the given Line object become 0
#include <iostream>
using namespace std;
struct Q{
public:
double x,y;
Q(double x, double y){
this->x = x;
this->y = y;
}
friend ostream& operator<< (ostream &wyjscie, Q const& ex){
wyjscie<<"("<<ex.x<<","<<ex.y<<")";
return wyjscie;
}
};
class Linia{
public:
double a,b,c;
Linia (double a, double b, double c){
this->a = a;
this->b = b;
this->c = c;
}
Linia operator+ (double i){
return Linia(a, b, c + i/b);
}
Linia operator- (double i){
return Linia(a, b, c - i/b);
}
Q operator* (const Linia& i){
double w = a*i.b - b*i.a;
double wx = -c*i.b + i.c*b;
double wy = a*(-i.c) + i.c*c;
double x = wx/w, y = wy/w;
cout<<*this<<endl;
cout<<i<<endl;
return Q(x,y);
}
friend ostream& operator<< (ostream &wyjscie, Linia const& ex){
wyjscie<<ex.a<<"x + "<<ex.b<<"y = "<<ex.c;
return wyjscie;
}
};//podwyzszenie przez ile/B
int main()
{
Linia* pionowa = new Linia(0,1,0);
Linia* l = new Linia(1,1,3);
// Q q = (*l) * (*pionowa);
cout<<"linia przed podniesieniem "<<*l<<endl;
// cout<<"punkt przeciecia przed podniesieniem: "<<q<<endl;
l = l+3;
cout<<"Line highered"<<*l<<endl;
l = l-3;
cout<<"Line Lowered "<<*l<<endl;
// q = (*l) * (*pionowa);
// cout<<"punkt przeciecia po podniesieniu: "<<q<<endl;
cout << "Hello world!" << endl;
return 0;
}
You are doing pointer arithmetic here. That means that l ends up pointing to some address where is shouldn't:
Linia* l = new Linia(1,1,3);
l = l+3; // l is a pointer!!!
If you stop using new and raw pointers everywhere it might just work.
int main()
{
Linia pionowa(0,1,0);
Linia l(1,1,3);
l = l+3;
cout<<"Line highered"<< l <<endl;
l = l-3;
cout<<"Line Lowered "<< l <<endl;
}