Function problems...

Started by
7 comments, last by Splinter of Chaos 15 years, 3 months ago
This tutroial is showing me how to change the contents of an array with a function. Here is the code and error message. #include <iostream> using namespace std; void cube(int *n, int num); int main() { int i, nums[10]; for(i=0; i<10; i++) nums = i+1; cout << "original contents: "; for(i=0; i<10; i++) cout << nums << ' '; cout << "\n"; cube(nums, 10); cout << "Altered contents :"; for(i=0; i<10; i++) cout << nums << ' '; return 0; } Funcs.obj : error LNK2019: unresolved external symbol "void __cdecl cube(int *,int)" (?cube@@YAXPAHH@Z) referenced in function _main i'm not sure why it's not compiling, but is it the fact that the pointer '*n' isnt used anywhere in main()?
Advertisement
The problem is that you don't provide a function definition for your cube() function.
When you declared - > void cube(int *n, int num);

You then called - > cube(nums, 10);

The problem is your program has no idea what your cube function does.

You need to make this under your main function:

void cube(int *n, int num)
{
// DO STUFF
}
Quote:Original post by MrCpaw

You need to make this under your main function:

void cube(int *n, int num)
{
// DO STUFF
}



That was my first thought. This book tutorial is missing that part of code, lol. Let me track down the source code they have at the publisher's site and see if its any different.
Maybe it might make a nice exercise to try implement the function without looking at the sample code. [smile]
Quote:Original post by XandX2005
This book tutorial is missing that part of code, lol.

Maybe it's your job to write the function? LOL.

Here's a C approach:
void cube(int *n, int num){    int x;    while (num--) x = *n, *n++ = x * x * x;}

And here's a C++ approach:
#include <algorithm>int cube_function(int x){    return x * x * x;}void cube(int *n, int num){    std::transform(n, n + num, n, cube_function);}

With boost, you can emulate functional programming and get rid of the helper function:
#include <algorithm>#include <boost/lambda/lambda.hpp>using namespace boost::lambda;void cube(int *n, int num){    std::transform(n, n + num, n, _1 * _1 * _1);}


Of course real functional programming would be even terser, for example in Haskell:
cube n = map (\x -> x^3) n

And with the power of partial function application and currying, you can just write:
cube = map (^3)
Or if you want a real basic cube function in C++:

int cube(int x){  return x*x*x;}


That works. Just a side-note, copying from sites and what not will not get you anywhere. Take time and try to write your own code, try and write the function by yourself. You'll be amazed how much better you feel :P
Jacob Foster
Software Engineering student at the Oregon Institute of Technology
Quote:Original post by dalindeck
Just a side-note, copying from sites and what not will not get you anywhere. Take time and try to write your own code, try and write the function by yourself. You'll be amazed how much better you feel :P


I have been trying to write my own programs. I just wanted a working example so that i could get some ideas, lol.
I agree with the suggestion to use the C++ <algorithm> solution, however, I thing YOU should write it so you understand it better. That way, when you use the standard implementation, you know why it works and how to help it.

Define cube as int cube(int x) { return x*x*x; } and then write your transform function.

If you don't know about templates yet, this might not be easy, and debugging errors in how your using the standard functions will be doubly hard. So, I don't suggest using the standard implementation until you know about templates AND can write your own, only using the standard for simplicity, readability, and convention.

Until then, I support not using the standard and doing this code. It would be too soon to abstract you from the lower level stuff and you wouldn't understand it.

However, I feel your cube function that takes an array is just unintuitive. Most people won't expect a cube function to take more than one parameter, the number to cube. How about calling it cubeAll or something?

This topic is closed to new replies.

Advertisement