1
votes

I have some rude errors... I search the web about it, and i was able to read her everywhere, but still not fix my problem ...

Here is my main :

#include <iostream>
#include "SFML/Network.hpp"
#include "decode.hpp"
#include "listOfFunction.hpp"
#include "map.hpp"

void createSocket(){
    unsigned short bindPort = 12800;
    unsigned short clientPort;
    int ret = 0;
    sf::UdpSocket socket;
    sf::IpAddress clientAddr;
    sf::Packet packet;
    Map mainMap;

    if (socket.bind(bindPort) != sf::Socket::Done){
        if (sf::Socket::Error) std::cout << "An unexpected error happened : Fatal Error !" << std::endl;
        if (sf::Socket::NotReady) std::cout << "The socket is not ready to send/receive data yet !" << std::endl;
    }

    while(1){
        packet.clear();
        socket.receive(packet, clientAddr, clientPort);
        std::string header = readFromPacket(packet);
        ret = callFunction(packet, header, clientAddr, clientPort, mainMap, socket);
    }
}

int main(){
    createSocket();
}

Here are the errors :

error : 'Map' was not declared in this scope error : expected ';'before 'mainMap' error : 'mainMap' was not declared in this scope error : callFunction was not declared in this scope

Map.cpp :

#include <iostream>
#include <vector>
#include "SFML/Network.hpp"
#include "map.hpp"

Map::Map(){
    char number[5] = "\0";
    int m_obstacle[80000] = {0};
    std::string m_error = "not error";
    std::vector <int> m_posPlayer(0);
    std::vector <std::string> m_namePlayer(0);
    std::vector <sf::IpAddress> m_addressPlayer(0);
    std::string m_stringObstacle = "";
    for (int i=0;i<80000;i++){
        sprintf(number, "%d", m_obstacle[i]);
        m_stringObstacle += std::string(number) + "+";
    }
}

int Map::sendMap(sf::IpAddress address, unsigned short clientPort, sf::UdpSocket& socket){
    std::string header = "rcv_map";
    sf::Packet packet;
    packet >> header >> m_stringObstacle;
    if(socket.send(packet, address, clientPort)!=sf::UdpSocket::Done){
        m_error += "Error sending the packet \n";
    }
    return 0;
}

int Map::error(){
    if (m_error != "not_error"){
        std::cout << "here is a following list of errors : " << m_error << std::endl;
    }
    m_error.erase(0,m_error.length());
}

Map.hpp:

#include <iostream>
#include "SFML/Network.hpp"

#ifndef DECODE_H_INCLUDED
#define DECODE_H_INCLUDED

class Map{

    public:
        Map();
        int sendMap(sf::IpAddress address, unsigned short clientPort, sf::UdpSocket& socket);
        int error();

    private:
        int m_obstacle;
        std::vector <int> m_posPlayer;
        std::vector <int> m_namePlayer;
        std::vector <sf::IpAddress> m_addressPlayer;
        std::string m_stringObstacle;
};

#endif // DECODE_H_INCLUDED

The problem probably exists in the headers, but I can't figure it out.

2
Are you sure that map.hpp available for compiler and really included to cpp files? Can you show how compiler is called? - VolAnd
You should put your #include statements inside your include guards. - Galik
If you use not Windows environment names of files are important : Map.hpp is not map.hpp - VolAnd

2 Answers

4
votes

Problems are probably about headers, but can't find why.

That is absolutely right! You have applied inclusion guards incorrectly.

You have used the inclusion guard from a wrong hpp file:

#ifndef DECODE_H_INCLUDED

in map.hpp comes from decode.hpp. It should be

#ifndef MAP_H_INCLUDED
2
votes

I think that the problem is related to these macro definitions in Map.hpp

#ifndef DECODE_H_INCLUDED
#define DECODE_H_INCLUDED

It seems they are the same definitions that are used in decode.hpp that is included before Map.hpp

Take into account that at least this constructor is wrong does not make sense and will not compile

Map::Map(){
    int i = 0;
    int m_obstacle[80000] = {0};
    std::vector <int> m_posPlayer(0);
    std::vector <std::string> m_namePlayer(0);
    std::vector <sf::IpAddress> m_addressPlayer(0);
    std::string m_stringObstacle = "";
    for (i=0;i<80000;i++){
        m_stringObstacle += m_obstacle[i] + "+";
   }
}

These local objects

    std::vector <int> m_posPlayer(0);
    std::vector <std::string> m_namePlayer(0);
    std::vector <sf::IpAddress> m_addressPlayer(0);
    std::string m_stringObstacle = "";

are not used. I think you mean class data members instead of the local objects.:)

And this statement

    m_stringObstacle += m_obstacle[i] + "+";

is wrong and does not make sense.

Also data member

int m_obstacle;

is declared like a scalar object. You may not redeclare it in the constructor like an array.