Creating a tally counter using Classes

May 17, 2013 at 9:36pm
So my instructions are to implement a class that models a tally counter. Whenever the operator pushes a button, the counter value advances by one. Model this operation with a count member function. Use a get_value member function instead. I'm getting an error at line 17 which I don't understand and I'm also getting "statement cannot resolve address of overloaded function" for line 23 and 24. Any help would be really appreciated :) Thanks in advance.

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

class TallyCounter {
  private:
      int people; 
  public:
      TallyCounter (); 
      void Clear ( int people = 0);
      void AddPerson ()
      {
          people ++; 
      }
      void GetValue () const; 
      {
        return people;  
      }     
  };

int main () {
    TallyCounter Counter1;
    Counter1.Clear();
    Counter1.AddPerson;
    Counter1.GetValue; 
  return 0;
}
May 17, 2013 at 9:47pm
Line 14, you have an extraneous ; at the end of the line.
Line 16, you're trying to return an int, but the function is declared void.
Line 23 & 24, you're trying to call functions, but you're missing the ()

Two other items:
line 24 doesn't do anything. It gets the value of people, ad the result is discarded.
Line 8, you're constructor should initialize people to 0. That's what constructors are for.


May 17, 2013 at 10:34pm
Thanks, I fixed my code up a bit but now I'm getting an error "undefined reference to `TallyCounter::TallyCounter()"

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
class TallyCounter {
  private:
      int people; 
  public:
      TallyCounter (); 
     void Clear  ()
     {
         people = 0;
         cout << people;
     }

     void AddPerson ()
      {
          people++; 
      }
      void GetValue () const
      {
        cout << people;  
      }
          
  };

int main () {
    TallyCounter Counter1;
    Counter1.Clear();
    Counter1.AddPerson();
    Counter1.GetValue(); 
  return 0;
}
May 17, 2013 at 11:20pm
That's because you have no implementation for TallyCounter().
Line 5 is only a declaration.

May 17, 2013 at 11:34pm
try
 
TallyCounter() : people( 0 ) {}


Otherwise it seems pointless to use a class if you are not even going to use a constructor...
Last edited on May 17, 2013 at 11:34pm
May 17, 2013 at 11:58pm
That's perfect. Thanks so much :)
Topic archived. No new replies allowed.