How to get rid of function already has a body errors?

I'm getting this error in my 3 set functions. Tried commenting things out but can't figure out what is wrong. here's my main file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "Polynomial.h"
#include <iostream>
#include <cmath>

using namespace std;
double tmp;
Polynomial::Polynomial() {
	x = 0;
	a = 0;
	b = 0;
	c = 0;
}

Polynomial::Polynomial (int a1, int b1, int c1) {
	a = a1;
	b = b1;
	c = c1;
}
void Polynomial::setA(double set_a){
	set_a = tmp;
}

void Polynomial::setB(double set_b){

	set_b = tmp;
}

void Polynomial::setC(double set_c){

	set_c = tmp;
}


double Polynomial::evaluate(double x){
	double ans = a * pow(x, 2) * b * x * c;
	return ans;
}
bool findRoots(double &r1, double &r2, double a, double b, double c) {
	double d;  
	d = b*b - 4 * a * c;

	if (d < 0) {
		return false;
	}

	r1 = (((-b) + sqrt(d)) / (2.0 * a));
	r2 = (((-b) - sqrt(d)) / (2.0 * a)); 
	return true;
}

void Polynomial::print(){
	cout << a << "x^2 + " << b << "x + " << c << " = 0\n";
}
and here's the header in case that's where the problem is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

class Polynomial {
public:

	double x, p1, p2;
	int a1, b1, c1;
	Polynomial();
	Polynomial(int a1, int b1, int c1);
	~Polynomial();
	double evaluate(double x);
	bool findRoots(double x, double &r1, double &r2);
	double getA() const { return a;  }
	void setA(double new_a) { a = new_a; }
	double getB() const { return b; }
	void setB(double new_b) { b = new_b; }
	double getC() const { return c; }
	void setC(double new_c) { c = new_c; }
	void print();

private:

	double a, b, c;

};

#endif POLYNOMIAL_H  
Don't you already have another thread about this assignment?
fg109: it was a different problem. it seems like whatever I try i just get more errors
actually I just figured it out. hmm how do I delete this
I haven't read through the other thread so I don't know why you have so many variables in your class, especially

1
2
3
int a1, b1, c1;
//...
double a, b, c;


Are your set functions supposed to change a1, b1, c1 or a, b, c? At any rate, your current set functions are like this:

1
2
3
void Polynomial::setA(double set_a){
	set_a = tmp;
}

^The problem here is that set_a is a local variable, a copy of whatever you passed in as a parameter. This set_a is not part of your class, so changing this has no meaning whatsoever. And assigning tmp to it is even more meaningless, since it seems to have nothing to do with the function whatsoever.

The correct function (assuming you're trying to change a) is

1
2
3
void Polynomial::setA(double set_a){
	a = set_a;
}


EDIT: I need to remember to refresh threads before submitting posts...
Last edited on
thank you!! i realized that because you see my header? I put the function's bodies over there. And I also put the function's bodies in the main
Yeah, I see now. Good luck with your assignment. :)
thanks again! sorry one last thing maybe you could figure out? I'm getting an error: too few arguments in function call and findRoots function does not take two arguments. the error is here it's the last one im getting

1
2
3
4
5
6
7
8
if (p.findRoots(r1, r2)) {

		cout << "\twith roots at " << r1 << " and " << r2 << endl;
	}
	else {

		cout << "\tNo Roots" << endl;
	}

and i tried fixing it by adding a, b, c to the parameters but then I get more errors than before
Last edited on
According to your header file, findRoots is supposed to take three parameters.

bool findRoots(double x, double &r1, double &r2);

You've only provided two. You should use it like this:

p.findRoots(?, r1, r2)

Without seeing more of your code, I have no idea what x is supposed to be.
Topic archived. No new replies allowed.