Undefined reference to error - Quadratic formula program

Hello, I'm working on creating a program that solves the quadratic formula when given coefficients a, b, and c.
I've just been receieving the errors:
undefined reference to 'equSolver()'
undefined reference to 'outResults()'

The thing that has been confusing me is that findCoeff() runs perfectly fine if the others are commented out.

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
54
55
56
57
58
#include <iostream>
#include <cmath>

using namespace std;
void findCoeff();
void equSolver();
double discr();
void outResults();

void findCoeff(){
		double a;
		double b;
		double c;
		
		cout << "\nEnter coefficient a:\n";
		cin >> a;
		cout << "\nEnter coefficient b:\n";
		cin >> b;
		cout << "\nEnter coefficient c:\n";
		cin >> c;
}

double discr(double a, double b, double c){
	
	double d;
	d = pow(b, 2) - (4 * a * c);
	return d;
}

void equSolver(double a, double b, double c, double d){
	double root1 = ((-1 * b) + sqrt(d)) / (2 * a);
	double root2 = ((-1 * b) - sqrt(d)) / (2 * a);
	
}

void outResults(double root1, double root2, double a, double b, double c, double d){
	if( std::isnan(root1) || std::isnan(root2)){
		cout << "Quadratic equation with the following coefficients: \n";
		cout << "a: " << a << "; b: " << b << "; c: " << c << "\n";
		cout << "has no roots in the real domain\n";
	}
	else {
		cout << "Quadratic equation with the following coefficients: \n";
		cout << "a: " << a << "; b:" << b << "; c:" << "\n";
		cout << "has the following roots\n";
		cout << "Root1: " << root1 << "; Root2: " << root2 << "\n";
		
	}
}

	


int main(){
findCoeff();
equSolver();
outResults();
}


Thanks!
Last edited on
the function header and proto must match.

you can't say

void foo();

void foo(int bar)
{
stuff;
}

you should instead say

void foo(int bar);

void foo(int bar)
{
stuff;
}


You don't *need* the headers so long as they appear before they are used from top to bottom. with main at the bottom, you don't need the headers unless the functions call each other and are out of order. Its good to have them, I am just telling you a side-note.

that is, this is legal:

void newfunc(); /// this isn't necessary here. But its nice to have.

void newfunc(int x)
{
cout << "newfunc rox";
}

int main()
{
newfunc(3);
}

other ramblings.. you can use <complex> type in c++ for the roots that are not real. If you want them.


Last edited on
That was what I had originally had but when I have the parameters matching I get around 10 of these errors:
main.cpp:56:11: error: 'a' was not declared in this scope

I've currently got all of the variables declared in the function declarations, main, and in the actual functions so im not sure what is wrong?

Are my variables only declared locally?

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
54
55
56
57
58
#include <iostream>
#include <cmath>

using namespace std;
void findCoeff();
void equSolver(double a, double b, double c, double d);
double discr(double a, double b, double c);
void outResults(double root1, double root2, double a, double b, double c, double d);

void findCoeff(){
		double a;
		double b;
		double c;
		
		cout << "\nEnter coefficient a:\n";
		cin >> a;
		cout << "\nEnter coefficient b:\n";
		cin >> b;
		cout << "\nEnter coefficient c:\n";
		cin >> c;
}

double discr(double a, double b, double c){
	
	double d;
	d = pow(b, 2) - (4 * a * c);
	return d;
}

void equSolver(double a, double b, double c, double d){
	double root1 = ((-1 * b) + sqrt(d)) / (2 * a);
	double root2 = ((-1 * b) - sqrt(d)) / (2 * a);
	
}

void outResults(double root1, double root2, double a, double b, double c, double d){
	if( std::isnan(root1) || std::isnan(root2)){
		cout << "Quadratic equation with the following coefficients: \n";
		cout << "a: " << a << "; b: " << b << "; c: " << c << "\n";
		cout << "has no roots in the real domain\n";
	}
	else {
		cout << "Quadratic equation with the following coefficients: \n";
		cout << "a: " << a << "; b:" << b << "; c:" << "\n";
		cout << "has the following roots\n";
		cout << "Root1: " << root1 << "; Root2: " << root2 << "\n";
		
	}
}

	


int main(){
findCoeff();
equSolver(a, b, c, d);
outResults(root1, root2, a, b, c, d);
}
Last edited on
yes, that is not quite right.

int main(){
findCoeff();
equSolver(a, b, c, d); <----------- the compiler can't find a,b,c,or d anywhere.

void findCoeff(){
double a; <-------- local to this function only.
double b;
double c;


you can define them inside main, and pass them to each function in turn.
you can define them in one function, and have that function pass them into the other functions.
you can make them global, at the top of the file outside of main, but everyone hates that because it causes many, many problems with larger projects.


I would just do it in main and pass every function what it needs from there, you are really close to that approach.

You also must not have this issue:

void foo()
int a; <-------- this a isnt the main's a!!!!!!

int main()
int a;

you want to have

void foo (int a) <-------- this IS main's a!
...
int main()
foo(a); <---- pass it !


As mentioned above, there are no variables named a,b or c declared in main(), which is why you're getting the aforementioned errors. I would make findCoeff() take 3 doubles by reference, which allows the original to be modified, like so:
1
2
3
4
5
6
7
8
9
void findCoeff(double& a, double& b, double& c)
{
        cout << "\nEnter coefficient a:\n";
        cin >> a;
        cout << "\nEnter coefficient b:\n";
        cin >> b;
        cout << "\nEnter coefficient c:\n";
        cin >> c;
}


Then in main() you use it like this:
1
2
3
4
5
6
int main()
{
    double a = 0, b = 0, c = 0;
    findCoeff(a, b, c);
    
}
Topic archived. No new replies allowed.