warning: statement is a reference, not call, to function

J

Jag

When I compile the program below I get this error. I don't understand
what is happening.
Please help. It works in VC++
---------------------------------------------------------------------
gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)
test.cpp: In function `int main()':
test.cpp:28: error: expected `;' before "r"
test.cpp:28: warning: statement is a reference, not call, to function
`srandom'
test.cpp:29: error: `r' was not declared in this scope
--------------------------------------------------

#include <iostream>
#include <fstream>
#include <assert.h>
#include <time.h>
#include <iomanip>


class srandom {
public:
srandom() {srand((unsigned)time(0)); }
int get_random_number(int a = 0, int b = 10000) const {
int upper_bound, lower_bound;
if(a < b) {upper_bound = b - a; lower_bound = a;}
else if(a >= b) {upper_bound = a - b; lower_bound = b;}

return(lower_bound + rand() % upper_bound);
}

private:
/* no body can copy srandom or equal srandom */
srandom(const srandom& x);
srandom& operator=(const srandom& x);
};

main() {

srandom r;
int real = r.get_random_number() ;
}
 
I

Ian Collins

When I compile the program below I get this error. I don't understand
what is happening.
Please help. It works in VC++
---------------------------------------------------------------------
gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)
test.cpp: In function `int main()':
test.cpp:28: error: expected `;' before "r"
test.cpp:28: warning: statement is a reference, not call, to function
`srandom'
test.cpp:29: error: `r' was not declared in this scope
--------------------------------------------------

#include<iostream>
#include<fstream>
#include<assert.h>
#include<time.h>
#include<iomanip>


class srandom {

Under Unix/Linux, srandom is a function declared in <stdlib.h>! You
haven't directly included this header (which you should for srand) but
it's probably dragged in by one of the others.

Including <cstdlib> first should fix this, but I don't think gcc keeps
its C library declarations in the std namespace.

Capitalising the first letter of class names is a good way to avoid such
conflicts.
 
J

James Kanze

On 08/ 1/10 03:04 PM, Jag wrote:

[...]
Under Unix/Linux, srandom is a function declared in
<stdlib.h>! You haven't directly included this header (which
you should for srand) but it's probably dragged in by one of
the others.
Including <cstdlib> first should fix this, but I don't think
gcc keeps its C library declarations in the std namespace.

There are, or should be, definitions which one can set to choose
between Posix compatibility and strict C++. (Regretfully, I
forget exactly what they are, and don't have a Unix box handy to
verify.)
Capitalising the first letter of class names is a good way to
avoid such conflicts.

It solves the problem for classes, but not for functions or
variables. (IIRC, Posix allows implementations the same
liberties as C. In other words, srandom may in fact be a macro.
Which can lead to some very incomprehensible error messages.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top