0

Car is a template class, and its constructor is defined as follows:

template <class carObject>
explicit Car( const carObject & notFound, int size = 101 );
Car( const carObject & rhs )

In my header file (h), I have:

class Storage{
public:
    Storage();
    Car <char*> myCars[12];
};

In my CPP file (.cpp), I'm trying to initialize myCars array using initializing list as follow:

Storage::Storage()
: myCars("notFound", 20)
{ //my other stuff}

but I keep getting the error message:

error: invalid initializer for array member Car:myCars("notFound", 20)

Any help?

8
  • Where does Storage::Storage come from? Commented Nov 17, 2015 at 8:09
  • 1
    @TonyTheLion it's inherited from Garage Commented Nov 17, 2015 at 8:10
  • @TonyTheLion Storage::Storage is the class that has Car <char*> mycars[12] declared in Commented Nov 17, 2015 at 8:10
  • @Andy the point is that it isn't obvious from your question. You need to add all relevant information to your question. Commented Nov 17, 2015 at 8:11
  • 3
    @Andy Don't do this. Make your question correct. There's no excuse for being lazy there. Just copy your actual code. Make it SSCCE. See also Nobody Writes Testcases Anymore and Solve your problem by almost asking a question on Stackoverflow. Commented Nov 17, 2015 at 8:48

1 Answer 1

2

Your array need 12 initializers like

Live On Coliru

template <typename T> struct Car {
    template <class carObject> explicit Car(const carObject &notFound, int size = 101) {}
};

struct Storage {
    Storage()
            : myCars{
                  Car<char *>{ "notFound", 20 }, Car<char *>{ "notFound", 20 }, Car<char *>{ "notFound", 20 },
                  Car<char *>{ "notFound", 20 }, Car<char *>{ "notFound", 20 }, Car<char *>{ "notFound", 20 },
                  Car<char *>{ "notFound", 20 }, Car<char *>{ "notFound", 20 }, Car<char *>{ "notFound", 20 },
                  Car<char *>{ "notFound", 20 }, Car<char *>{ "notFound", 20 }, Car<char *>{ "notFound", 20 },
              } {}
    Car<char *> myCars[12];
};

int main(){}

Of course you can leave out some initializers if the element type is default-constructible. But the structure of the initializer would need to match this structure

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

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.