I need to make an object for a class in main in a piece of code that I am compiling. However it won't let me create this object, similar to how I've done it with other classes. It's coming up with:
error: no matching function for call to 'class::class()'
note: candidates are: class::class(std::vector<double, std::allocator<double> >, std::vector<double, std::allocator<double> >, double, double, double)
class::class(const CorrBinModel&)
Can anybody suggest any reasons why this might be? This is a really frustrating problem and has been holding me back for a while so any help would be much appreciated. Will provide code if required. Thanks in advance.
You are trying to create the object using the default constructor (constructor with that takes no arguments). The class doesn't have a default constructor so it fails to compile. If you want the class to have a default constructor you have to create one, otherwise you have to use one of the constructors that do exist.
Thanks very much, there is a constructor in the class already that I'm not using for anything. Should I just leave it blank? This is how I have it now although I didn't really know how to use it.
This seems to be the only thing holding back my code. I have tried deleting the constructor and changing it to use the default constructor, however I've not had any luck so far. Any ideas would be much appreciated?
/usingnamespace std;
int main()
{
double E, F;
int G;
class MyModel;
if (MyModel.G()==1) return 1;
M MyOption1;
P(MyModel,MyOption1,E,F,G) << endl;
return 0;
}
Ok slightly different question as I think I have solved the problem I was having initially. It concerns the object of a virtual function. I am getting this error message. I have seen a similar piece of code which I have adapted to suit my purposes. However I am receiving this error message which wasn't received before and I cannot see what the difference is. Would anyone be able to tell me why I might get this type of error?
error: cannot declare variable 'C' to be of abstract type 'M'|
note: because the following virtual functions are pure within 'M':|
note: virtual double Rectangle::P(double)
You have code structured something along these lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Rectangle
{
public:
virtualdouble P(double x) = 0;
};
class M : public Rectangle
{
};
int main()
{
M c;
return 0;
}
The function Rectangle::P() has no definition (called "pure virtual", designated by "= 0"), so it is an abstract class. Abstract classes cannot be instantiated. You must declare a subclass and define the pure virtual functions (P in this case) in order to instantiate a derived object with a base class of Rectangle.
To fix your problem, declare and define: double M::P(double);