1

I am relatively new to C++ and I installed CDT Plugin for eclipse for developing on my Ubuntu machine. I am a Java programmer and I am struggling hard to get C++ code work correctly.

I created a class like this:

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

In main.cpp on generateMap() I get this error

undefined reference to `RandomMapGenerator::generateMap(std::vector >&, char const*, unsigned int, unsigned int, int, unsigned int, unsigned int, bool, bool)'

I am passing the correct type of arguments why I am getting this error? I tried:

  • Cleaning Project
  • Restarting Eclipse
  • Rebuilding Index

RandomGenerator.hpp and RandomGenerator.cpp are not in the same folder as main.cpp but I have added the folder to the build path.

taocp
  • 23,276
  • 10
  • 49
  • 62
Andres
  • 6,080
  • 13
  • 60
  • 110
  • Are you sure that this is the code you're using? It compiles fine for me. – Simon G. Jun 12 '13 at 17:33
  • 3
    Seems you didn't add `RandomGenerator.cpp` to your project, so it doesn't get compiled and linked. – Lol4t0 Jun 12 '13 at 17:33
  • In doubt you should place any system `#include ` statements before including your 'main' `#include "RandomMapGenerator.hpp"`. I prefer to have system include statements in the header itself (RandomMapGenerator.hpp). – πάντα ῥεῖ Jun 12 '13 at 17:33
  • @Lol4t0 you are a genious!! I added the folder where I have the files to the list of source folders and it is working! Sorry for the inconvenience, I am getting used to with this things of C++! – Andres Jun 12 '13 at 17:37

1 Answers1

0

I added the folder where I have the files to the list of source folders of the Eclipse project and it is working now!

Andres
  • 6,080
  • 13
  • 60
  • 110