0

I am trying to write a program that computes the area of a right triangle. It has to use a function to compute the area. This is actually a part of a bigger program, but when I couldn't get it to work I isolated this section. I tried looking on google for help, but nothing I found seemed to help. I get no errors when I compile, but when I try to execute I get the following error:

Linking...
area.obj : error LNK2001: unresolved external symbol "double __cdecl findarea(double,double)" (?findarea@@YANNN@Z)
Debug/area.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

area.exe - 2 error(s), 0 warning(s)

Below is my code:

//Program to compute the area of a right triangle
#include <iostream>
#include <iomanip>
#include <fstream>



//Declare std
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::cerr;
using std::ofstream;

//Function Prototype
double findarea (double,double);

//Start the program
int main ()
{

//Declare the variables
	double a; //side a
	double b; //side b
	double d; //area


//Open the file for output
	ofstream datafile ("program.txt", ios::out);

//Error routine if file can not be created
	if (!datafile) {
		cerr << "File could not be opened" << endl;
		exit (1);
	}

//Initialize a 
	a=1;

//Describe program to user
	cout << "This program will compute the area of a right triangle.\n\nSet side a or b to 0 or less to end the program.";
	datafile << "This program will compute the area of a right triangle.\n\nSet side a or b to 0 or less to end the program.";

//Begin the WHILE loop to set sentinal value to loop the program
	while (a>0){


//Get a 
		cout << "\n\nEnter the length of side a: " << endl;
		cin >> a;
		datafile << "\n\nSide a is: " << a << endl;

//Use an IF to dictate the behavior of the program if a>0 and get b
		if (a>0){			
			cout << "\nEnter the length of side b: " << endl;
			cin >> b;
			datafile <<"Side b is: " << b << endl;

//Allow for b<=0
			if (b<=0){
				cout << "\n\nYou have ended the program." << endl;
				datafile << "You have ended the program." << endl;
				break;
			}			

			d=findarea(a,b);

			cout << "\nThe area is: " << d << endl;
			datafile << "The area is: " << d << endl;

//End IF
		}

//Start ELSE to allow output that tells the user they have ended the program
		else {
			cout << "\n\nYou have ended the program." << endl;
			datafile << "You have ended the program." << endl;

//End ELSE 
		}


//End WHILE
	}

//End program
		return 0;

}



double findarea (double q,double r,double s) {
	s=(q*r)/2;
	return s;
}

Any pointers/help would be appreciated.

TIA
Heather

2
Contributors
4
Replies
6
Views
12 Years
Discussion Span
Last Post by HConn
0

Compare and contrast:

>double findarea (double,double);
>d=findarea(a,b);
>double findarea (double q,double r,double s) {

Be sure to take careful note that the declaration takes two arguments and the definition takes three. C++ treats these as two unique functions that are overloaded to have the same name, therefore the linker is complaining about your not defining the findarea that takes two arguments.

0

Thanks so much :cheesy: I had a feeling it was something simple I was overlooking. It was driving me bonkers

0

>I had a feeling it was something simple I was overlooking.
That's not really something simple, so don't feel bad. :) Function overloading problems tend to be incredibly subtle and difficult to find, even for experienced C++ programmers.

0

Part of my confusion came from the fact I had done a different program set up the same way... prototyped-(double, double) invoked-(a,b) then defined-(double x, double y, double z) for finding the hypotenuse of a right triangle...and it compiled and linked and worked fine for whatever fluke reason. Then we had to make it also do the area and I set it up like I had the hypotenuse function...and the hypotenuse still worked but then the area part errored out. So I pulled the area part of it and made a program just for that....it still errored on the linking...I couldn't figure out why, so that is why I came on here yelling for help. My teacher even said "I can tell you why the area function did NOT work, but I can't explain why the hypotenuse one DID work." Just a fluke thing that for some reason I got away with. So now I know...lol

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.