0
votes

I have three .cpp files: Game, Guesser, and Provider. Each one of the header files for those contains its respective class declaration.

In my Game class, I need private variables for Guesser and Provider.

Guesser has a non-default constructor

Guesser::Guesser (int wordLength, const char* wordListFilename){}

so I have to initialize it through an initialization list on the Game constructor.

What I have so far:

Guesser& guesser;

Provider& provider;

`Game::Game(int& wordLength, const char* filename, const Provider& providr) : guesser(wordLength, filename),provider(providr)
{
    numMissedGuesses = 0;
    wordSoFar = string(wordLength, FILL_CHARACTER);
}`

This code doesn't compile, and I'm lost on where to go from here.

Error messages from compiler:

||=== Build: Debug in adthangman (compiler: GNU GCC Compiler) ===|

error: 'guesser' declared as reference but not initialized|

error: 'provider' declared as reference but not initialized|

In constructor 'Game::Game(int, const char*, const Provider&)':|

error: expression list treated as compound expression in mem-initializer [-fpermissive]| warning: left operand of comma operator has no effect [-Wunused-value]|

error: invalid initialization of reference of type 'Guesser&' from expression of type 'const char*'|

error: invalid initialization of reference of type 'Provider&' from expression of type 'const Provider'| ||=== Build failed: 5 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

Any help is greatly appreciated.

2
guesser shouldn't be a reference. - 0x499602D2
I can't declare it any other way without getting an error that I haven't met the format of the constructor. - James
Please post an MCVE. - R Sahu
Best guess I have for guesser is that the type is incomplete. The compiler is pretty clear on provider. - chris
Chris, it does mention that the type is incomplete when I do: Guesser guesser; I've just started learning C++, so I have no idea what to do. I've spent two days on this to no avail. - James

2 Answers

0
votes

You can't use a constructor on an object that is a reference. It in itself is not the object, but is points to another object. So either pass the reference guesser variable another object of class type guesser or change it to a regular variable. Also you can't assign a const object to a non constant reference. Make the provider parameter non constant.

0
votes

guesser and provider need to be declared inside the class Game { } scope if they are to be initialized by the constructor.

You can't initialize a reference to a class object by providing arguments to the constructor of its class, though. References must be initialized by a preexisting object. (Non-member references can also be initialized by temporary objects, but that's a bit different.)

Also, reference members are often a red flag for class design. You might want to use regular member objects for guesser and provider instead of references.

class Game {
    Guesser guesser;
    Provider provider;
};