Help with OOP

I am wondering how to get my class to work with the main that I have right here.
The error that I am getting is that there is no matching function call for the class Averager. Here is the simple code that I am testing out right now
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>

class Averager
{
    private:
    double include;
    
    
    public:
    Averager(double i)
    {
       include = i; 
    
    }
    
    
};


using namespace std;

int main()
{

      Averager * avg= new Averager;
      avg->include(10.25);
      avg->include(9.75);
      avg->include(4.9);// display the calculated averagecout << avg->calc() << endl;// 8.3delete avg;return 0;}

        cout << avg->calc() << endl;// 8.3delete avg;return 0;}

        delete avg;

return 0;
}
Line 25, you are trying to use a default ctor without parms you haven't defined. Since you defined a ctor that takes a double parameter you need to define one that doesn't.

Lines 26-28, you are trying to access a private data member. Can't do that. Either declare include as public OR declare an accessor to add a value.

line 30, where's your calc() member function definition?

If'n you are wanting to get the average of a number of inputs you need to keep track of the number of inputs you provide.

Rough, quick and dirty:
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
#include <iostream>

class Averager
{
private:
   double include { };
   int    num     { };

public:
   Averager()                      { }
   Averager(double i) : include(i) { }

   void AddInclude(double val) { include += val; num++; }

   double GetAverage()         { return (include / num); }
};

int main()
{
   Averager* avg = new Averager;

   avg->AddInclude(10.25);
   avg->AddInclude(9.75);
   avg->AddInclude(4.9);

   std::cout << avg->GetAverage() << '\n';

   delete avg;
}

8.3

Creating a single class instance on the heap is a bit of overkill IMO. YMMV.
With a bit of reflection I'd recommend one change to the class:

Either delete the ctor that takes a parameter (recommended)

OR

modify the ctor:

Averager(double i) : include(i), num(1) { }

Now the ctor will accept a base-line number for later averaging and set the initial number of entries to one.
To expand on what others have said, when you talk about:

function call for the class Averager


What that really is is the default constructor. It's the constructor that's automatically called when you create an object but don't specify an argument. You do this on line 25.

If you don't define any constructors for your class, then the compiler will automatically create an empty default constructor for you. But you have defined a constructor (lines 10 - 14), so the compiler doesn't create a default constructor - and, therefore, can't find the one you're attempting to invoke at line 25.

Hope that's clear now?
Topic archived. No new replies allowed.