Hello, I am using multiple files and getting LNK errors.
here is the error list:
1>driver.obj : error LNK2019: unresolved external symbol "void __cdecl goodbye(void)" (?goodbye@@YAXXZ) referenced in function _main
1>driver.obj : error LNK2019: unresolved external symbol "double __cdecl eToTheN(double &)" (?eToTheN@@YANAAN@Z) referenced in function _main
1>functions.obj : error LNK2001: unresolved external symbol "double __cdecl eToTheN(double &)" (?eToTheN@@YANAAN@Z)
1>driver.obj : error LNK2019: unresolved external symbol "double __cdecl nthRoot(double &,int &)" (?nthRoot@@YANAANAAH@Z) referenced in function _main
//Header.h
#ifndef NUM_H
#define NUM_H
#include <iostream>
usingnamespace std;
//Description: Outputs a greeting
//Pre: None
//Post: User hello'd
void greeting();
//Description: Outputs the menu and returns the value entered by the user
//Pre: None
//Post: returns the user's input (even if invalid)
char outputMenu();
//Description: Keeps asking for user input until the number input is positive
//Pre: T is a numeric type
//Post: If doOutput = 1, output prompt. Returns a positive number specified by user
template <typename T>
T positiveInput(bool doOutput = 1)
{
T input;
do
{
if(doOutput)
cout << "Enter a number: ";
cin >> input;
if(input <= 0)
cout << "Must be positive" << endl;
}while(input <= 0);
return input;
}
//Description: Takes a number to an integer power
//Pre: Base is positive
//Post: Returns base to the power of pow
double exponent(double base, int pow);
//Description: Calculate the nth root of a number
//Pre: Num is positive, num and original are same number on user's original call
//Post: returns the nth root of num
double nthRoot(double& num, int& n);
//Description: Calculates e to the power n
//Pre: n is an integer
//Post: returns e to the power n
double eToTheN(double& n);
//Description: Outputs a goodbye message
//Pre: None
//Post: User goodbye'd
void goodbye();
//Description: Calculates the factorial of a number
//ex: n! = n(n-1)(n-2)(n-3)...(1)
//Pre: n is positive
//Post: returns the factorial of the argument
int factorial(int n);
//Description: Calculates the hyperbolic sin of a number
//Pre: none
//Post: returns the hyperbolic sine of the arguement
double sinh(double num);
#endif
//func.cpp
#include <iostream>
usingnamespace std;
#include "header.h"
void greeting()
{
cout << "Hello Willie! I can tell your math skills are lacking so I will help you" << endl;
}
char outputMenu()
{
char input;
cout << "1) Enter a number" << endl
<< "2) Take the number to the power of another number" << endl
<< "3) Display the nth root of a number" << endl
<< "4) Calculate e ^ n" << endl
<< "5) Display the hyperbolic sine (sinh) of a number" << endl
<< "6) Quit";
cin >> input;
return input;
}
double exponent(double base, int power)
{
double result = base;
for(int i = 0; i < power -1; i++)
result *= base;
return result;
}
double nthRoot(double original, int m, constint n = 10)
{
int num = original;
for(int i = 1; i < n; i++)
num += ((m - 1)*(num) + original/exponent(num, m - 1))/m;
return num;
}
double eToTheN(int num, constint& n = 10)
{
double result = 0;
for(int i = 0; i < n; i++)
result += exponent(num, i)/factorial(i);
return result;
}
int factorial(int num)
{
if (num != 0)
return num * factorial(num - 1);
return 1;
}
double sinh(double num)
{
return (eToTheN(num) - eToTheN(num * -1))/2;
}
//main.cpp
//#includes
#include <iostream>
#include "header.h"
usingnamespace std;
//Body
int main()
{
//Declarations
bool entered = 0;
constchar enter = '1', power = '2', root = '3', ePower = '4', hypsin = '5', quit = '6';
int powerInput;
char userInput;
double num;
greeting();
do
{
do
{
userInput = outputMenu();
if(userInput < enter || userInput > quit)
cout << "Must be between" << enter <<" and" << quit << endl;
}while(userInput < enter || userInput > quit);
if(userInput != enter && userInput != quit && !entered)
cout << "Must enter" << enter << " first" << endl;
elseswitch(userInput)
{
case enter://Enter a number
num = positiveInput<double>();
break;
case power://Raise a num to a power
cout << "Enter a number n to calculate " << num <<" ^ n:";
powerInput = positiveInput<int>(0);
cout << num << " ^ " << powerInput << " = " << exponent(num, powerInput) << endl;
break;
case root://Take the nth root of a num
cout << "Enter a number n to calculate " << num << " ^ 1/n:";
powerInput = positiveInput<int>(0);
cout << num << " ^ 1/" << powerInput << " = " << nthRoot(num, powerInput) << endl;
break;
case ePower: //Take e ^ num
cout << "e ^ " << num << " = " << eToTheN(num);
case hypsin://Take the hyperbolic sine of num
cout << "Sinh(" << num << ") = " << sinh(num);
break;
}
}while(userInput != quit);
goodbye();
}
The linker cannot find an object (compiled file) containing the following function definitions matching the parameters you're trying to use the functions with:
goodbye, eToTheN, nthRoot.
Looking through the code above, you've not defined goodbye at all, your definition of eToTheN accepts two variables but you're trying to call it with one variable only, and nthRoot accepts three variables but you're trying to use it with two.