2

I've written this code but I have some errors when I try to initialize an array of Critter objects and don't know what they're about.

My code:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Critter {
private:
    string crName;
public:
    Critter(string = "Poochie");
    string getName() const { return crName; }
};

Critter::Critter(string n) {
    crName = n;
}

int main() {
    Critter c[10] = { "bob","neo","judy","patrik","popo" }; //here
    return 0;
}

The errors:

E0415 - no suitable constructor exists to convert from "const char [4]" to "Critter"
...
4 more like this.

This code worked on a friend's Visual Studio 2017, but not in mine which is the 2019 version.

Thanks.

2
  • 2
    ideone.com/he9Ppn Commented Jun 15, 2020 at 17:59
  • 3
    Plenty of fine answers already, but yet another way to initialize it would be to put using namespace std::literals::string_literals; in scope, then do Critter c[10] = { "bob"s, "neo"s, "judy"s, "patrik"s, "popo"s }; Commented Jun 15, 2020 at 19:15

4 Answers 4

10

The initialization you have is for an array of strings, for the objects you need:

Critter c[10] = {Critter("bob"), Critter("neo"), Critter("judy"),
                 Critter("patrik"), Critter("popo")};

Or

Critter c[10] = {{"bob"}, {"neo"}, {"judy"}, //(*)
                 {"patrik"}, {"popo"}}; 

*This second method is credited to @drescherjm comment followed by @dxiv's answer, both mentioned it first.

This second initialization may be what your friend used, and maybe you forgot the braces, the IDE version difference doesn't seem relevant here.

Note that C++ provides a container for fixed size arrays, std::array:

std::array<Critter, 10> c = {Critter("bob"), Critter("neo"),
                             Critter("judy"), Critter("patrik"), Critter("popo")};

On a side note:

You should avoid using namespace std;

Sign up to request clarification or add additional context in comments.

Comments

4

Critter c[10] = { "bob","neo","judy","patrik","popo" };

This defines an array of const char *. To initialize an array of Critter with those strings, instead:

Critter c[10] = { {"bob"}, {"neo"}, {"judy"}, {"patrik"}, {"popo"} };


[ EDIT ] It was pointed out that the same answer was first posted in a comment, only it was hidden in an external link with no indication of what's behind it, which I did not see before posting the above. Credit goes to @drescherjm so I'll leave this here as a CW.

Comments

0

string c[10] = {"bob","neo","judy","patrik","popo"}; Would be correct.

{"bob","neo","judy","patrik","popo"} is an array containing string elements.

You need to do

Critter c[10]={ Critter("bob"),Critter("neo"),Critter("judy"),Critter("patrik"),Critter("popo")};

Comments

-1

C++ only allows one "user-defined conversion" at a time.

You're providing char const[N]s (let's call them char const*s for the sake of argument), which need to be turned into std::strings (which count as "user-defined" in this context), which need to be turned into Critters.

That's two conversions, not one.

That's just a limitation of C++, I'm afraid. You'll have to temporarily instantiate either strings or Critters within that initialiser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.