fork download
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <random>
  5.  
  6. // Set population size
  7.  
  8. const int population_size = 10;
  9. const int number_of_variables = 4;
  10.  
  11.  
  12. class cChromo
  13. {
  14.  
  15. public:
  16. std::vector<int> myVariables(number_of_variables );
  17.  
  18. void Rand()
  19. {
  20. std::random_device rd;
  21. std::mt19937 rng(rd()); // random-number engine (Mersenne-Twister in this case)
  22. std::uniform_int_distribution<int> uni(-10, 10);
  23. for (int i = 0; i < number_of_variables; ++i)
  24. {
  25. myVariables.push_back( uni(rng) );
  26. }
  27. }
  28. void Show()
  29. {
  30. std::cout << "Chromo: ";
  31. for( auto v : myVariables )
  32. std::cout << " ";
  33. std::cout << "\n";
  34. }
  35.  
  36. };
  37.  
  38. int main()
  39. {
  40. // Generate random number
  41.  
  42. std::random_device rd;
  43. std::mt19937 rng(rd()); // random-number engine (Mersenne-Twister in this case)
  44. std::uniform_int_distribution<int> uni(-10, 10);
  45.  
  46. // Set gene values.
  47.  
  48. std::vector< std::vector<int>>chromosome;
  49.  
  50.  
  51. for( int kp = 0; kp < population_size; kp++ )
  52. {
  53. std::vector<int>variables;
  54. for (int i = 0; i < number_of_variables; ++i)
  55. {
  56. double rand_num = uni(rng);
  57. variables.push_back (rand_num);
  58.  
  59. }
  60. chromosome.push_back( variables );
  61. }
  62.  
  63. for( auto c : chromosome )
  64. {
  65. for( auto v : c )
  66. {
  67. std::cout << v << " ";
  68. }
  69. std::cout << "\n";
  70. }
  71.  
  72.  
  73.  
  74. std::cout << "OOD\n";
  75.  
  76. std::vector< cChromo > Population( population_size );
  77.  
  78. for( auto c : Population )
  79. {
  80. c.Rand();
  81. }
  82.  
  83. for( auto c : Population )
  84. {
  85. c.Show();
  86. }
  87.  
  88.  
  89. return 0;
  90. }
Compilation error #stdin compilation error #stdout 0s 3460KB
stdin
Standard input is empty
compilation info
prog.cpp:16:32: error: 'number_of_variables' is not a type
   std::vector<int> myVariables(number_of_variables );
                                ^
prog.cpp: In member function 'void cChromo::Rand()':
prog.cpp:25:5: error: '((cChromo*)this)->cChromo::myVariables' does not have class type
     myVariables.push_back(  uni(rng) );
     ^
prog.cpp: In member function 'void cChromo::Show()':
prog.cpp:31:18: error: invalid initialization of reference of type 'std::vector<int> (cChromo::*&&)(int)' from expression of type '<unresolved overloaded function type>'
    for( auto v : myVariables )
                  ^
prog.cpp:31:18: error: no matching function for call to 'begin(std::vector<int> (cChromo::*&)(int))'
In file included from /usr/include/c++/5/string:51:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:2:
/usr/include/c++/5/bits/range_access.h:87:5: note: candidate: template<class _Tp, unsigned int _Nm> constexpr _Tp* std::begin(_Tp (&)[_Nm])
     begin(_Tp (&__arr)[_Nm])
     ^
/usr/include/c++/5/bits/range_access.h:87:5: note:   template argument deduction/substitution failed:
prog.cpp:31:18: note:   mismatched types '_Tp [_Nm]' and 'std::vector<int> (cChromo::*)(int)'
    for( auto v : myVariables )
                  ^
In file included from /usr/include/c++/5/string:51:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:2:
/usr/include/c++/5/bits/range_access.h:58:5: note: candidate: template<class _Container> decltype (__cont.begin()) std::begin(const _Container&)
     begin(const _Container& __cont) -> decltype(__cont.begin())
     ^
