2013-03-24 44 views
0

http://cppcms.com/wikipp/en/page/cppcms_1x_tut_url_mappingCppCMS URL調度和映射失敗。不完整的代碼?

重寫後,hello.cpp代碼如下:

#include <cppcms/application.h> 
#include <cppcms/service.h> 
#include <cppcms/http_response.h> 
#include <cppcms/url_dispatcher.h> 
#include <cppcms/url_mapper.h> 
#include <cppcms/applications_pool.h> 
#include <iostream> 
#include <stdlib.h> 

hello(cppcms::service &srv) : 
    cppcms::application(srv) 
{ 
    dispatcher().assign("/number/(\\d+)",&hello::number,this,1); 
    mapper().assign("number","/number/{1}"); 

    dispatcher().assign("/smile",&hello::smile,this); 
    mapper().assign("smile","/smile"); 

    dispatcher().assign("",&hello::welcome,this); 
    mapper().assign(""); 

    mapper().root("/hello"); 
} 


void number(std::string num) 
{ 
    int no = atoi(num.c_str()); 
    response().out() << "The number is " << no << "<br/>\n"; 
    response().out() << "<a href='" << url("/") << "'>Go back</a>"; 
} 

void smile() 
{ 
    response().out() << ":-) <br/>\n"; 
    response().out() << "<a href='" << url("/") << "'>Go back</a>"; 
} 

void welcome() 
{ 
    response().out() << 
     "<h1> Wellcome To Page with links </h1>\n" 
     "<a href='" << url("/number",1) << "'>1</a><br>\n" 
     "<a href='" << url("/number",15) << "'>15</a><br>\n" 
     "<a href='" << url("/smile") << "' >:-)</a><br>\n"; 
} 

但現在,它編譯引發錯誤:

hello.cpp:10:27: error: ISO C++ forbids declaration of ‘hello’ with no type [-fpermissive] 
hello.cpp: In function ‘int hello(cppcms::service&)’: 
hello.cpp:11:5: error: only constructors take member initializers 
hello.cpp:13:16: error: ‘dispatcher’ was not declared in this scope 
hello.cpp:13:43: error: ‘hello’ is not a class or namespace 
hello.cpp:13:57: error: invalid use of ‘this’ in non-member function 
hello.cpp:14:12: error: ‘mapper’ was not declared in this scope 
hello.cpp:16:35: error: ‘hello’ is not a class or namespace 
hello.cpp:16:48: error: invalid use of ‘this’ in non-member function 
hello.cpp:19:29: error: ‘hello’ is not a class or namespace 
hello.cpp:19:44: error: invalid use of ‘this’ in non-member function 
hello.cpp: In function ‘void number(std::string)’: 
hello.cpp:29:14: error: ‘response’ was not declared in this scope 
hello.cpp:29:14: note: suggested alternative: 
/usr/local/include/cppcms/http_response.h:31:19: note: ‘cppcms::http::response’ 
hello.cpp:30:47: error: ‘url’ was not declared in this scope 
hello.cpp: In function ‘void smile()’: 
hello.cpp:35:14: error: ‘response’ was not declared in this scope 
hello.cpp:35:14: note: suggested alternative: 
/usr/local/include/cppcms/http_response.h:31:19: note: ‘cppcms::http::response’ 
hello.cpp:36:47: error: ‘url’ was not declared in this scope 
hello.cpp: In function ‘void welcome()’: 
hello.cpp:41:14: error: ‘response’ was not declared in this scope 
hello.cpp:41:14: note: suggested alternative: 
/usr/local/include/cppcms/http_response.h:31:19: note: ‘cppcms::http::response’ 
hello.cpp:43:39: error: ‘url’ was not declared in this scope 

我認爲新hello.cpp代碼是不完整的(和不知道完整的代碼),但因爲它說「重寫」,我只是刪除舊的代碼,並創建一個新的代碼。

我已經嘗試與老Hello World結合,但沒有運氣。

回答

2

在鏈接的示例中,構造函數在類內部(頭部中)定義。如果在.cpp文件中定義它,則必須將類名稱添加到構造函數和成員函數中。

hello::hello(cppcms::service &srv) : 
    cppcms::application(srv) 
{ 

... 

void hello::number(std::string num) 
{ 

否則,編譯器認爲你正在添加一些無關的自由函數。

+0

仍然會產生很多錯誤 – apasajja 2013-03-24 04:35:46

+0

謝謝。現在我明白這意味着什麼 – apasajja 2013-03-24 05:33:50

+0

@Bo Persson你能解釋一下這點嗎? – pregmatch 2013-10-08 01:27:27