0

Here is the constructor from my Character class :

Character(int, int, int, string, string, string, string, string, double, double, bool, bool);

And in the main function, here is how I have tried :

Character character[100];
character[0] = {777, 10, 5, "one", "type", "desc", "status", "knife", 10.0, 20.0, false, true};
character[1] = {707, 5, 10, "two", "type2", "desc2", "status", "knife", 15.0, 23.4, true, true};

The error state that it has no storage class or type specifier..

EDIT* Finally got it right using this : p/s - Please provide a better solution if any. =/

Character character[100] = { character[0] = { 777, 10, 5, "one", "type", "desc", "status", "knife", 10.0, 20.0, false, true },
                             character[1] = { 707, 5, 10, "two", "type2", "desc2", "status", "gun", 15.0, 23.4, true, true },
                             character[2] = { 888, 7, 43, "three", "type3", "desc3", "status", "sword", 15.0, 23.4, false, false } };
8
  • 5
    It's assignment, not initialization. Commented Aug 1, 2014 at 13:33
  • 1
    I would have absolutely no idea what to pass into that constructor without looking. That's a really bad sign. Commented Aug 1, 2014 at 13:33
  • 2
    Looks like you have a forward-declaration, but haven't included the actual header containing the class definition. Commented Aug 1, 2014 at 13:33
  • 1
    why don't people use std::vector? Commented Aug 1, 2014 at 13:37
  • 2
    @snurby77 Bad C++ tutorials or teachers, usually. Commented Aug 1, 2014 at 13:39

2 Answers 2

2

Assuming you have operator= properly defined, you can do this:

Character character[100];
character[0] = Character( 777, 10, 5, "one", "type", "desc", "status", "knife", 10.0, 20.0, false, true );
character[1] = Character( 707, 5, 10, "two", "type2", "desc2", "status", "knife", 15.0, 23.4, true, true );
Sign up to request clarification or add additional context in comments.

4 Comments

What about this? Is it the same? Character character[100] = {character[0] = { 777, 10, 5, "one", "type", "desc", "status", "knife", 10.0, 20.0, false, true }, character[1] = { 707, 5, 10, "two", "type2", "desc2", "status", "gun", 15.0, 23.4, true, true }, character[2] = { 888, 7, 43, "three", "type3", "desc3", "status", "sword", 15.0, 23.4, false, false}};
Tried your method. Not working. Still having errors. Image LINK
Do you have a constructor for Character that takes no arguments? You'll need that to declare the array. If you don't want to do that, I'd suggest using a vector. Also, showing the actual error message would be helpful.
Yes I have the no argument constructor.
1

If you only mean to selectively initialize the elements in the array, C99 has designated initializers which also seem to work in C++.

Character character[100] = {
    [0] = Character( 777, 10, 5, "one", "type", "desc", "status", "knife", 10.0, 20.0, false, true ),
    [1] = Character( 707, 5, 10, "two", "type2", "desc2", "status", "knife", 15.0, 23.4, true, true )
};

You must also have a default Character constructor for the rest of the elements.

However this feature seems to not exist in C++11 but gcc seems to handle it though (probably via an extension).

9 Comments

What is C99? Sorry I'm still new to all this.
@Student I was referring to the C99 standard.
Still not working with your method. What wrong am i doing. =/ postimg.org/image/pa57e6adb
@Student: I had some misplaced brackets. I reedited my example. Please try again ...
@dragosht Some compilers allow the use of some C99 features but a warning is clearly displayed. Obviously my comment was about the extension part.
|

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.