1

I am studying random number generation in C++ and I had implemented it from the following site:

https://www.quantstart.com/articles/Random-Number-Generation-via-Linear-Congruential-Generators-in-C/

When running this program, I am getting the following error:

main.cpp:12:31: error: cannot declare variable ‘lcg’ to be of abstract type ‘LinearCongruentialGenerator’
   12 |   LinearCongruentialGenerator lcg(num_draws, init_seed);
      |                               ^~~
In file included from main.cpp:2:
lin_con_gen.h:6:7: note:   because the following virtual functions are pure within ‘LinearCongruentialGenerator’:
    6 | class LinearCongruentialGenerator : public RandomNumberGenerator
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from lin_con_gen.h:4,
                 from main.cpp:2:
random.h:30:18: note:   ‘virtual void RandomNumberGenerator::get_uniform_draws(std::vector&)’
   30 |     virtual void get_uniform_draws(std::vector<double>& draws) = 0;
      |                  ^~~~~~~~~~~~~~~~~
lin_con_gen.cpp:51:6: error: no declaration matches ‘void LinearCongruentialGenerator::get_unifrom_draws(std::vector&)’
   51 | void LinearCongruentialGenerator::get_unifrom_draws(std::vector<double>&draws)
      |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
lin_con_gen.cpp:51:6: note: no functions named ‘void LinearCongruentialGenerator::get_unifrom_draws(std::vector&)’
In file included from lin_con_gen.cpp:4:
lin_con_gen.h:6:7: note: ‘class LinearCongruentialGenerator’ defined here
    6 | class LinearCongruentialGenerator : public RandomNumberGenerator
      |

Please kindly help me in running it correctly.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    The error message means you have an abstract class (_i.e._ a class that has one or more virtual functions defined to have no implementation -- see the definition of `RandomNumberGenerator::get_uniform_draws`) and you are attempting to create an instance of that class. As the compiler helpfully informs you, along with file names and line numbers of related information, you can't do this. It looks like `LinearCongruentialGenerator` forgot to implement this function. – paddy Dec 13 '21 at 04:49
  • In other words is your code exactly the same as the example and does LinearCongruentialGenerator implement virtual void get_uniform_draws(std::vector draws); ? or is the =0 still there? – Pepijn Kramer Dec 13 '21 at 04:53
  • Dear Pepijn Kramer, -- Yes, after studying the code I had implemented it from the site. That's why I am curious that why isn't it working. – Daanial Ahmad Dec 13 '21 at 05:27
  • Dear Paddy- Then how shall we correct this? – Daanial Ahmad Dec 13 '21 at 05:30
  • 1
    Read the error message from `lin_con_gen.cpp` line 51 that says "no declaration matches" and "no functions named get_unifrom_draws". Do you see the typo? This is why the `override` keyword exists for virtual functions, so you don't get messed up by spelling something wrong. In this case if you declared the typo name as override it would have immediately been an error. – paddy Dec 13 '21 at 08:11

0 Answers0