当我编译我的项目时,Visual Studio说它无法找到.exe文件。我认为这是因为程序无法编译。我修复了大部分的错误,但我无法弄清楚剩下的是什么。
1>------ Build started: Project: Hmwk.3.Assignment, Configuration: Debug Win32 ------
1> engine.cpp
1>c:\users\tmars_000\desktop\hmwk.3.released\engine.cpp(23): error C2082: redefinition of formal parameter 'elements'
1>c:\users\tmars_000\desktop\hmwk.3.released\engine.cpp(29): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users\tmars_000\desktop\hmwk.3.released\engine.cpp(30): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我不确定如何以修复错误的方式格式化Block **。
//Engine.cpp
include <iostream>
#include <fstream>
#include <string>
#include "engine.h"
#include "Player.h"
#include "Opponent.h"
using namespace std;
fstream fin;
void loadBackground(std::string gameFile, int & numElements, Block**& elements){
int tempX;
int tempY;
fin.open(gameFile,ios::in);
fin >> numElements;
Block** elements = new Block*[numElements];
for(int i=0; i<numElements; i++){
elements[i] = new Block();
fin >> tempX;
fin >> tempY;
elements[i]->setPosX(tempX);
elements[i]->setPosY(tempY);
}
fin.close();
}
void changeGameState(std::string command, Player* player, Opponent* opponent){
player->update(command);
opponent->update(command);
}
当我将其更改为仅仅元素= new Block * [numElements];它造成了这些错误。
1>------ Build started: Project: Hmwk.3.Assignment, Configuration: Debug Win32 ------
1> engine.cpp
1>c:\users\tmars_000\desktop\hmwk.3.released\engine.cpp(25): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>c:\users\tmars_000\desktop\hmwk.3.released\engine.cpp(26): warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data
1>Opponent.obj : error LNK2005: "class std::basic_fstream<char,struct std::char_traits<char> > fin" (?fin@@3V?$basic_fstream@DU?$char_traits@D@std@@@std@@A) already defined in engine.obj
1>Player.obj : error LNK2005: "class std::basic_fstream<char,struct std::char_traits<char> > fin" (?fin@@3V?$basic_fstream@DU?$char_traits@D@std@@@std@@A) already defined in engine.obj
1>gui.obj : error LNK2019: unresolved external symbol "public: int __thiscall Opponent::getSpriteID(void)" (?getSpriteID@Opponent@@QAEHXZ) referenced in function "public: void __thiscall GUI::displayGameState(class Player *,class Opponent *,int,class Block * *)" (?displayGameState@GUI@@QAEXPAVPlayer@@PAVOpponent@@HPAPAVBlock@@@Z)
1>C:\Users\tmars_000\Desktop\Hmwk.3.Released\Debug\Hmwk.3.Assignment.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
这些是否与该行的格式相关,或者它们是否可能仅在编译该行后出现?
答案 0 :(得分:0)
我确定另一个答案会更加深入,但短版本正是错误所说的:
void loadBackground(std::string gameFile, int & numElements, Block**& elements){ //elements is being defined as a parameter name
int tempX;
int tempY;
fin.open(gameFile,ios::in);
fin >> numElements;
Block** elements = new Block*[numElements]; //ERROR: elements is being redefined
for(int i=0; i<numElements; i++){
elements[i] = new Block();
fin >> tempX;
fin >> tempY;
elements[i]->setPosX(tempX);
elements[i]->setPosY(tempY);
}
fin.close();
}
因此,您可以重命名elements
。
答案 1 :(得分:0)
编译器告诉你,你告诉它两次elements
是什么,因此错误。修复它只需替换
Block** elements = new Block*[numElements];
有:
elements = new Block*[numElements];
如果我理解正确,您正在尝试分配内存并使用作为参数传递的指针指向它。这正是它的作用。