2013-03-16 2 views
1

내 코드에서 다음 오류가 발생합니다. 'socketfd'가 client.hpp에 선언되어 있고 내부에서 생성자로 사용 된 이유가 확실하지 않습니다. client.cpp하지만 나중에 사용하려고하면 오류가 발생합니다.C 11 오류 : 'X'가이 범위에서 선언되지 않았습니다.

터미널 출력 :

g++ client.cpp -o client.o -pthread -c -std=c++11 
client.cpp: In function ‘void sendMessage(std::string)’: 
client.cpp:37:23: error: ‘socketfd’ was not declared in this scope 

client.hpp

#include <iostream> 
#include <cstring> 
#include <sys/socket.h> 
#include <netdb.h> 

class Client { 

    public: 
     Client(); 
     ~Client(); 
     void sendMessage(std::string); 

    private: 
     int status, socketfd; 
     struct addrinfo host_info; 
     struct addrinfo *host_info_list; 

}; 

client.cpp

#include "client.hpp" 


Client::Client() { 
    memset(&host_info, 0, sizeof host_info); 
    std::cout << "Setting up the structs..." << std::endl; 
    host_info.ai_family = AF_UNSPEC; 
    host_info.ai_socktype = SOCK_STREAM; 
    status = getaddrinfo("192.168.1.3", "8888", &host_info, &host_info_list); 
    if (status != 0) { 
     std::cout << "getaddrinfo error " << gai_strerror(status); 
    } 

    std:: cout << "Creating a socket..." << std::endl; 
    socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype, host_info_list->ai_protocol); 
    if (socketfd == -1) { 
     std::cout << "Socket Errror "; 
    } 
    std::cout << "Connecting..." << std::endl; 
    status = connect(socketfd, host_info_list->ai_addr, host_info_list->ai_addrlen); 
    if (status == -1) { 
     std::cout << "Connect Error" << std::endl; 
    } 
    std::cout << "Successfully Connected" << std::endl; 
} 

Client::~Client() { 

} 

void sendMessage(std::string msg) { 
    std::cout << "Sending Message: " << msg << std::endl; 
    int len; 
    ssize_t bytes_sent; 
    len = strlen(msg.c_str()); 
    bytes_sent = send(socketfd, msg.c_str(), len, 0); 
} 

이 처음 C의 일부 ++ 나는 짓을했는지, 그리고 난 왜 내가이 오류가 발생하는지 혼란스러워.

void Client::sendMessage(std::string msg) { 
    ^^^^^^^^ 

답변

5

당신은 Client::sendMessage() 전에 놓치고있어. 사용해보기 - :

void Client::sendMessage(std::string msg) { 
4

sendMessage의 기능 서명이 멤버 함수의 그것되지 않습니다 :

관련 문제