Учебное пособие по 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

Я что то пропустил

0

Решение

Сообщения об ошибках рассказывают вам все, что вам нужно знать.

  1. virtual может использоваться только в классе. Ваш основной метод не в классе.
  2. main метод должен вернуть int, Твое возвращается void,
  3. У вас есть два основных метода, один из которых 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;
}
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;
}
}
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector