Hangman build errors

The "Word.length" in my code is showing red underlines saying "Error: a pointer to a bound function may only be used to call function." I googled but no luck, anyone have any ideas? I tried building it and got this error.

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
1
1>Build started 5/25/2012 2:03:12 PM.
1>PrepareForBuild:
1>  Creating directory "C:\Users\Enveus\Desktop\Projects\GSP115\Dark GDK Demo 2-D Game\hangman\Debug\".
1>InitializeBuildStatus:
1>  Creating "Debug\hangman.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  hangman.cpp
1>c:\users\enveus\desktop\projects\gsp115\dark gdk demo 2-d game\hangman\hangman\hangman.cpp(41): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\enveus\desktop\projects\gsp115\dark gdk demo 2-d game\hangman\hangman\hangman.cpp(62): error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::length': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::length' to create a pointer to member
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>c:\users\enveus\desktop\projects\gsp115\dark gdk demo 2-d game\hangman\hangman\hangman.cpp(62): error C2446: '<' : no conversion from 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Ax>::* )(void) const' to 'int'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          There is no context in which this conversion is possible
1>c:\users\enveus\desktop\projects\gsp115\dark gdk demo 2-d game\hangman\hangman\hangman.cpp(62): error C2297: '<' : illegal, right operand has type 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Ax>::* )(void) const'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>c:\users\enveus\desktop\projects\gsp115\dark gdk demo 2-d game\hangman\hangman\hangman.cpp(110): error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::length': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::length' to create a pointer to member
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>c:\users\enveus\desktop\projects\gsp115\dark gdk demo 2-d game\hangman\hangman\hangman.cpp(110): error C2446: '<' : no conversion from 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Ax>::* )(void) const' to 'int'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          There is no context in which this conversion is possible
1>c:\users\enveus\desktop\projects\gsp115\dark gdk demo 2-d game\hangman\hangman\hangman.cpp(110): error C2297: '<' : illegal, right operand has type 'unsigned int (__thiscall std::basic_string<_Elem,_Traits,_Ax>::* )(void) const'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.83
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


This is my code

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;



string Generate_Word();
void Print_Letters();
void Print_Man();
void Player_Win();
void Player_Lose();

int running = 1;
string Word = "";
char Known_Letters[];
char Guess;

int main()
{
	Word = Generate_Word(); // Picks random word from list
	int lives = 7;

	while (running)
	{
		Print_Man();
		Print_Letters();
		if (lives <= 0) Player_Lose();
		if (Known_Letters == Word) Player_Win();
	}
    return 0;
}




string Generate_Word()              //Function to generate from word list
{
	srand(time(NULL));			    //Using system time as seed
	int Word_ID = rand() % 11; 
	string List[10] =			    //Contains the 10 word in arrays
	{
		"ketchup",
		"controller",
		"alcohol",
		"paperwork",
		"muscle",
		"bandaid",
		"math",
		"harddrive",
		"powder",
		"sibling"
	};
	string Word = List[Word_ID];;   //Generates from 0 - 11 containing the words
	return Word;					//Returns the word in function
}

void Print_Letters()
{
	for (int pos = 0; pos < Word.length; pos++)
	{
		if (Known_Letters[pos] == Word[pos])
			cout << Word[pos];
		else
			cout << "x";
	}
	cout << "\n";
	return;
}

void Print_Man()
{
	int lives = 7; // 7 stages, 7 lifes

	switch(lives) 
	{
    case 7: 
        cout << "\n\n\n\n\n";
        break;
    case 6:
        cout << "\n\n\n\n______"; 
        break;
    case 5:
        cout << "\n    |\n    |\n    |\n    |\n____|_";
        break;
    case 4:
        cout << " ___\n | \\|\n    |\n    |\n    |\n____|_";
        break;
    case 3:
        cout << " ___\n | \\|\n O  |\n    |\n    |\n____|_";
        break;
    case 2:
        cout << " ___\n | \\|\n O  |\n |  |\n    |\n____|_";
        break;
    case 1:
        cout << " ___\n | \\|\n O  |\n/|\\ |\n    |\n____|_";
        break;
    case 0:
        cout << " ___\n | \\|\n O  |\n/|\\ |\n/ \\ |\n____|_";
        break;
	}
	cout << endl;
		cout << "Guess a letter: ";
		cin >> Guess;

		bool correct = 0;
		
		for (int pos = 0; pos < Word.length; pos++)
		{
			if (Guess == Word[pos])
			{
				Known_Letters[pos] = Guess;
				correct = 1;
			}
			if (correct == 0) lives--;
			correct = 0;
		}
}

