我在这里学习教程http://cppcms.com/wikipp/en/page/cppcms_1x_forms
content.h
:
#include <cppcms/view.h>
#include <cppcms/form.h>
namespace content {
struct info_form : public cppcms::form {
cppcms::widgets::text name;
cppcms::widgets::radio sex;
cppcms::widgets::select marital;
cppcms::widgets::numeric<double> age;
cppcms::widgets::submit submit;
};
info_form()
{
name.message("Your Name");
sex.message("Sex");
marital.message("Marital Status");
age.message("Your Age");
submit.value("Send");
add(name);
add(sex);
add(marital);
add(age);
add(submit);
sex.add("Male","male");
sex.add("Female","female");
marital.add("Single","single");
marital.add("Married","married");
marital.add("Divorced","divorced");
name.non_empty();
age.range(0,120);
};
struct message : public cppcms::base_content {
std::string name,state,sex;
double age;
info_form info;
};
}
下面是my_skin.tmpl
:
<% c++ #include "content.h" %>
<% skin myskin %>
<% if not empty name %>
<h1>Hello <%= name %></h1>
<p>You are <%= sex %>, <%= state %></p>
<p>Your age is <%= age %></p>
<% else %>
<h1>Input your details</h1>
<% end %>
<form method="post" action="" ><% csrf %>
<% form as_p info %>
</form>
<% end skin %>
是myapp.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>
#include "content.h"
class myapp : public cppcms::application {
public:
myapp(cppcms::service &srv) : cppcms::application(srv)
{
}
};
void myapp::main(std::string /*url*/)
{
content::message c;
if(request().request_method()=="POST") {
c.info.load(context());
if(c.info.validate()) {
c.name=c.info.name.value();
c.sex=c.info.sex.selected_id();
c.state=c.info.marital.selected_id();
c.age=c.info.age.value();
c.info.clear();
}
render("message",c);
}
virtual bool validate()
{
if(!form::validate())
return false;
if(marital.selected_id()!="single"
&& age.value()<18)
{
marital.valid(false);
return false;
}
return true;
}
int main(int argc,char ** argv)
{
try {
cppcms::service app(argc,argv);
app.applications_pool().mount(cppcms::applications_factory<myapp>());
app.run();
}
catch(std::exception const &e) {
std::cerr<<e.what()<<std::endl;
}
}
运行时cppcms_tmpl_cc my_skin.tmpl -o my_skin.cpp
,一切看起来都很好。
但是当运行时g++ myapp.cpp my_skin.cpp -o myapp -lcppcms -lbooster
,我得到以下错误:
In file included from myapp.cpp:9:0:
content.h:13:15: error: expected unqualified-id before ‘)’ token
myapp.cpp:18:37: error: no ‘void myapp::main(std::string)’ member function declared in class ‘myapp’
In file included from my_skin.tmpl:1:0:
content.h:13:15: error: expected unqualified-id before ‘)’ token
my_skin.tmpl:4:2: error: expected unqualified-id before ‘if’
my_skin.tmpl:8:3: error: expected unqualified-id before ‘else’
my_skin.tmpl:12:8: error: expected constructor, destructor, or type conversion before ‘<<’ token
my_skin.tmpl:13:2: error: expected unqualified-id before ‘{’ token
我不确定构造函数info_form()
应该放在哪个文件中。看起来很奇怪content.h
。