c++cppcms

CppCMS form fail (wrong file for code?)


I'm following the tutorial here 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;
    };

}

Below is 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 %>

The myapp.cpp is:

#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;
    }
}

When run cppcms_tmpl_cc my_skin.tmpl -o my_skin.cpp, everything looks fine.

But when run g++ myapp.cpp my_skin.cpp -o myapp -lcppcms -lbooster, I got below error:

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

I'm not sure what file the constructor info_form() should be placed in. Looks strange in content.h.


Solution

  • Where is closing bracket corresponding to struct info_form : public cppcms::form { in content.h? Maybe error is there?