2011-04-18 33 views
0

我創建了一個ServerService名稱空間,用於包含類名稱server_datetime。 Server_datetime類作爲Boost示例的教程,但是我通過使用模板參數改進了server_datetime類,以將io_service(boost :: asio :: io_service)和endpoint(tcp :: endpoint(tcp :: v4(),SIZE_DATA))對象插入模板。我遵循下面的例子:編譯器編譯「io_service_」變量顯示爲:不能出現在常量表達式中

using boost::asio::ip::tcp; 
namespace ServerService{ 
template<typename Service, typename Endpoint> 
class server_datetime { 
public: 
    server_datetime(){ 
     acceptor_(service_, endpoint_); 
     for(;;) 
     { 
      tcp::socket socket(Service); 
      acceptor_.accept(socket); 
      std::string message = make_daytime_string; 

      boost::system::error_code ignored_error; 
      boost::asio::write(socket, boost::asio::buffer(message),boost::asio::transfer_all(), ignored_error); 
     } 
    } 
    std::string make_daytime_string(){ 
     std::time_t now = std::time(0); 
     return std::ctime(&now); 
    } 
    virtual ~server_datetime(); 
private: 
    tcp::acceptor acceptor_; 
    Service service_; 
    Endpoint endpoint_; 
}; 
} 

main函數調用server_datetime類通過遵循:

#include "server_datetime.hpp" 
using namespace std; 
using boost::asio::ip::tcp; 
int main() { 
    const boost::asio::io_service io_service_; 
    const int SIZE_DATA = 13; 
    ServerService::server_datetime<io_service_, tcp::endpoint(tcp::v4(),SIZE_DATA) > server; 
    cout << "" << endl; // prints 
    return 0; 
} 

由編譯器編譯的主要功能,編譯器顯示誤差後:

..\src\connectk.cpp: In function 'int main()': 
..\src\connectk.cpp:10: error: 'io_service_' cannot appear in a constant-expression 
..\src\connectk.cpp:10: error: 'boost::asio::ip::tcp::v4()' cannot appear in a constant-expression 
..\src\connectk.cpp:10: error: a function call cannot appear in a constant-expression 
..\src\connectk.cpp:10: error: template argument 1 is invalid 
..\src\connectk.cpp:10: error: template argument 2 is invalid 
..\src\connectk.cpp:10: error: invalid type in declaration before ';' token 

回答

4
std::string message = make_daytime_string; 

忘記了(),應該是:

std::string message = make_daytime_string(); 
2

的server_datetime模板需要類型名稱(它在第一個源代碼的頂部說得很對),但是您需要提供值。

也許你不應該在main中創建io_service_,而是讓服務器這樣做?

2
模板

參數對於在編譯時間指定類型和恆定值,而不是用於注入在運行時對象;這就是普通的函數/構造函數參數。因此,在這種情況下,如果您將服務和端點提供給服務器,請將它們作爲參數傳遞給構造函數。

代碼中還有一些其他錯誤;這裏有一些更正(雖然可能還有問題,我可能已經介紹了一些我自己):

namespace ServerService{ 

// Put 'using' declarations inside the namespace, 
// to avoid polluting the global namespace 
using boost::asio::ip::tcp; 
using boost::asio::io_service; 

// Not a template - pass runtime objects as constructor arguments 
class server_datetime { 
public: 
    server_datetime(io_service & service, tcp::endpoint const & endpoint) : 
     // Initialise members in an initialiser list 
     acceptor_(service, endpoint), 
     service_(service) 
    {} 

    // Put the main loop in a separate function; it's rather odd 
    // to have a constructor that doesn't return. 
    void run(){ 
     for(;;) 
     { 
      // Argument must be the object service_, not the type Service 
      tcp::socket socket(service_); 
      acceptor_.accept(socket); 
      std::string message = make_daytime_string(); // missing parentheses 

      boost::system::error_code ignored_error; 
      boost::asio::write(socket, boost::asio::buffer(message),boost::asio::transfer_all(), ignored_error); 
     } 
    } 
    std::string make_daytime_string(){ 
     std::time_t now = std::time(0); 
     return std::ctime(&now); 
    } 
    // No need for a virtual destructor - this class is not polymorphic 
private: 
    boost::asio::io_service & service_; // must be a reference - io_service is not copyable 
    tcp::acceptor acceptor_; 
    // No need to store the endpoint - it's only used to initialise acceptor_. 
}; 
} 

int main() { 
    using boost::asio::ip::tcp; 

    // can't be const if you want to use it 
    boost::asio::io_service io_service_; 

    // renamed SIZE_DATA and given it the same type as the constructor argument 
    const unsigned short port = 13; 

    ServerService::server_datetime server(io_service_, tcp::endpoint(tcp::v4(),port)); 
    server.run(); 
    // no need to explicitly return zero, unless you want to. 
} 
+0

我可以使用模板參數列表是「類」嗎?如 'template '如果我想注入對象。 [鏈接](http://stackoverflow.com/questions/213121/c-use-class-or-typename-for-template-parameters) – 2011-04-19 09:47:29

+1

@ Chatsiri.rat:no,在模板參數列表中'class'表示與'typename'相同;都聲明一個類型參數。模板在編譯時被實例化,所以你可以注入的唯一對象是編譯時常量。如果你想注入運行時對象,那必須通過函數或構造函數參數來完成。 – 2011-04-19 09:57:26

+0

我將類文件和ServerService命名空間的主要方法分散到頭文件中。爲什麼編譯器顯示無法創建在類文件上實例化的服務器? – 2011-04-21 05:36:13

相關問題