Can somebody tell me whats wrong with my code please??? Thanks


In the code below I get these 2 errors:

Error E0289: No instance of constructor "Spy::Spy" matches the argument list

Compiler Error C2664: 'Spy::Spy(Spy &&)': cannot convert argument 2 from 'const char [16]' to 'char *'

Any suggestions how to fix it? Thanks

CODE:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

class Person {
public:
Person(const char* name = "nobody");
virtual ~Person() = default;
virtual void identity() const;
virtual void interrogate();

private:
std::string name;
};

class Spy: public Person {
public:
Spy(const char* name, char* alias, int i = 0);
void identity() const;
void interrogate();

void set_identity(const char* alias);

private:
std::string name;
std::string alias;
};


int main(int argc, char** argv) {
Person agent("James Bond");
Spy spy("Emilio Largo", "William Johnson", 3);
Spy spy2("Ernst Blofield", "John Keats", 5);
std::cout << std::endl << "Nice to meet you. ";
agent.identity();
for (int i = 0; i < 6; ++i) {
std::cout << "Who are you?" << std::endl;
spy.interrogate();
spy.identity();
}
spy.set_identity("Bill Munny");
spy.identity();
std::cout << std::endl << "Nice to meet you. ";
agent.identity();
for (int i = 0; i < 6; ++i) {
std::cout << "Who are you?" << std::endl;
spy2.interrogate();
spy2.identity();
}
return 0;
}

Have you considered using std::sting instead of C-strings for your constructor parameters?

The problem your compiler is finding is that the arguments you are tyring to pass to the constructor are C-string constants by the constructor expects non-constant C-strings. You may want to use the const qualifier in those constructors.

Thanks. You mean I should write: Spy(const char* name, const char* alias, const int i = 0);
Well I would really recommend something more like Spy(const std::string& name, const std::string name& alias, int someNumber);. Don't really know the purpose of "someNumber" since there is no class variable that is a single int.

Prefer std::string over C-strings. You also don't seem to need the destructor, the default should work.

I don't know what the various class functions are supposed to do, but this compiles and runs

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
#include <iostream>
#include <string>
using namespace std;

class Person {
public:
	Person(const char* n = "nobody") : name(n) {}
	virtual ~Person() = default;
	virtual string identity() const { return name; }
	virtual void interrogate() {}

protected:
	std::string name;
};

class Spy : public Person {
public:
	Spy(const char* n, const char* a, int n1) : Person(n), alias(a), n(n1) {}
	string identity() const override { return alias; }
	void interrogate() override { getline(cin, name); }
	void set_identity(const char* al) {	alias = al; }

private:
	std::string alias;
	int n;
};


int main()
{
	Person agent("James Bond");

	Spy spy("Emilio Largo", "William Johnson", 3);
	Spy spy2("Ernst Blofield", "John Keats", 5);

	cout << "\nNice to meet you. \n";
	cout << agent.identity() << '\n';

	for (int i = 0; i < 6; ++i) {
		cout << "Who are you? ";
		spy.interrogate();
		cout << spy.identity() << '\n';
	}

	spy.set_identity("Bill Munny");
	cout << spy.identity() << '\n';

	cout << std::endl << "Nice to meet you. ";
	cout << agent.identity() << '\n';

	for (int i = 0; i < 6; ++i) {
		std::cout << "Who are you? ";
		spy2.interrogate();
		spy2.identity();
	}
	return 0;
}

Thanks seeplus!
Topic archived. No new replies allowed.