### Not sure where I went wrong

This is what I am trying to accomplish...You need to implement a recursive function namely Apowerfun. Apowerfun is a
power function which receives the value of a real number āaā raised to the
power of a non negative integer ānā.
Write a header file which that you will call Apower.h. The file contains a
recursive method Apowerfun that has two parameters:
1- First parameter is a of type float
2- Second parameter is n of type long
and returns the an
value as a float.
Write a C++ file which you will call Apower.cpp that reads in a long non
negative number n, and a float number a, and prints the corresponding a
number. And my code is below with the error(s)

 ``123456789101112131415161718192021222324252627282930313233`` `````` #ifndef APOWER_H // Preprocessor #define APOWER_H #include #include using namespace std; class Apower { public: float apowerfun(float a, long n) { if (a==0) { return (0); } if (n==0) { return (1); } else if (n > 0) { return (a * apowerfun(a,n-1)); } } }; #endif ``````

And the .cpp file

 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include using namespace std; #include "APower.h" //implementation of the employee class void main() { Apower ap1(float a, long n); float a; long n; cout << "Enter base as an integer: "; cin >> a; cout << "Enter exponent as an integer: "; cin >> n; Apower* apowerfun = new Apower(ap1(a,n)); cout << "\nResult: " << apowerfun << endl; cin.get(); cin.get(); };``````

And here is the error(s):
1>Apower.obj : error LNK2019: unresolved external symbol "class Apower __cdecl ap1(float,long)" (?ap1@@YA?AVApower@@MJ@Z) referenced in function _main
1>C:\Users\Friedmann\Desktop\Data Structures\Assignment 7\Apower\Debug\Apower.exe : fatal error LNK1120: 1 unresolved externals

I know part of the issue is I need to create a new object to link it to the header file but the issue is I am not exactly sure how to do it correctly. I have tried some different things but none of them have worked. Any help would be awesome, thanks
Last edited on
On line 10 of your .cpp file, you're calling a constructor which takes two arguments, even though no such constructor exists.
Also, what exactly do you think this should do anyways?

 ``1234`` `````` Apower ap1(float a, long n); float a; long n;``````

Also, `void main()`...
Additionally, you're not deleting the for the `new` on line 23, and you also don't have a copy constructor for your Apower class.

Nitpicky things below this line:
--------------------------------------

`using namespace std;` in global scope.
using double cin.get to pause the program is the poor man's way of doing it. Use std::ignore instead.
Why do you have lines 4 - 7 in your header, if it doesn't use them?
The comment on line 6 of your .cpp could be misleading, since the actual class implementations usually go into a .cpp file by the same name (also... "employee"?)...
Last edited on
What I am trying to figure out is that I need a constructor in the header file right? That is where I am confused, the instructions say I have to use a header file. I am just confused about how and where the constructor should go in the header file that way the 2 files are linked. Also thanks for the tips on nitpicky things that will help me clean up the project.
Hi again,

Anyways, at no point in the instructions you provided does it say to write a class, it only says to write a function.
I'm going to assume this is some sort of homework assignment, in which case, I would be able to help you further if you posted the entire instructions your instructor gave you (if there's more than in your first post), because the wording of the instructions you posted is somewhat confusing.

Here's how I interpret your instructions:

Create three files:

1.) "Apower.h"
2.) "Apower.cpp"
3.) "main.cpp"

"Apower.h" should have a function prototype/declaration for the function "Apowerfun", which takes a float ("a") and a long ("n") argument, and should return a float.

"Apower.cpp" should have the implementation for the function "Apowerfun". The function should print the value of "a" and return a^n.

"main.cpp" should contain your main function, and all the necessary code to exercise the previously mentioned function.
Last edited on
There should only be 2 files: "Apower.h" and "Apower.cpp".

An outline of the program is:

 ``123456789101112131415161718192021222324252627282930`` `````` 1- APower.h 2- APower.cpp Here is an outline of the file APower.h: // recursive computation of the apowerfun(): #include #include using namespace std; class Apower{ public: float apowerfun(float a, long n) { if //the if part is the base case statement else //else part is recursive step return ( ); } } the outline of the APower.cpp: #include #include #include "Apower.h" // header file using namespace std; void main() { /* *declare an object ap1 of Apower created in the header file above; */ //connect ap1 to apowerfun(a,n) //and print its value; };``````
Topic archived. No new replies allowed.