No Suitable Constructor Exists

Nov 21, 2014 at 9:06pm
I am getting this error message and so far it's the only error in my program which is a game. I am hoping to get some insight as to what to do as this is quite frustrating and I wanna wrap this program up.

no suitable constructor exists to convert from "Stringword [19]" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>"

I declared this at the beginning of the program which it is my understanding should take care of what's above, however, I still get this error message:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//create type to make an array of arrays to place strings in arrays
typedef char Stringword[7];//each word is an array of up to 7 chars
Stringword WORD[19];//string of 20-1 words

//These are my function declarations:

void readFile();
//postconditions:
//read a word from the file into the string WORD.
int Play_Game(string WORD);
//postconditions: 
//keeps track of and returns: misses, when guess is incorrect, repeated guess of same character, and when guess is correct
//display stars and expose letters to replace stars as correct letters are guessed.

//The issue is being highlighted in this function call for WORD:

Play_Game(WORD);//run game 
Nov 21, 2014 at 9:17pm
line 10: Do you mean Stringword instead of std::string? std::string has no constructor that takes a Stringword array.
Last edited on Nov 21, 2014 at 9:17pm
Nov 22, 2014 at 1:14am
I made Stringword up. It's a type I made up to make an array of chars to put in an array of words. I hope that makes sense...I got the idea from this link http://www.eastcoastgames.com/cpp/chapter4.html. however the way I implemented my gameplay is quite different. Yes in the function definition I did change string WORD to Stringword WORD at one point and that introduced a bunch of other problems...so I know I am missing a pretty important concept somewhere or the program would at least compile.
Nov 22, 2014 at 3:18pm
It's better if you keep your posts in your thread rather than IM.
It can help others and others can help you.

You're trying to use Stringword and std::string interchangably and they are not interchangeable. Stringword is a 2D array of chars, while std::string is a single instance of a C++ string.

Just get rid Stringword and use std::string. Stringword provides no benefit to your code.

After changing Stringword to std:;string, the problem I see is that you're confused as to whether WORD is an array or a simple string.

Note: Following line numbers refer to code posted via IM.

Line 18,57: Play_Game is declared and implemented to take one string by value. At line 30, you call Play_Game attempting to pass an array. This is not permitted. I'm assuming you intended to pass Play_Game a single word.

It's generally poor style to use the same variable name as a global array (WORD) and and a local argument (WORD) where it's a single class instance. It confuses the reader (me).

Line 48: Your loop assumes your file will never have more than 19 words in it.
Have you learned vectors yet? A vector for words would eliminate having a hard limit on the number of words in your file and avoids confusion with how WORD is used within Play_Game(). This also eliminates the restriction on how long words can be since your typedef limited them 6 characters (+1 for trailing null).

Line 50: You're treating WORD here as an array (which the global is). Would be better to name this words. The plural indicating it is a collection or array.

Line 63: Serves no purpose. Is hidden by subsequent declaration of SOLUTION at line 66.

I personally dislike variable names that are all caps. The usual convention is that all caps are used for defines or constants, but this is purely a matter of style.

Edit: code removed at OP's request.
Last edited on Nov 23, 2014 at 10:16pm
Nov 23, 2014 at 12:50am
Thanks for looking that over!, ill work on it this weekend .
Topic archived. No new replies allowed.