7 IntelliSense: no suitable user-defined conversion from "Difficulty" to "Difficulty" exists e:\ICS3U CT\CT\game.h 29 10 CT
这是错误。下面列出了该文件的受影响部分。
typedef struct Difficulty{
char* name;
char* description;
bool cumulatesHungerLoss;
int rngMod;
Difficulty(char* n, char* desc, bool cumulate, int rng){
name = n;
description = desc;
cumulatesHungerLoss = cumulate;
rngMod = rng;
}
};
Difficulty& easy = Difficulty("EASY","Normal RNG and Hunger Loss",false,0);
Difficulty& hard = Difficulty("HARD","Harder RNG and cumulative Hunger Loss",true,5);
Difficulty& insane = Difficulty("INSANE","Very Hard RNG and Cumulative Hunger Loss",true,15);
Difficulty getById(int id){
switch(id){
case 0:
return easy;
break;
case 1:
return hard;
break;
case 2:
return insane;
break;
}
return *NULL;
}
代码在C中,我只定义了一个名为Difficulty的结构类型。错误是返回容易;回归艰难;并且返回疯狂;。
每当我尝试将用户定义类型的值分配给该类型的另一个值时,就会发生这种情况。它搞乱了我的游戏,因为我需要将我的用户类型的值分配给数组并从函数返回值。这只影响我的一些类型。我可以保证我定义的每一种类型的唯一性。
[编辑]我打算用C编写代码。我正在编写的程序是一个C程序,如果它似乎是在C ++中,我很抱歉,它可能在C ++中解释,尽管意图是用C写的。
答案 0 :(得分:1)
它是C ++,而不是C.我编辑了你的问题以解决这个问题。
Difficulty& easy = Difficulty("EASY","Normal RNG and Hunger Loss",false,0);
Difficulty& hard = Difficulty("HARD","Harder RNG and cumulative Hunger Loss",true,5);
Difficulty& insane = Difficulty("INSANE","Very Hard RNG and Cumulative Hunger Loss",true,15);
问题在于您是否尝试将临时对象绑定到非const引用。您不需要引用,只需直接定义一些命名对象:
const Difficulty easy("EASY","Normal RNG and Hunger Loss",false,0);
const Difficulty hard("HARD","Harder RNG and cumulative Hunger Loss",true,5);
const Difficulty insane("INSANE","Very Hard RNG and Cumulative Hunger Loss",true,15);
这不是唯一的问题,但它是你提出的问题。