By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

How to let a function return an array

P: n/a
#include <iostream>
#include <cmath>
using namespace std;

// implements a pseudo RNG generator by the linear congruential
generator
const int n1=100;
double *LRNG(int, int, int);
int main()
{
double *random;
int a,c,n ;
cout << "Please enter the seed for a and the constant for c";
cin >> a >> c;
cout << "How many random numbers do you want to generate?";
cin >> n;
random = LRNG(a, c, n);
for (int i=1; i<n1;i++)
{
cout << random[i] << endl;
}
// How do u return a array from a function?
return 0;
}

double *LRNG(int a, int c, int seed, int n) {
double M;
//const int n1= 100;
double m0[n1], r[n1];
m0[1]= 1;
// How do u assign a variable size to an array??
M = pow(2,32);
for (int i=1;i<n1;i++)
{
r[i] = fmod(a * m0[i]+c,M);
}
return r;
}
Compile with error: warning C4172: returning address of local variable
or temporary
But on linking:
Linking...
LinearRNG.obj : error LNK2001: unresolved external symbol "double *
__cdecl LRNG(int,int,int)" (?LRNG@@YAPANHHH@Z)
Debug/LinearRNG.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

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

I am lost as to what to do next. thanks..

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
er******@hotmail.com wrote:
[...]
Compile with error: warning C4172: returning address of local variable
or temporary
But on linking:
Linking...
LinearRNG.obj : error LNK2001: unresolved external symbol "double *
__cdecl LRNG(int,int,int)" (?LRNG@@YAPANHHH@Z)
Debug/LinearRNG.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.


You should get rid of both the warning and the error. First, do not
return a local variable. Better pass the appropriately sized array
into the function so it can fill it with required values. Second,
compare the declaration (prototype) of the 'LRNG' function and the
definition. Should become obvious why symbol is unresolved.

V
Jul 23 '05 #2

P: n/a
er******@hotmail.com wrote:
#include <iostream>
#include <cmath>
using namespace std;

// implements a pseudo RNG generator by the linear congruential
generator
const int n1=100;
double *LRNG(int, int, int);
double *LRNG(int, int, int); <- takes 3 ints
int main()
{
double *random;
int a,c,n ;
cout << "Please enter the seed for a and the constant for c";
cin >> a >> c;
cout << "How many random numbers do you want to generate?";
cin >> n;
random = LRNG(a, c, n);
for (int i=1; i<n1;i++)
{
cout << random[i] << endl;
}
// How do u return a array from a function?
return 0;
}

double *LRNG(int a, int c, int seed, int n) {
double *LRNG(int a, int c, int seed, int n) <- takes 4 ints

The linker is saying it can't find LRNG with 3 ints ! This is because
you have not provided one.
double M;
//const int n1= 100;
double m0[n1], r[n1];
r[n1]; <- returning this means it will be destructed before
the user can use it.

try
std::vector<double> r(n1);

returning this will make a copy, if you don't want to make a copy, pass
it as a parameter.
m0[1]= 1;
// How do u assign a variable size to an array??
M = pow(2,32);
for (int i=1;i<n1;i++)
{
r[i] = fmod(a * m0[i]+c,M);
}
return r;
}

Compile with error: warning C4172: returning address of local variable
or temporary
But on linking:
Linking...
LinearRNG.obj : error LNK2001: unresolved external symbol "double *
__cdecl LRNG(int,int,int)" (?LRNG@@YAPANHHH@Z)
Debug/LinearRNG.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

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

I am lost as to what to do next. thanks..

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.