Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversion from 'KanjiCard*' to non-scalar type 'KanjiCard' requested (custom enum)

Tags:

c++

c

enums

g++

Okay, I've tried using Google-sensei and searching around on this website, and while I've found many posts about this error, I haven't found any that have addressed enums. Also, all the ones I have seen have either been someone trying to assign one type to another, improper use of 'new', etc. As far as I can tell, that's not the case in this instance.

As stated in the title, I'm getting an error conversion from 'KanjiCard*' to non-scalar type 'KanjiCard' requested when trying to compile the program I'm working on using g++.

I have a class named KanjiCard that has this publicly-defined enum:

enum KanjiCardType {
    KANJI_CARD = 1,
    KEYWORD_CARD = 2
};

The constructor for the class is defined as follows:

    KanjiCard(char *cardText, int cardTextLength, int cardWidth, int cardHeight,
        int cardX, int cardY, KanjiCardType cardType, SDL_Color textColor);

(I'm using char* instead of std::string because it's easier to work with the libraries I'm using that way.)

I'm calling it to create new cards like so (this is where the error comes up):

KanjiCard currentCard = new KanjiCard(kanji_line, strlen(kanji_line), 
    CARD_WIDTH, CARD_HEIGHT, xPos, yPos, cardType, textColor);

cardType is defined as such: KanjiCard::KanjiCardType cardType = KanjiCard::KANJI_CARD;

I originally tried just passing in KanjiCard::KANJI_CARD where cardType is in that constructor call right now, but I got that error, so I've been trying everything I could think of to try and get it to work, including trying switching the cardType parameter in the constructor to *cardType and &cardType and messing around with the type of cardType in the call as well, to no avail.

As far as I can tell, cardType isn't a pointer, so I really can't figure out why I'm getting that message. Any help would be greatly appreciated, as I'm at my wit's end trying to figure this out.

Edit: I should also mention that I also tried pulling the enum out of the class (and removing the KanjiCard:: preface, of course), and still had the same issue.

like image 385
Kanmuri Avatar asked Aug 15 '11 06:08

Kanmuri


2 Answers

It's not cardType that's the problem; it's currentCard. You wrote this:

KanjiCard currentCard = new KanjiCard(...

That's assigning a pointer to a non-pointer type. You may have meant this:

KanjiCard *currentCard = new KanjiCard(...

...or you may have meant this:

KanjiCard currentCard(...

The former of my suggestions will allocate a KanjiCard on the heap. currentCard will point to the KanjiCard. The latter will allocate it on the stack. It is a KanjiCard.

The former you'll have to manually delete. The latter doesn't need to be deleted; it is valid within the scope it's declared and once it goes out of scope, it's invalid.

The former you can access members of using ->. The latter you can access members of using ..

like image 160
icktoofay Avatar answered Sep 26 '22 19:09

icktoofay


Do

KanjiCard currentCard = KanjiCard(
    kanji_line, strlen(kanji_line), CARD_WIDTH, CARD_HEIGHT, xPos, yPos, cardType, textColor
    );

or

KanjiCard currentCard(
    kanji_line, strlen(kanji_line), CARD_WIDTH, CARD_HEIGHT, xPos, yPos, cardType, textColor
    );

The first is called copy initialization, and the second is called direct initialization.

Note that there is no new here.

Cheers & hth.,

like image 27
Cheers and hth. - Alf Avatar answered Sep 22 '22 19:09

Cheers and hth. - Alf