다음과 같은 멤버 변수로 정렬되지 않은 맵이있는 프로그램이 있습니다. initialize_resource_map
을 추가 한 멤버 함수에서 맵을 초기화하고 싶습니다.멤버 함수에서 순서가 지정되지 않은 맵 초기화
#include <unordered_map>
#include <thread>
#include <functional>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <fstream>
#include <vector>
template < class socket_type > class MyClass {
std::unordered_map < std::string, std::unordered_map < std::string,
std::function < void (std::shared_ptr < typename MyClass <socket_type>::Response >,
std::shared_ptr < typename MyClass <socket_type>::Request >) > >>resource;
class Request: public std::ostream
{
};
class Response
{
};
void initialize_resource_map(MyClass server, std::string pattern,
std::function <void (std::shared_ptr < typename MyClass <socket_type>::Response> response_func,
std::shared_ptr < typename MyClass <socket_type>::Request> request_ptr))
{
server.resource[pattern]["GET"] =
[&server] (response_func, request_ptr)
{
std::string number = request_ptr->path_match[1];
*response_func << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
}
};
};
int main()
{
return 0;
}
다음 명령을 사용하여 컴파일 중입니다.
g++ -std=c++11 test.cpp -o test
다음과 같은 오류가 발생합니다.
test.cpp:29:106: error: template argument 1 is invalid
std::shared_ptr < typename MyClass <socket_type>::Request> request_ptr))
^
test.cpp:29:106: error: template argument 1 is invalid
test.cpp:29:106: error: template argument 1 is invalid
test.cpp:29:106: error: template argument 1 is invalid
test.cpp:29:106: error: template argument 1 is invalid
test.cpp:28:39: error: ‘std::function’ is not a type
std::function <void (std::shared_ptr < typename MyClass <socket_type>::Response> response_func,
^
test.cpp:28:48: error: expected ‘,’ or ‘...’ before ‘<’ token
std::function <void (std::shared_ptr < typename MyClass <socket_type>::Response> response_func,
^
test.cpp: In member function ‘void MyClass<socket_type>::initialize_resource_map(MyClass<socket_type>, std::string, int)’:
test.cpp:32:36: error: ‘response_func’ has not been declared
[&server] (response_func, request_ptr)
^
test.cpp:32:51: error: ‘request_ptr’ has not been declared
[&server] (response_func, request_ptr)
^
test.cpp: In lambda function:
test.cpp:34:46: error: ‘request_ptr’ was not declared in this scope
std::string number = request_ptr->path_match[1];
^
test.cpp:35:26: error: ‘response_func’ was not declared in this scope
*response_func << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
^
test.cpp: In member function ‘void MyClass<socket_type>::initialize_resource_map(MyClass<socket_type>, std::string, int)’:
test.cpp:37:5: error: expected ‘;’ before ‘}’ token
};
저는 템플릿 프로그래밍을 처음 사용합니다. 내가 도대체 뭘 잘못하고있는 겁니까? 이것은 멤버 함수로 변환하려고하는 정렬되지 않은 맵을 초기화하는 원래 코드입니다.
std::shared_ptr < typename MyClass <socket_type>::Request> request_ptr))
이것은
std::shared_ptr < typename MyClass <socket_type>::Request> request_ptr)>)
내가 공개 일치
emacs
의 자동 강조의 큰 팬이 있어야한다 : 경우 일부 문장 부호가이 라인에없는
int main()
{
typedef Server <HTTP> HttpServer;
HttpServer server(8080, 1);
server.resource["^/match/([0-9]+)$"]["GET"] =
[&server] (std::shared_ptr <HttpServer::Response> response, std::shared_ptr <HttpServer::Request> request) {
std::string number = request->path_match[1];
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
};
return 0;
}
일부 typedef를 사용할 수 있습니다. 아마 당신의 실수도 쉽게 찾아 낼 수있을 것입니다. 당신이 의미하는 이름 공간에 대한 –
typedefs? – liv2hak