gpt4 book ai didi

c++ - CppCMS URL 调度和映射失败。代码不完整?

转载 作者:行者123 更新时间:2023-11-28 03:20:26 24 4
gpt4 key购买 nike

来自 http://cppcms.com/wikipp/en/page/cppcms_1x_tut_url_mapping

改写后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 结合使用,但没有成功。

最佳答案

在链接示例中,构造函数在类内部(在 header 中)定义。如果在 .cpp 文件中定义它,则必须将类名添加到构造函数和成员函数中。

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

...

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

否则编译器认为你添加了一些不相关的自由函数。

关于c++ - CppCMS URL 调度和映射失败。代码不完整?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15594898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com