0

I am having problems initializing Warrior Objects in my main function

The code for my Warrior Class is below

class Warrior{
public:

Warrior(const string& input_name, int& input_strength) :name(input_name), strength(input_strength) {};

string getname()const{
    return name;
};

int getstrength(){
    return strength;
};

void change_strength(int damg_taken){
    strength -= damg_taken;
};

private:
string name;
int strength;
};

This is part of the code for the main function

Warrior cheetah("Tarzan", 10);
Warrior wizard("Merlin", 15);
Warrior theGovernator("Conan", 12);
Warrior nimoy("Spock", 15);
Warrior lawless("Xena", 20);
Warrior mrGreen("Hulk", 8);
Warrior dylan("Hercules", 3);

All of the Warrior initializations in the main function cause a error that is something like this:

Error:no instance of constructor "Warrior::Warrior" matches the argument list argument types are:(const char[7],int)

I read somewhere that strings in C++ are actually arrays of chars and that is why the warrior initialization are not working but I don't know how to change the constructor to make the program work. Also I am not allowed to change the main function because it was provided by the instructor.

  • Why is it `int&`? You're not even changing it. – chris Mar 01 '15 at 04:29
  • I don't think the string is your problem; it's the `int&` which requires a reference, but you are passing it a literal value to which a reference can't be taken. – Jonathan Potter Mar 01 '15 at 04:33
  • Indeed this has nothing to do with the string, and you'd know that if you'd constructed a [MCVE](http://stackoverflow.com/help/mcve). I've answered, as you're new, but please next time ensure that your programs are _minimal_: until they are, you have not finished your own debugging and should not be posting a question. Thanks! – Lightness Races in Orbit Mar 01 '15 at 04:38
  • Thanks, getting rid of the & worked, I guess I don't really understand pass-by-reference or pass-by-value that much so I didn't realize that passing the second parameter by reference would make the code not work. – I_don't_get_it Mar 01 '15 at 04:40
  • @I_don't_get_it: Because of this: http://stackoverflow.com/q/7701221/560648 Don't worry about it too much. There was just no reason for you to pass those `int`s by reference. – Lightness Races in Orbit Mar 01 '15 at 04:41
  • 1
    Accepting a const reference to a `string` instead of just a `string` is generally a good idea, since you're passing around way less data when calling the function, therefore gaining some performance (especially with large strings). But in the case of an `int`, you don't need a reference, since a pointer is about the same size as an `int`, so you're not gaining any performance there. And of course the way you did it is also wrong, as others have said. – notadam Mar 01 '15 at 04:54
  • 1
    As stated in the question kinked by @Lightness he is loosing performance because now you're gonna dereference the pointer. – Rptx Mar 01 '15 at 04:57
  • So as a general rule of thumb non-primitive data types should be passed by reference and primitive data types should be passed by value. Is this correct? – I_don't_get_it Mar 03 '15 at 17:55

2 Answers2

5

The problem is not the string. The const char[7] would be successfully used to construct the temporary std::strings.

The problem is you are trying to bind int literals to references-to-non-const; you cannot do that. Accepting const int& into your constructor would fix your program.

However, I recommend changing your constructor to take its ints by value, as it does not need to modify the originals and ints are small:

Warrior(const string& input_name, int input_strength)
//                                ^^^
   : name(input_name)
   , strength(input_strength)
{}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

You are asking for a reference to non-const int& in the constructor and providing an int literal. The error is telling you that there is no constructor taking an int as an argument. Passing the int by value should fix it. Also, A string literal (string between quotes) in C++, is a null terminated char array, not a std::string, although it can be used to construct one, and it's not causing the error in this case.

Rptx
  • 1,159
  • 1
  • 12
  • 17
  • `std::string` is constructible from a string literal. `int&`s are also constructible from `int`s (as long as they have the correct value category). When a function wants an `int&` you _have_ to provide an `int`. So this is pretty much all wrong I'm afraid. – Lightness Races in Orbit Mar 01 '15 at 04:42
  • I didn't mean to.say that the strings can't be constructed. I was just trying to clarify to the OP what the string literal is. And as the error in the program. And as you said, you can't pass an int literal to a no const int&. which is the cause of the error OP was getting. – Rptx Mar 01 '15 at 04:47
  • Right but that's not what your answer says. – Lightness Races in Orbit Mar 01 '15 at 04:48
  • It's still not right. The error has nothing to do with an attempt to "modify a literal". Nobody's attempting to do so. Note that if the constructor took `int&&` then it'd work fine _and_ we could modify it. – Lightness Races in Orbit Mar 01 '15 at 05:07
  • How about now? Thanks for your feedback. – Rptx Mar 01 '15 at 05:17
  • Yeah now it's pretty much the same as my answer :p – Lightness Races in Orbit Mar 01 '15 at 05:21
  • Yeah. I knew I was going to end up with your answer. I started answering when there where no answers still. At least it's right. – Rptx Mar 01 '15 at 05:29