'未在此范围内宣布' C ++

时间:2015-08-09 17:38:32

标签: c++ compiler-errors sfml

我有一些粗鲁的错误......我在网上搜索它,我能够随处读她,但仍然没有解决我的问题......

这是我的主要

#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();
}

以下是错误:

  

错误:&#39;地图&#39;在这方面没有申明   错误:预期&#39;;&#39;之前&#39; mainMap&#39;   错误:&#39; mainMap&#39;在这方面没有申明   错误:未在此范围内声明callFunction

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

问题可能存在于标题中,但我无法弄清楚。

2 个答案:

答案 0 :(得分:4)

  

问题可能与标题有关,但无法找到原因。

这是绝对正确的!您错误地应用了inclusion guards

您使用了错误的hpp文件中的包含守卫:

#ifndef DECODE_H_INCLUDED
map.hpp中的

来自decode.hpp。它应该是

#ifndef MAP_H_INCLUDED

答案 1 :(得分:2)

我认为问题与Map.hpp中的这些宏定义有关

#ifndef DECODE_H_INCLUDED
#define DECODE_H_INCLUDED

看起来它们与在Map.hpp

之前包含的decode.hpp中使用的定义相同

考虑到至少这个构造函数是错误的没有意义并且不会编译

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] + "+";
   }
}

这些本地对象

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

未使用。 我认为你的意思是类数据成员而不是本地对象。:)

这句话

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

错了,没有意义。

也是数据成员

int m_obstacle;

被声明为标量对象。您可能不会像数组一样在构造函数中重新声明它。