C++ CppCMS教程:静态链接模板错误(控制器问题)

C++ CppCMS教程:静态链接模板错误(控制器问题),c++,cppcms,C++,Cppcms,从 我在hello.cpp的底部放置了以下代码: virtual void main(std::string /*url*/) { content::message c; c.text=">>>Hello<<<"; render("message",c); } 我遗漏了什么吗?错误消息告诉您需要知道的一切 virtual只能在类中使用。您的主方法不在类中 main方法必须返回int。您的正在返回void 您有两个主要方法,一个是mai

我在
hello.cpp
的底部放置了以下代码:

virtual void main(std::string /*url*/)
{
    content::message c;
    c.text=">>>Hello<<<";
    render("message",c);
}

我遗漏了什么吗?

错误消息告诉您需要知道的一切

  • virtual
    只能在类中使用。您的主方法不在类中
  • main
    方法必须返回
    int
    。您的正在返回
    void
  • 您有两个主要方法,一个是
    main(std::string)
    ,另一个是
    main(int,char**)
  • 渲染方法必须在主方法上方有一个函数原型,否则需要移动整个方法
  • 所以这更合适:

    void render(std::string, std::string) // or whatever
    {
       // do something
    }
    
    int main(int argc, char** argv)
    {
       render("string", c);
       return 0;
    }
    

    您的hello.cpp应如下所示:

    #include <cppcms/application.h>
    #include <cppcms/applications_pool.h>
    #include <cppcms/service.h>
    #include <cppcms/http_response.h>
    #include <iostream>
    #include "content.h"
    
    class hello : public cppcms::application {
    public:
        hello(cppcms::service &srv) : cppcms::application(srv) {}
        virtual void main(std::string url);
    };
    
    void hello::main(std::string /*url*/){
        content::message cc;
        cc.text=">>>Hello<<<";
        render("message", cc);
    }
    
    int main(int argc,char ** argv){
        try {
            cppcms::service srv(argc,argv);
            srv.applications_pool().mount(cppcms::applications_factory<hello>());
            srv.run();
        }
        catch(std::exception const &e) {
            std::cerr<<e.what()<<std::endl;
        }
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括“content.h”
    类hello:public cppcms::application{
    公众:
    您好(cppcms::service&srv):cppcms::application(srv){}
    虚拟void main(std::string url);
    };
    void hello::main(std::string/*url*/){
    内容::消息抄送;
    cc.text=“>>>您好
    
    #include <cppcms/application.h>
    #include <cppcms/applications_pool.h>
    #include <cppcms/service.h>
    #include <cppcms/http_response.h>
    #include <iostream>
    #include "content.h"
    
    class hello : public cppcms::application {
    public:
        hello(cppcms::service &srv) : cppcms::application(srv) {}
        virtual void main(std::string url);
    };
    
    void hello::main(std::string /*url*/){
        content::message cc;
        cc.text=">>>Hello<<<";
        render("message", cc);
    }
    
    int main(int argc,char ** argv){
        try {
            cppcms::service srv(argc,argv);
            srv.applications_pool().mount(cppcms::applications_factory<hello>());
            srv.run();
        }
        catch(std::exception const &e) {
            std::cerr<<e.what()<<std::endl;
        }
    }