Re: [Cppcms-users] Compile error: traits is not a template
Brought to you by:
artyom-beilis
|
From: Artyom B. <art...@ya...> - 2012-09-01 13:15:33
|
You hadn't read my code correctly
cppcms::json::array &a=v.at("values",cppcms::json::array());
a is a reference and you fill it.
Anyway read the cppcms::json::value reference documentation for further details
Artyom Beilis
--------------
CppCMS - C++ Web Framework: http://cppcms.com/
CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/
>________________________________
> From: 陈抒 <csf...@gm...>
>To: Artyom Beilis <art...@ya...>; cpp...@li...
>Sent: Saturday, September 1, 2012 3:32 PM
>Subject: Re: [Cppcms-users] Compile error: traits is not a template
>
>
>I changed your codes a bit.
>namespace cppcms {
> namespace json {
>
>
> template<>
> struct traits<kaimei::groups> {
>
>
> static void set(value &v, kaimei::groups const& in) {
>v.set("size", in.size());
>cppcms::json::array a;
>a.resize(in.values_.size());
>for(size_t i = 0; i < in.values_.size(); i++) {
> a[i]=*in.values_[i];
>}
>v.set("values", a);
> }
> };
>
>
> }
>}
>
>
>Two issues:
>1. This way is not efficient, a lot of deep copy would occur when copying vector from array.
>My original way is very simple, create a stringstream object, groups object converts all member variables into string, append them to stringstream. It does not looks elegant, but faster.
>2. It doesn't work. I got an exception:
>cppcms::json::bad_cast: error converting from object
>
>
>陈抒
>Best regards
>http://blog.csdn.net/sheismylife
>
>
>
>On Sat, Sep 1, 2012 at 5:41 PM, Artyom Beilis <art...@ya...> wrote:
>
>Because shared_ptr<group> is not specialized for traits...
>>
>>You need manually create an array and sets its memebers.i.e
>>
>>Something like that:
>>
>>cppcms::json::array &a=v.at("values",cppcms::json::array());
>>a.resize(in.values_.size());
>>for(size_t i = 0;i<in.values_.size();i++)
>> a[i]=*in.values_[i];
>>
>>
>>
>>
>>
>>Artyom Beilis
>>--------------
>>CppCMS - C++ Web Framework: http://cppcms.com/
>>CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/
>>
>>
>>
>>>________________________________
>>> From: 陈抒 <csf...@gm...>
>>>To: Artyom Beilis <art...@ya...>; cpp...@li...
>>>Sent: Saturday, September 1, 2012 10:05 AM
>>>Subject: Re: [Cppcms-users] Compile error: traits is not a template
>>
>>>
>>>
>>>Thank you. But I see new error.
>>>groups.h file:
>>>
>>>
>>>#ifndef __GROUPS_H
>>>#define __GROUPS_H
>>>
>>>
>>>#include <vector>
>>>#include <boost/shared_ptr.hpp>
>>>#include "group.h"
>>>
>>>
>>>namespace kaimei{
>>>
>>>
>>> class groups{
>>> public:
>>> typedef std::vector<boost::shared_ptr<group> >::iterator iterator;
>>> typedef std::vector<boost::shared_ptr<group> >::const_iterator const_iterator;
>>>
>>>
>>> void add(boost::shared_ptr<group> g);
>>>
>>>
>>> bool empty() const;
>>>
>>>
>>> unsigned int size() const;
>>>
>>>
>>> std::string json_string() const;
>>>
>>> friend class cppcms::json::traits<kaimei::groups>;
>>>
>>>
>>> private:
>>> std::vector<boost::shared_ptr<kaimei::group> > values_;
>>> };
>>>
>>>
>>>}
>>>
>>>
>>>namespace cppcms {
>>> namespace json {
>>>
>>>
>>> template<>
>>> struct traits<kaimei::groups> {
>>>
>>>
>>> static void set(value &v, kaimei::groups const& in) {
>>>v.set("size", in.size());
>>>v.set("values", in.values_);
>>> }
>>> };
>>>
>>>
>>> }
>>>}
>>>
>>>
>>>#endif
>>>
>>>
>>>
>>>
>>>error message:
>>> 3%] Generating ../../src/view/welcome.cpp, ../../src/view/home.cpp, ../../src/view/gprs_message.cpp, ../../src/view/gprs_manager.cpp, ../../src/view/gprs_password.cpp, ../../src/view/gprs_log.cpp, ../../src/view/register_client.cpp, ../../src/view/gprsConfig.cpp, ../../src/view/login.cpp, ../../src/view/group_management.cpp
>>>[ 6%] Building CXX object bin/CMakeFiles/cppweb.dir/groups.cpp.o
>>>In file included from /home/chenshu/work/kaimei/cppweb/include/group.h:5:0,
>>> from /home/chenshu/work/kaimei/cppweb/include/groups.h:6,
>>> from /home/chenshu/work/kaimei/cppweb/src/groups.cpp:1:
>>>/usr/include/cppcms/json.h: In member function ‘void cppcms::json::value::set_value(const T&) [with T = boost::shared_ptr<kaimei::group>]’:
>>>/usr/include/cppcms/json.h:604:5: instantiated from ‘static void cppcms::json::traits<std::vector<T> >::set(cppcms::json::value&, const std::vector<T>&) [with T = boost::shared_ptr<kaimei::group>]’
>>>/usr/include/cppcms/json.h:247:4: instantiated from ‘void cppcms::json::value::set_value(const T&) [with T = std::vector<boost::shared_ptr<kaimei::group> >]’
>>>/usr/include/cppcms/json.h:316:4: instantiated from ‘cppcms::json::value::value(const T&) [with T = std::vector<boost::shared_ptr<kaimei::group> >]’
>>>/usr/include/cppcms/json.h:352:4: instantiated from ‘void cppcms::json::value::set(const char*, const T&) [with T = std::vector<boost::shared_ptr<kaimei::group> >]’
>>>/home/chenshu/work/kaimei/cppweb/include/groups.h:39:28: instantiated from here
>>>/usr/include/cppcms/json.h:247:4: error: incomplete type ‘cppcms::json::traits<boost::shared_ptr<kaimei::group> >’ used in nested name specifier
>>>make[2]: *** [bin/CMakeFiles/cppweb.dir/groups.cpp.o] Error 1
>>>make[1]: *** [bin/CMakeFiles/cppweb.dir/all] Error 2
>>>make: *** [all] Error 2
>>>
>>>
>>>陈抒
>>>Best regards
>>>http://blog.csdn.net/sheismylife
>>>
>>>
>>>
>>>On Sat, Sep 1, 2012 at 2:34 PM, Artyom Beilis <art...@ya...> wrote:
>>>
>>>>
>>>>
>>>>>But I got error when compiling another class. It has group.h and group.cpp file.
>>>>>
>>>>>
>>>>>#ifndef __GROUP_H
>>>>>#define __GROUP_H
>>>>>
>>>>>
>>>>>#include <string>
>>>>>
>>>>>
>>>>>namespace kaimei{
>>>>>...
>>>>
>>>>}
>>>>>
>>>>>
>>>>>namespace cppcms {
>>>>> namespace json {
>>>>> template<>
>>>>> struct traits<kaimei::group> {
>>>>>...
>>>>> }
>>>>> }
>>>>>}
>>>>>
>>>>>
>>>>>
>>>>> [snip]
>>>>
>>>>>
>>>>>I am not good at template programming. Why did this happen?
>>>>>
>>>>
>>>>You can't specialize undefined struct - you need to include <cppcms/json.h>
>>>>header. It provides the original traits struct and than you specialize it for
>>>>your type of class
>>>>
>>>>
>>>>
>>>>>
>>>>>陈抒
>>>>>Best regards
>>>>>http://blog.csdn.net/sheismylife
>>>>>
>>>>
>>>>Regards,
>>>>Artyom
>>>>
>>>>
>>>>------------------------------------------------------------------------------
>>>>Live Security Virtual Conference
>>>>Exclusive live event will cover all the ways today's security and
>>>>threat landscape has changed and how IT managers can respond. Discussions
>>>>will include endpoint security, mobile security and the latest in malware
>>>>threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>>>>_______________________________________________
>>>>Cppcms-users mailing list
>>>>Cpp...@li...
>>>>https://lists.sourceforge.net/lists/listinfo/cppcms-users
>>>>
>>>
>>>------------------------------------------------------------------------------
>>>Live Security Virtual Conference
>>>Exclusive live event will cover all the ways today's security and
>>>threat landscape has changed and how IT managers can respond. Discussions
>>>will include endpoint security, mobile security and the latest in malware
>>>threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>>>_______________________________________________
>>>Cppcms-users mailing list
>>>Cpp...@li...
>>>https://lists.sourceforge.net/lists/listinfo/cppcms-users
>>>
>>>
>>>
>>
>>------------------------------------------------------------------------------
>>Live Security Virtual Conference
>>Exclusive live event will cover all the ways today's security and
>>threat landscape has changed and how IT managers can respond. Discussions
>>will include endpoint security, mobile security and the latest in malware
>>threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>>_______________________________________________
>>Cppcms-users mailing list
>>Cpp...@li...
>>https://lists.sourceforge.net/lists/listinfo/cppcms-users
>>
>
>------------------------------------------------------------------------------
>Live Security Virtual Conference
>Exclusive live event will cover all the ways today's security and
>threat landscape has changed and how IT managers can respond. Discussions
>will include endpoint security, mobile security and the latest in malware
>threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>_______________________________________________
>Cppcms-users mailing list
>Cpp...@li...
>https://lists.sourceforge.net/lists/listinfo/cppcms-users
>
>
> |