Compiler/IDE misunderstands my functions

I'm coding in Dev C++ IDE. And here are some (parts) of my code:

1
2
3
#include <string>
#include <vector>
using namespace std;


1
2
3
4
5
6
7
8
9
10
11
12
struct Thing {
	string Name;
	vector<Question> _Question;
	
	Thing(string name) : Name(name) {
		_Question.push_back(Question("Is it a " + name + "? ", 'Y'));
	}
	
	void AddQuestion(string quest, char ans) {
		_Question.push_back(Question(quest, ans));
	}
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Guesser {
	vector<Thing> Memory;
	Thing ThinkinOf;
	
	void Ask(string);
	char GetAnswer();
	void GiveUp();
	
public:
	Guesser();
	void Guess();
	
	
} computer;


1
2
3
4
5
6
Guesser::Guesser() { //at this line: [error] no matching function for call to 'Thing::Thing()'
	Thing thing(string("rock"));
	thing._Question.push_back(Question("Is it alive? ", 'N'));
	Memory.push_back(thing);
	ThinkinOf = thing;
}



Note for the error (from compiler):
- cadidates are:
- Thing::Thing(std::string)
- candidate expects 1 argument, 0 provided
- Thing::Thing(const Thing&)
- candidate expects 1 argument, 0 provided

If I add a string parameter to constructor Guesser, all the errors will go away, another appears: expected constructor, destructor, or type conversion before '(' token.

I think the IDE misunderstands constructor of Guesser to constructor of Thing. Any idea?
I think the IDE misunderstands constructor of Guesser to constructor of Thing. Any idea?


Stop believing the compiler "misunderstands" code. When an error occurs, it is most likely because of something you have done. Not so likely the compiler is just arbitrarily interpreting your code incorrectly.

A Thing must be initialized with a string. You don't do so in the Guesser constructor.

1
2
3
4
Guesser::Guesser() : ThinkinOf("rock")  // initialize your Thing.
{
    // ...
}



1
2
3
4
5
6
7
8
struct Thing {
....
class Guesser {
....
Thing ThinkinOf;
....
Guesser::Guesser()
....

This needs default constructor for struct Thing. Introducing the default constructor Thing(){} within struct Thing should solve the problem.
I think cire's way solved the problem. Change the constructor to:
1
2
3
Guesser::Guesser() : ThinkinOf("rock") {
	Memory.push_back(ThinkinOf);
}

Thanks cire.
Last edited on
Topic archived. No new replies allowed.