2013-06-12 83 views
1

我对C++比较新,我在Ubuntu机器上安装了用于eclipse开发的CDT Plugin。我是一名Java程序员,我努力努力让C++代码正常工作。为什么我在Eclipse CDT上遇到这个错误?

我创造了这样一个类:

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; 
} 

在main.cpp中对generateMap()我得到这个错误

未定义参考 `RandomMapGenerator :: generateMap(标准::矢量> &,字符常量*,无符号整型, unsigned int类型,INT ,unsigned int,unsigned int,bool,bool)'

我传递了正确类型的参数,为什么我得到这个错误?我想:

  • 清洁工程
  • 。重新启动Eclipse
  • 重建索引

RandomGenerator.hppRandomGenerator.cpp不在同一文件夹中main.cpp但我已经加入了文件夹到构建路径。

+0

您确定这是您使用的代码吗?它对我来说编译得很好。 –

+3

似乎你没有将'RandomGenerator.cpp'添加到你的项目中,所以它不会被编译和链接。 – Lol4t0

+0

怀疑你应该在包含你的'main''#include'RandomMapGenerator.hpp''之前放置任何系统'#include '语句。我更喜欢系统在头本身包含语句(RandomMapGenerator.hpp)。 –

回答

0

我将文件夹添加到Eclipse项目的源文件夹列表中,现在它正在工作!