void Player_Win()
{
	cout << "Congratulations, you have guessed the word correctly!";
	cout << "Press 'ENTER' to quit\n";
	cin.get();
	running = 0;
	return;
}

void Player_Lose()
{
	cout << "You lose! The word was: " << Word << endl;
	cout << "Press 'ENTER' to quit\n";
	cin.get();
	running = 0;
	return;
}
You need to use Word.length() with the brackets, since it's a function call.
length() is a member function, so when you use it, as in pos < Word.length(), you need the parentheses.
Common mistake, it's str.length() :D

It's a function, not a data member

EDIT:
And atropos beat me to it :(
So did BlackSheep
Last edited on
Thanks! didn't know that lol

But now that I have no more syntax errors, I got this build error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

1
1>Build started 5/25/2012 2:47:57 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\hangman.unsuccessfulbuild".
1>ClCompile:
1>  hangman.cpp
1>c:\users\enveus\desktop\projects\gsp115\dark gdk demo 2-d game\hangman\hangman\hangman.cpp(41): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\enveus\desktop\projects\gsp115\dark gdk demo 2-d game\hangman\hangman\hangman.cpp(62): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\enveus\desktop\projects\gsp115\dark gdk demo 2-d game\hangman\hangman\hangman.cpp(110): warning C4018: '<' : signed/unsigned mismatch
1>hangman.obj : error LNK2001: unresolved external symbol "char * Known_Letters" (?Known_Letters@@3PADA)
1>C:\Users\Enveus\Desktop\Projects\GSP115\Dark GDK Demo 2-D Game\hangman\Debug\hangman.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.30
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Any ideas? Also isn't there a way to find out which line of code is having errors etc?
That's happened to me a few times before; I found that closing the project and re-opening it fixes the linker problem.

Also, there's a (potential) problem on line 42. Your string array for the choice of words has 10 elements ([0-9]), but Word_ID takes a random value [0-10]. So, there's a 9.99...% chance that the element accessed in List[10] will throw an out-of-range exception.
Thanks for the reply atropos,

I tried restarting the project multiple times with this still

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
1>Build started 5/25/2012 3:48:00 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\hangman.unsuccessfulbuild".
1>ClCompile:
1>  All outputs are up-to-date.
1>ManifestResourceCompile:
1>  All outputs are up-to-date.
1>hangman.obj : error LNK2001: unresolved external symbol "char * Known_Letters" (?Known_Letters@@3PADA)
1>C:\Users\Enveus\Desktop\Projects\GSP115\Dark GDK Demo 2-D Game\hangman\Debug\hangman.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.88
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The declaration of Known_Letters is just a forward definition. As the [] has no number in it, no storage has been allocated for it.

Change

char Known_Letters[];

to

char Known_Letters[26];

or whatever size you need.
Thanks andy, I just ran my program, works really well, but one problem.

It keeps saying that I have 7 lives, so the hangman never comes out. So I deleted the lives = 7 in the main, how do I use the if statement from the main to use 'lives' from the "Print_Man" function instead of using the main's 'lives' variable?

Because of right now, I have infinite lives
On line 24, you define an int variable lives in main() and initialize it to 7.
Then, in Print_Man() you define an int variable lives and initialize it to 7.
Because the Print_Man() variable has the same name as the main() variable, the compiler "hides" the main() lives.

At line 117 the Print_Man() lives variable is reduced by 1.

When the program exits the Print_Man() function, all local variables are destroyed. ( Print_Man() lives goes 'poof' )

When the while loop (line 26) returns to Print_Man(), Print_Man() lives comes into existence again, with the initial value of 7.
And the cycle repeats.

So, knowing this, which one of the two int lives = 7; should you remove, and which one should stay?
Last edited on
Yeah I was thinking the same logic, but without the int lives = 7 in main, the main's

if (lives <= 0) Player_Lose();

goes unidentified, how do I go about doing so?

I am trying to get the lives from the Print_Man() function, or should either way work?
If you move the definition of int lives to the global scope (declare it outside of any function) it can be used by every function in the file.

An alternative would be to pass it by reference to Print_Man(), but I don't know if you've learned about references yet.
Well! I got past that part now, but it seems that now I only have 2 tries before I lose, ._.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;


int lives = 7; // 7 stages, 7 lifes
string Generate_Word();
void Print_Letters();
void Print_Man();
void Player_Win();
void Player_Lose();

int running = 1;
string Word = "";
char Known_Letters[26];
char Guess;

int main()
{
	Word = Generate_Word(); // Picks random word from list


	while (running)
	{
		Print_Man();
		Print_Letters();
		if (lives <= 0) Player_Lose();
		if (Known_Letters == Word) Player_Win();
	}
    return 0;
}




string Generate_Word()              //Function to generate from word list
{
	srand(time(NULL));			    //Using system time as seed
	int Word_ID = rand() % 11; 
	string List[10] =			    //Contains the 10 word in arrays
	{
		"ketchup",
		"controller",
		"alcohol",
		"paperwork",
		"muscle",
		"bandaid",
		"math",
		"harddrive",
		"powder",
		"sibling"
	};
	string Word = List[Word_ID];;   //Generates from 0 - 11 containing the words
	return Word;					//Returns the word in function
}

void Print_Letters()
{
	for (int pos = 0; pos < Word.length(); pos++)
	{
		if (Known_Letters[pos] == Word[pos])
			cout << Word[pos];
		else
			cout << "x";
	}
	cout << "\n";
	return;
}

void Print_Man()
{

	switch(lives) 
	{
    case 7: 
        cout << "\n\n\n\n\n";
        break;
    case 6:
        cout << "\n\n\n\n______"; 
        break;
    case 5:
        cout << "\n    |\n    |\n    |\n    |\n____|_";
        break;
    case 4:
        cout << " ___\n | \\|\n    |\n    |\n    |\n____|_";
        break;
    case 3:
        cout << " ___\n | \\|\n O  |\n    |\n    |\n____|_";
        break;
    case 2:
        cout << " ___\n | \\|\n O  |\n |  |\n    |\n____|_";
        break;
    case 1:
        cout << " ___\n | \\|\n O  |\n/|\\ |\n    |\n____|_";
        break;
    case 0:
        cout << " ___\n | \\|\n O  |\n/|\\ |\n/ \\ |\n____|_";
        break;
	}
	cout << endl;
		cout << "Guess a letter: ";
		cin >> Guess;

		bool correct = 0;
		
		for (int pos = 0; pos < Word.length(); pos++)
		{
			if (Guess == Word[pos])
			{
				Known_Letters[pos] = Guess;
				correct = 1;
			}
			if (correct == 0) lives--;
			correct = 0;
		}
}

void Player_Win()
{
	cout << "Congratulations, you have guessed the word correctly!";
	cout << "Press 'ENTER' to quit\n";
	cin.get();
	running = 0;
	return;
}

void Player_Lose()
{
	cout << "You lose! The word was: " << Word << endl;
	cout << "Press 'ENTER' to quit\n";
	cin.get();
	running = 0;
	return;
}


Also what is reference? Our professor told us about it but never got to it, maybe it can fix it ; /
This is the result

1
2
3
4
5
6
7
8
9
10
11






Guess a letter: d
xxxxxxx
You lose! The word was: alcohol
Press 'ENTER' to quit
Press any key to continue . . .
The problem is here (line 109-118)

1
2
3
4
5
6
7
8
9
10
for (int pos = 0; pos < Word.length(); pos++)
		{
			if (Guess == Word[pos])
			{
				Known_Letters[pos] = Guess;
				correct = 1;
			}
			if (correct == 0) lives--;
			correct = 0;
		}


Look at where in the loop if (correct == 0) lives--; is.
Also, what is effect of the line correct = 0; ?
Last edited on
if (correct == 0) lives--;

Then
 
correct = 0; 


So it loops until I run out of lives.

But I for the love of god can't figure another way to do it lol.
Your correct == 0 test should be outside the for loop, since you don't want to subtract a life every time their guess doesn't match the letter currently being tested, do you? Just test after the loop to see if they didn't get any matches.
Also, correct is set to 0 every time the function is called again, so there is no need to reset it at the end.
Furthermore, you should use true and false when dealing with bools. It's good practice and much more readable.
Finally, in your generate_word function, you have a size 10 array of words, but you generate a number from 0-10 using rand%11. List[10] does not exist, since arrays in C++ start at index 0. Use rand%10.
Topic archived. No new replies allowed.