【问题标题】:CppCMS form fail (wrong file for code?)CppCMS 表单失败(代码文件错误?)
【发布时间】:2013-04-03 14:18:45
【问题描述】:

我正在关注这里的教程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 中看起来很奇怪。

【问题讨论】:

    标签: c++ cppcms


    【解决方案1】:

    content.h 中的 struct info_form : public cppcms::form { 对应的右括号在哪里?也许有错误?

    【讨论】:

    • info_form() - 这是什么?如果它是你提到的构造函数,它应该类似于void info_form::info_form()
    • 和struct info_form声明应该包含void info_form()的声明
    • 如果只有add(name);add(sex);等三个是构造函数,而其他三个是声明,那怎么关联呢?我试过没有成功
    猜你喜欢
    • 2017-07-20
    • 2016-08-23
    • 2015-11-10
    • 1970-01-01
    • 2014-01-25
    • 2013-06-24
    • 2011-08-01
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多