expression cannot be used as a function

Hello,

I am exploring Mersenne Twister implementation to use it as a wrapped class that can be reused as a dll for other implementations like C#. I was trying this class and cannot figure out what is wrong. The code listed below and also available @ cpp.sh/4hte. I will appreciate any guidance help with fixing this class.

It gives me error
In member function 'double Random::GenerateNext()': 19:19: error: expression cannot be used as a function 20:2: warning: control reaches end of non-void function [-Wreturn-type]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <random> 
#include <iostream>
#include <iomanip>
#include <string>
#include <map>


class Random {
    std::mt19937* gen;
    std::uniform_real_distribution<>* distr;
  public:
	Random (int seed, double min, double max)
	{
		gen = new std::mt19937(seed);
		distr = new std::uniform_real_distribution<>(min,max);
	};
    double GenerateNext() 
	{
		return distr(gen);
	};
};

int main()
{
    double a = 1.0;
    double b = 2147483647.0;
	int seed = 999999999;
    Random x(seed, a, b);
	std::cout << std::fixed << std::setprecision(10) << x.GenerateNext() << std::endl;
	std::cout << std::fixed << std::setprecision(10) << x.GenerateNext() << std::endl;
	std::cout << std::fixed << std::setprecision(10) << x.GenerateNext() << std::endl;
}
Well, you have ponters, not objects, remember. You need to dereference pointers first.

Why do you use pointer semantic at all?
1
2
3
4
5
6
7
8
9
10
11
class Random 
{
    std::mt19937 gen;
    std::uniform_real_distribution<> distr;
  public:
    Random (int seed, double min, double max) : gen(seed), distr(min, max) {}
    double GenerateNext() 
    {
        return distr(gen);
    }
};
Last edited on
Topic archived. No new replies allowed.