Class objects in main

Hi,

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.
Last edited on
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.

1
2
3
4
5
6
7
8
9
/class::class(vector<double> _A, vector<double> _B, double _C, double _D, double _E)
{
    A=_A;
   B=_B;
    C=_C;
    D=_D;
    E=_E;

}
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?
Can you show us the code in Main?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/using namespace 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;
}

closed account (zb0S216C)
class cannot be used as an identifier since it's a reserved word.

Wazzak
Thank you I have changed that, however it is still causing the same problem.
You need something like this:

1
2
3
4
5
6
vector<double> _A, _B;
double _C, _D, _E;

// Initialize these variables

MyModel myModel(_A, _B, _C, _D, _E);


Then members of myModel can be accessed.

I also highly recommend changing your variable names to mean something. You can also keep each one the same:

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
34
35
#include <iostream>
using namespace std;

class Refrigerator 
{
private:
  int amountOfEggs;

public:
  // Default constructor
  Refrigerator()
    :amountOfEggs(0)
    {
    };
  // Custom constructor
  Refrigerator(int amountOfEggs)
    :amountOfEggs(amountOfEggs)
    {
    };

   //Accessor
   int getAmountOfEggs(){ return amountOfEggs;}

};


int main(){
  int amountOfEggs = 12;
  Refrigerator yourFridge, myFridge(amountOfEggs);

  cout << "You have: " << yourFridge.getAmountOfEggs() << " eggs.\n";
  cout << "I have: " << myFridge.getAmountOfEggs() << " eggs.";
  cin.get();
  return 0;
} 


Last edited on
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:
	virtual double 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);
Last edited on
Topic archived. No new replies allowed.