问题描述
好吧,我一直在使用谷歌,老师与本网站上搜索周围的尝试,虽然我发现这个错误的很多帖子,我还没有发现任何已经解决枚举。此外,所有我见过的那些要么被别人试图将一种类型分配给另一个,使用不当'新'等,至于我可以告诉大家的,这不是在这种情况下的情况。
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.
正如标题所说,我得到一个错误从'KanjiCard *'非标量类型转换'KanjiCard要求试图编译程序的时候
我M工作使用G ++。
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++.
我有一个名为KanjiCard类具有此公开定义枚举:
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);
(我用的char *来代替std ::字符串,因为它更容易与我使用这种方式的库工作。)
(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
的定义是这样的: KanjiCard :: KanjiCardType cardType = KanjiCard :: KANJI_CARD;
我最初尝试只是传递 KanjiCard :: KANJI_CARD
,其中 cardType
是在构造函数调用的权利,但我得到的错误,所以我一直在乱投医我能想到的尝试,并得到它的工作,包括努力在构造函数中 cardType
参数切换到 * cardType
和&放大器; cardType
和 cardType 在通话为好,都无济于事。
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.
据我所知,
cardType
不是指针,所以我真的想不通,为什么我收到消息。任何帮助将大大AP preciated,因为我在我束手无策试图弄清楚这一点。
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.
编辑:我应该还提到,我也试图拉出来枚举类的(并删除
KanjiCard ::
preface当然),仍然有同样的问题。
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.
推荐答案
这不是
cardType
这就是问题所在;这是 currentCard
。你写这样的:
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(...
前者我建议将在堆中分配
KanjiCard
。 currentCard
将指向 KanjiCard
。后者将它分配在堆栈中。它的是的一个 KanjiCard
。
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
.
前者你必须手动
删除
。后者并不需要是删除
D;这是它的声明范围内有效,一旦超出范围,它是无效的。
The former you'll have to manually
delete
. The latter doesn't need to be delete
d; 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 .
.
这篇关于从“KanjiCard *'非标量型”KanjiCard'要求(自定义枚举)转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!