/usr/include/c++/5/bits/range_access.h:58:5: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.begin()) std::begin(const _Container&) [with _Container = std::vector<int> (cChromo::*)(int)]':
prog.cpp:31:18:   required from here
/usr/include/c++/5/bits/range_access.h:58:5: error: request for member 'begin' in '__cont', which is of non-class type 'std::vector<int> (cChromo::* const)(int)'
/usr/include/c++/5/bits/range_access.h:48:5: note: candidate: template<class _Container> decltype (__cont.begin()) std::begin(_Container&)
     begin(_Container& __cont) -> decltype(__cont.begin())
     ^
/usr/include/c++/5/bits/range_access.h:48:5: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.begin()) std::begin(_Container&) [with _Container = std::vector<int> (cChromo::*)(int)]':
prog.cpp:31:18:   required from here
/usr/include/c++/5/bits/range_access.h:48:5: error: request for member 'begin' in '__cont', which is of non-class type 'std::vector<int> (cChromo::*)(int)'
In file included from /usr/include/c++/5/bits/range_access.h:36:0,
                 from /usr/include/c++/5/string:51,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:2:
/usr/include/c++/5/initializer_list:89:5: note: candidate: template<class _Tp> constexpr const _Tp* std::begin(std::initializer_list<_Tp>)
     begin(initializer_list<_Tp> __ils) noexcept
     ^
/usr/include/c++/5/initializer_list:89:5: note:   template argument deduction/substitution failed:
prog.cpp:31:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'std::vector<int> (cChromo::*)(int)'
    for( auto v : myVariables )
                  ^
prog.cpp:31:18: error: no matching function for call to 'end(std::vector<int> (cChromo::*&)(int))'
In file included from /usr/include/c++/5/string:51:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:2:
/usr/include/c++/5/bits/range_access.h:97:5: note: candidate: template<class _Tp, unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])
     end(_Tp (&__arr)[_Nm])
     ^
/usr/include/c++/5/bits/range_access.h:97:5: note:   template argument deduction/substitution failed:
prog.cpp:31:18: note:   mismatched types '_Tp [_Nm]' and 'std::vector<int> (cChromo::*)(int)'
    for( auto v : myVariables )
                  ^
In file included from /usr/include/c++/5/string:51:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:2:
/usr/include/c++/5/bits/range_access.h:78:5: note: candidate: template<class _Container> decltype (__cont.end()) std::end(const _Container&)
     end(const _Container& __cont) -> decltype(__cont.end())
     ^
/usr/include/c++/5/bits/range_access.h:78:5: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.end()) std::end(const _Container&) [with _Container = std::vector<int> (cChromo::*)(int)]':
prog.cpp:31:18:   required from here
/usr/include/c++/5/bits/range_access.h:78:5: error: request for member 'end' in '__cont', which is of non-class type 'std::vector<int> (cChromo::* const)(int)'
/usr/include/c++/5/bits/range_access.h:68:5: note: candidate: template<class _Container> decltype (__cont.end()) std::end(_Container&)
     end(_Container& __cont) -> decltype(__cont.end())
     ^
/usr/include/c++/5/bits/range_access.h:68:5: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/range_access.h: In substitution of 'template<class _Container> decltype (__cont.end()) std::end(_Container&) [with _Container = std::vector<int> (cChromo::*)(int)]':
prog.cpp:31:18:   required from here
/usr/include/c++/5/bits/range_access.h:68:5: error: request for member 'end' in '__cont', which is of non-class type 'std::vector<int> (cChromo::*)(int)'
In file included from /usr/include/c++/5/bits/range_access.h:36:0,
                 from /usr/include/c++/5/string:51,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from prog.cpp:2:
/usr/include/c++/5/initializer_list:99:5: note: candidate: template<class _Tp> constexpr const _Tp* std::end(std::initializer_list<_Tp>)
     end(initializer_list<_Tp> __ils) noexcept
     ^
/usr/include/c++/5/initializer_list:99:5: note:   template argument deduction/substitution failed:
prog.cpp:31:18: note:   mismatched types 'std::initializer_list<_Tp>' and 'std::vector<int> (cChromo::*)(int)'
    for( auto v : myVariables )
                  ^
stdout
Standard output is empty