我对C ++比较陌生,我在eclipse上安装了CDT插件,用于在我的Ubuntu机器上进行开发。我是一名Java程序员,我正在努力让C ++代码正常工作。
我创建了一个这样的类:
RandomMapGenerator.hpp:
#ifndef RANDOMMAPGENERATOR_HPP_
#define RANDOMMAPGENERATOR_HPP_
class RandomMapGenerator {
public:
bool generateMap (std::vector<sf::ConvexShape> &polygonList, const char* name, unsigned int width,
unsigned int height, int seed, unsigned int frec, unsigned int dist, bool log, bool deleteOnFinish);
private:
bool loadMap (std::vector<sf::ConvexShape> &polygonList, const char* name, unsigned int &width,
unsigned int &height, bool log);
// Convierte 4 bytes a un int de 32 bits siendo b1 el MSB
unsigned int bytesToInt (unsigned char b1, unsigned char b2, unsigned char b3, unsigned char b4){
return ( b4 | (b3<<8) | (b2<<16) | (b1<<24) );
}
// Convierte 2 bytes a un int de 32 bits siendo b1 el MSB
unsigned int bytesToInt (unsigned char b1, unsigned char b2){
return ( b2 | (b1<<8) );
}
};
#endif /* RANDOMMAPGENERATOR_HPP_ */
RandomMapGenerator.cpp:
#include "RandomMapGenerator.hpp"
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <stdio.h>
bool RandomMapGenerator::generateMap (std::vector<sf::ConvexShape>& polygonList, const char* name, unsigned int width,
unsigned int height, int seed, unsigned int frec, unsigned int dist, bool log, bool deleteOnFinish) {
// Nombre del archivo binario generado
const std::string fileName = std::string(name, 0, 20) + ".map";
std::ostringstream command;
// Ejecuto el .jar que genera el archivo
command << "java -jar MapGenerator.jar" << " -w " << width << " -h " << height
<< " -d " << dist << " -s " << seed << " -f " << frec
<< " -b " << name << (log ? " -l " : "");
// http://stackoverflow.com/questions/1374468/c-stringstream-string-and-char-conversion-confusion
const std::string &cmd = command.str();
system(cmd.c_str());
// Genero los polígonos en base al archivo binario generado
bool result = loadMap(polygonList, fileName.c_str(), width, height, log);
// Borro el archivo .map si asi se especifico
if(deleteOnFinish) remove(fileName.c_str());
return result;
}
bool RandomMapGenerator::loadMap(std::vector<sf::ConvexShape>& polygonList, const char* name, unsigned int& width,
unsigned int& height, bool log) {
long int fileSize;
unsigned int polygonsNumber;
char *memBuffer;
std::ifstream file (name, std::ios::in | std::ios::binary | std::ios::ate);
std::ofstream logFile ("log_c.txt");
if (file.is_open() && logFile.is_open()) {
if(log) logFile << "Reserving memory" << std::endl;
fileSize = file.tellg(); // Tamaño del archivo en bytes
memBuffer = new char[fileSize]; // Array del tamaño del archivo
file.seekg (0, std::ios::beg); // Voy al comienzo del archivo
file.read(memBuffer, fileSize); // Copio el archivo a RAM
width = bytesToInt(memBuffer[0],memBuffer[1],memBuffer[2],memBuffer[3]);
height = bytesToInt(memBuffer[4],memBuffer[5],memBuffer[6],memBuffer[7]);
polygonsNumber = bytesToInt(memBuffer[8],memBuffer[9],memBuffer[10],memBuffer[11]);
if(log) logFile << "Size: " << fileSize/1000 << "Kb" << std::endl;
if(log) logFile << "Cantidad de poligonos: " << polygonsNumber << std::endl;
if(log) logFile << "Ancho: " << width << std::endl;
if(log) logFile << "Alto: " << height << std::endl;
// Reservo para la cantidad de poligonos y comienzo a leer
polygonList.reserve(polygonsNumber);
unsigned int pointCount;
unsigned int x,y,r,g,b,n;
for(n = 12; n < fileSize-16;){
// Cantidad de puntos del polígono
if(log) logFile << "n: " << n << std::endl;
pointCount = bytesToInt(memBuffer[n], memBuffer[n+1]);
n += 2;
// Leo las coordenadas (x,y) de cada punto siendo cada una de 4 bytes
sf::ConvexShape mPolygon;
mPolygon.setPointCount(pointCount);
if(log) logFile << "pointCount: " << pointCount << std::endl;
for(unsigned int i = 0; i < pointCount; ++i){
x = bytesToInt(memBuffer[n], memBuffer[n+1], memBuffer[n+2], memBuffer[n+3]);
n += 4;
y = bytesToInt(memBuffer[n], memBuffer[n+1], memBuffer[n+2], memBuffer[n+3]);
n += 4;
mPolygon.setPoint(i, sf::Vector2f(x,y));
if(log) logFile << std::dec << "(" << x << "," << y << ")" << std::endl;
}
// Leo el color del poligono siendo cada componente de 2 bytes
r = bytesToInt(memBuffer[n], memBuffer[n+1]); n += 2;
g = bytesToInt(memBuffer[n], memBuffer[n+1]); n += 2;
b = bytesToInt(memBuffer[n], memBuffer[n+1]); n += 2;
if(log) logFile << "R: " << r << " G: " << g << " B: " << b << std::endl << std::endl;
// Agrego el poligono a la lista
mPolygon.setFillColor(sf::Color(r,g,b,255));
polygonList.push_back(mPolygon);
}
file.close();
logFile.close();
delete memBuffer;
if(log) logFile << "Terminado - n: " << n << std::endl;
return true;
}
else {
std::cout << "Unable to open file" << std::endl;
return false;
}
}
Main.cpp的
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <AdvancedView.hpp>
#include <RandomMapGenerator.hpp>
using namespace std;
using namespace sf;
#define seed 80
int main() {
std::vector<sf::ConvexShape> list;
unsigned int screenWidth = 500;
unsigned int screenHeight = 500;
RandomMapGenerator map;
map.generateMap(list, "prueba", screenWidth, screenHeight, seed, 5, 3, true, false);
sf::RenderWindow mWindow(sf::VideoMode(screenWidth,screenHeight), "Cave Doom");
mWindow.clear();
for(vector<ConvexShape>::iterator it = list.begin(); it < list.end(); ++it){
mWindow.draw(*it);
}
mWindow.display();
return 0;
}
在generateMap()
的main.cpp中,我收到此错误
未定义的引用 `RandomMapGenerator :: generateMap(std :: vector&gt;&amp;,char const *,unsigned int, unsigned int,int,unsigned int,unsigned int,bool,bool)'
我正在传递正确类型的参数,为什么我收到此错误?我试过了:
RandomGenerator.hpp
和RandomGenerator.cpp
与main.cpp
不在同一文件夹中,但我已将该文件夹添加到构建路径中。
答案 0 :(得分:0)
我将我拥有文件的文件夹添加到Eclipse项目的源文件夹列表中,它现在正在运行!