我在此部分代码中遇到编译器错误。据说没有从NPC到NPC *的适当转换。
Player::Player(Game* g) {
target = g->getNPCListItem(0);
}
这就是main中所说的:
int main()
{
Game* gameRef = new Game();
}
这是Game的构造函数:
Game::Game() {
player = new Player(this);
}
最后,这是目标指针的代码:
class Player {
NPC* target;
}
我觉得我的代码的另一部分是问题。
这是属性:
class Game {
NPC* nPCList[2];
}
这是初始化:
bool Game::initNPCList() {
nPCList[0] = new NPC("Gregory");
nPCList[1] = new NPC("Tasha");
return false;
}
最后,这是可能会产生编译器错误的代码行。
NPC& Game::getNPCListItem(int i) {
return *nPCList[i];
}
让我知道是否需要添加更多信息。
输出:
1>------ Build started: Project: CloutGame, Configuration: Debug Win32 ------
1>misc.cpp
1>C:\Zach In A Bear Studios\CloutGame\CloutGame\CloutGame\misc.cpp(66,31): error C2440: '=': cannot convert from 'NPC' to 'NPC *'
1>C:\Zach In A Bear Studios\CloutGame\CloutGame\CloutGame\misc.cpp(66,28): message : No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Done building project "CloutGame.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
错误 list :
Severity Code Description Project File Line Suppression State
Error (active) E0413 no suitable conversion function from "NPC" to "NPC *" exists CloutGame C:\Zach In A Bear Studios\CloutGame\CloutGame\CloutGame\misc.cpp 66
Error C2440 '=': cannot convert from 'NPC' to 'NPC *' CloutGame C:\Zach In A Bear Studios\CloutGame\CloutGame\CloutGame\misc.cpp 66
最佳答案
更改
NPC& Game::getNPCListItem(int i) {
return *nPCList[i];
}
至
NPC* Game::getNPCListItem(int i) {
return nPCList[i];
}