我在这里按照教程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
看起来很奇怪。
答案 0 :(得分:0)
struct info_form : public cppcms::form {
中与content.h
对应的右括号在哪里?也许有错误?