Class initialization problem??

I tried writing a simple program where you have to guess a number between 1 and 20 using Classes. The code is as follows.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <stdlib.h>
using namespace std;

//---------------------------------Interface----------------------------------//
class guessNumber
{
      public:
             guessNumber(int guess)
             {
                                 Number = generate();
                                 Guess  = guess;
                                 
             }
             bool Comparator();
             bool isBigger();
      private:
              int generate();
              int Number;
              int Guess;
};

//-------------------------------Implementation-------------------------------//

int guessNumber::generate()
{
    srand ( time(NULL) );
    return (rand()%20 + 1);
}

bool guessNumber::Comparator()
{
     if (Number==Guess) return true;
     else return false;
}

bool guessNumber::isBigger()
{
     if (Number>Guess)return true;
     else return false;
}

//----------------------------------Main--------------------------------------//

int main()
{
    int g;
    guessNumber GN; //ERROR LINE 48
    cout<<"I'm thinking of a number between 1 and 20.\nYou have three guesses to find out the number."<<endl;
    for (int guess=1;guess=3;guess++)
    {
        cout<<"Guess "<<guess<<": ";
        cin>>g;
        GN(g); //ERROR LINE 54
        if (GN.Comparator())
        {
                            cout<<"Yup! That's the number alright. Way to go!"<<endl;
                            cin.get();
                            return 0;
        }
        else if(GN.isBigger())
        {
                              cout<<"Nope. Think of a larger number."<<endl;
                              continue;
        }
        else
        {
                              cout<<"Nope. Think of a smaller number."<<endl;
                              continue;
        }
    }
    cout<<"HAHA! you couldn't guess my Number!"<<endl;
    system ("pause");
    return 0;  
}


If you're wondering, the interface implementation headers are there cuz i find reading codes with classes in them a little confusing).

Anyway, I got the following error in line 48 and 54

no matching function for call to 'guessNumber::guessNumber()'


how do you fix that?
Second, how can this code be more efficient?
GN isn't a function. It's a class object. Calling GN(g) is a function call to operator()(some param) which doesn't exist.

Also, you only need one tab per indention, not a million spaces.
Last edited on
Delete lines 9-14 and

1. create a constructor guessNumber() that just calls Number = generate();
2. create a new method void guess( int myGuess ) { }
3. edit your main() appropriately

You fell into a trap because you have not given your class a good enough name (you tried using that name both as a constructor and as a function name).

Don't worry about efficiency yet - get your code right first.
Last edited on
Thansk alot - I got the code working fine.
One thing, though. Is it possible to get the number generator to generate a single random number and stick with it. Right now, it generates a different number for each try.
Yes, modify line 27 - I will not tell you how (type "man srand" on your command line) - I am sure you can figure it out - gl
Thanks - I'll have a go :)
Thanks guys, it works perfect now.

I changed the srand( time(NULL) ) to srand ( 5).
Topic archived. No new replies allowed.