繁体   English   中英

'未在此范围内声明'C ++

[英]' was not declared in this scope' C++

我有一些粗鲁的错误...我在网上搜索了一下,并且能够在任何地方阅读她,但仍然无法解决我的问题...

这是我的主要内容

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

这是错误:

错误:未在此范围内声明“ Map”错误:预期为“;”,在“ mainMap”之前错误:未在此范围内声明“ mainMap”错误:未在此范围内声明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

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

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

绝对正确! 您错误地应用了包含防护

您使用了错误的hpp文件中的包含防护:

#ifndef DECODE_H_INCLUDED

map.hpp它来自decode.hpp 它应该是

#ifndef MAP_H_INCLUDED

我认为问题与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;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM