Jump to content
  • Advertisement
Sign in to follow this  
XandX2005

Function problems...

This topic is 3577 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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()?

Share this post


Link to post
Share on other sites
Advertisement
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
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Maybe it might make a nice exercise to try implement the function without looking at the sample code. [smile]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!