In C++, creating an array with new[] initializes all the objects with their default/no-parameter constructor.
So this line: (semicolon added)
Genes *genes=new Genes[10];
Will result in ten calls to Genes::Genes().
That would normally seem fine, since C++ will give you a default constructor when you don't declare any. However, for this to happen, you must not declare any constructors. Your constructor:
Genes::Genes(double a, double b, double c)
Prevents the compiler from creating a default constructor for you, which in turn prevents you from making an array of Genes objects.
There are two reasonable solutions to this problem:
You could add a default/no argument constructor to the Genes class. This is simple, but lacks some elegance. What is a default Genes object? If such an object made sense, you probably would have declared a default constructor already.
Look into using std::vector instead of an array: http://www.cplusplus.com/reference/stl/vector/ . While this is a more complicated fix in the short term, being familiar with the Standard Template Library (which supplies the vector class) will be valuable in the long term. That said, if you are just learning C++ and haven't seen templates before, this might be a bit overwhelming and you might want to read a bit about templates first. (for example, at http://www.learncpp.com/cpp-tutorial/143-template-classes/ )
The vector class allows you to declare a capacity, for how many objects you will put into your array (or you may not declare a capacity, resulting in a slower insert). Then, it will only construct objects when they are placed into the vector. Your code would look something like this:
#include <vector> // to get the vector class definition
using std::vector; // to
vector<Genes> genes;
genes.reserve(geneno); // optional, but speeds things up a bit
for(i = 0; i <= geneno; i++) {
double d = random();
double e = random();
double f = random();
genes.push_back(Genes(d, e, f));
}
The last statement is (roughly) equivalent to:
Genes temp(d, e, f);
genes.push_back(temp);
vector::push_back adds an item to the back of the vector and increases the vector capacity by 1: http://www.cplusplus.com/reference/stl/vector/push_back/
You can subsequently access elements in the vector the same way as you would the array:
cout << "The third gene's coding is " << genes[3].coding << endl;
And you can query the size of the vector with vector::size():
cout << "The vector has " << genes.size() << "elements" << endl;
std::vectorrather than playing around with dynamic allocation directly.Genes::Genes(double a, double b, double c) : cis(a), coding(b), effect(c) { }. C++ Lesson #18: If you use assignment in a constructor, you're probably Doing It Wrong.Generather thanGenes.delete, you're probably Doing It Wrong :-)