0

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

私は何かを逃しましたか

4

2 に答える 2

0

エラーメッセージはあなたが知る必要があるすべてをあなたに伝えています。

  1. virtualクラスでのみ使用できます。あなたのメインメソッドはクラスにありません。
  2. mainメソッドはを返す必要がありますint。あなたが戻ってきvoidました。
  3. あなたには2つの主な方法がmain(std::string)あります。main(int, char**)
  4. レンダリングメソッドには、メインメソッドの上に関数プロトタイプが必要です。そうでない場合は、メソッド全体を移動する必要があります。

したがって、これはより適切です。

void render(std::string, std::string) // or whatever
{
   // do something
}

int main(int argc, char** argv)
{
   render("string", c);
   return 0;
}
于 2013-03-21T17:57:32.040 に答える
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;
    }
}
于 2014-07-03T16:35:15.480 に答える