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

时间:2013-03-21 17:51:29

标签: c++ cppcms

来自http://cppcms.com/wikipp/en/page/cppcms_1x_tut_hello_templates#The.controller

我在hello.cpp

的底部放置了以下代码
virtual void main(std::string /*url*/)
{
    content::message c;
    c.text=">>>Hello<<<";
    render("message",c);
}

运行g++ hello.cpp my_skin.cpp -o hello -lcppcms -lbooster时出错:

hello.cpp:44:38: error: ‘virtual’ outside class declaration
hello.cpp:44:38: error: ‘::main’ must return ‘int’
hello.cpp:44:14: warning: first argument of ‘int main(std::string)’ should be ‘int’ [-Wmain]
hello.cpp:44:14: warning: ‘int main(std::string)’ takes only zero or two arguments [-Wmain]
hello.cpp: In function ‘int main(std::string)’:
hello.cpp:44:38: error: declaration of C function ‘int main(std::string)’ conflicts with
hello.cpp:27:5: error: previous declaration ‘int main(int, char**)’ here
hello.cpp: In function ‘int main(std::string)’:
hello.cpp:48:23: error: ‘render’ was not declared in this scope

我错过了什么

2 个答案:

答案 0 :(得分:0)

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

  1. virtual只能在课堂上使用。你的主要方法不在课堂上。
  2. main方法必须返回int。你的回归void
  3. 您有两种主要方法,一种是main(std::string),另一种是main(int, char**)
  4. 你的渲染方法必须在main方法之上有一个函数原型,否则整个方法都需要我移动。
  5. 所以这更合适:

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

答案 1 :(得分: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;
    }
}