Am beginner in move semantics and I'm confused by compilation result. When I use qt 5.5.1 all are okey, but when I use qt 4.8.7 I have build errors. I'm trying to build the following code:
struct TResource
{
int _resource_id = 18;
};
typedef std::vector<std::unique_ptr<TResource>> TResources;
struct TChannel
{
QString _title;
TResources _ress;
};
typedef std::vector<TChannel> TChanneles;
int main(int , char **)
{
TChannel channel;
TChanneles chnls;
//Creating some channel resorces and place it to the vector
// .......
chnls.push_back(std::move(channel));
return 0;
}
The error messages are following:
g++ -c -m64 -pipe -std=c++0x -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I. -o main.o main.cpp
In file included from /usr/include/c++/5/bits/stl_tempbuf.h:60:0,
from /usr/include/c++/5/bits/stl_algo.h:62,
from /usr/include/c++/5/algorithm:62,
from /usr/include/qt4/QtCore/qglobal.h:68,
from /usr/include/qt4/QtCore/qnamespace.h:45,
from /usr/include/qt4/QtCore/qobjectdefs.h:45,
from /usr/include/qt4/QtCore/qobject.h:47,
from /usr/include/qt4/QtCore/qcoreapplication.h:45,
from /usr/include/qt4/QtCore/QCoreApplication:1,
from main.cpp:1:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<TResource>; _Args = {const std::unique_ptr<TResource, std::default_delete<TResource> >&}]’:
/usr/include/c++/5/bits/stl_uninitialized.h:75:18: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<TResource>*, std::vector<std::unique_ptr<TResource> > >; _ForwardIterator = std::unique_ptr<TResource>*; bool _TrivialValueTypes = false]’
/usr/include/c++/5/bits/stl_uninitialized.h:126:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<TResource>*, std::vector<std::unique_ptr<TResource> > >; _ForwardIterator = std::unique_ptr<TResource>*]’
/usr/include/c++/5/bits/stl_uninitialized.h:281:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<TResource>*, std::vector<std::unique_ptr<TResource> > >; _ForwardIterator = std::unique_ptr<TResource>*; _Tp = std::unique_ptr<TResource>]’
/usr/include/c++/5/bits/stl_vector.h:322:31: required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<TResource>; _Alloc = std::allocator<std::unique_ptr<TResource> >]’
main.cpp:11:8: required from ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = TChannel; _Args = {TChannel&}]’
/usr/include/c++/5/bits/stl_uninitialized.h:75:18: [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/5/bits/stl_uninitialized.h:281:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = TChannel*; _ForwardIterator = TChannel*; _Tp = TChannel]’
/usr/include/c++/5/bits/stl_uninitialized.h:303:2: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = TChannel*; _ForwardIterator = TChannel*; _Allocator = std::allocator<TChannel>]’
/usr/include/c++/5/bits/vector.tcc:422:8: required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {TChannel}; _Tp = TChannel; _Alloc = std::allocator<TChannel>]’
/usr/include/c++/5/bits/vector.tcc:101:23: required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {TChannel}; _Tp = TChannel; _Alloc = std::allocator<TChannel>]’
/usr/include/c++/5/bits/stl_vector.h:932:21: required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = TChannel; _Alloc = std::allocator<TChannel>; std::vector<_Tp, _Alloc>::value_type = TChannel]’
main.cpp:26:39: required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = TResource; _Dp = std::default_delete<TResource>]’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
In file included from /usr/include/c++/5/memory:81:0,
from main.cpp:2:
/usr/include/c++/5/bits/unique_ptr.h:356:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
^
Makefile:203: recipe for target 'main.o' failed
make: *** [main.o] Error 1
It seems that something not supported in qt 4.8.7 and I should take this into account and change my code. But I don't know what change.
Thank you in advance.
I have fixed my issue.
A have added move constructor TChannel struct:
TChannel(TChannel&& that)
{
_title = std::move(that._title);
}
It helped because move constructor was introduced in Qt 5.2.
Related
I'm a newbie using the RViennaCL package, trying to do GPU matrix multiplication.
Trying a simple case I created a cpp file inside the src folder within the package I created (gpuUtils), with this code:
#include "viennacl/ocl/backend.hpp"
#include "viennacl/linalg/prod.hpp"
#include "viennacl/tools/random.hpp"
#include "viennacl/matrix.hpp"
//' PU matrix multiplication
//'
//' #export
// [[Rcpp::export]]
SEXP matMult(){
viennacl::tools::uniform_random_numbers<float> randomNumber;
viennacl::matrix<float> vcl_A(400, 400);
viennacl::matrix<float> vcl_B(400, 400);
for (unsigned int i = 0; i < vcl_A.size1(); ++i)
for (unsigned int j = 0; j < vcl_A.size2(); ++j)
vcl_A(i,j) = randomNumber();
for (unsigned int i = 0; i < vcl_B.size1(); ++i)
for (unsigned int j = 0; j < vcl_B.size2(); ++j)
vcl_B(i,j) = randomNumber();
viennacl::matrix<float> vcl_C = viennacl::linalg::prod(vcl_A, vcl_B);
return Rcpp::wrap(vcl_C);
}
Then I build the package:
$ R CMD build gpuUtils
* checking for file ‘gpuUtils/DESCRIPTION’ ... OK
* preparing ‘gpuUtils’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘gpuUtils_0.0.0.9000.tar.gz’
But when trying to install it I get:
$ R CMD INSTALL gpuUtils_0.0.0.9000.tar.gz
* installing to library ‘/home/usr/R’
* installing *source* package ‘gpuUtils’ ...
** libs
g++ -I/opt/ohpc/pub/libs/gnu7/R/3.4.2/lib64/R/include -DNDEBUG -I/usr/local/cuda-9.0/include/ -I"/home/usr/R/Rcpp/include" -I"/home/usr/R/RViennaCL/include" -I/usr/local/include -fpic -g -O2 -c gpuUtils.cpp -o gpuUtils.o
In file included from /home/usr/R/Rcpp/include/RcppCommon.h:195:0,
from /home/usr/R/Rcpp/include/Rcpp.h:27,
from /home/usr/R/RViennaCL/include/viennacl/ocl/forwards.h:29,
from /home/usr/R/RViennaCL/include/viennacl/ocl/context.hpp:36,
from /home/usr/R/RViennaCL/include/viennacl/ocl/backend.hpp:26,
from gpuUtils.cpp:1:
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h: In instantiation of ‘SEXPREC* Rcpp::internal::wrap_dispatch_unknown_iterable(const T&, Rcpp::traits::false_type) [with T = viennacl::matrix<float, viennacl::row_major>; SEXP = SEXPREC*; Rcpp::traits::false_type = Rcpp::traits::integral_constant<bool, false>]’:
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h:732:43: required from ‘SEXPREC* Rcpp::internal::wrap_dispatch_unknown(const T&, Rcpp::traits::false_type) [with T = viennacl::matrix<float, viennacl::row_major>; SEXP = SEXPREC*; Rcpp::traits::false_type = Rcpp::traits::integral_constant<bool, false>]’
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h:770:41: required from ‘SEXPREC* Rcpp::internal::wrap_dispatch_eigen(const T&, Rcpp::traits::false_type) [with T = viennacl::matrix<float, viennacl::row_major>; SEXP = SEXPREC*; Rcpp::traits::false_type = Rcpp::traits::integral_constant<bool, false>]’
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h:787:39: required from ‘SEXPREC* Rcpp::internal::wrap_dispatch_unknown_importable(const T&, Rcpp::traits::false_type) [with T = viennacl::matrix<float, viennacl::row_major>; SEXP = SEXPREC*; Rcpp::traits::false_type = Rcpp::traits::integral_constant<bool, false>]’
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h:807:52: required from ‘SEXPREC* Rcpp::internal::wrap_dispatch(const T&, Rcpp::traits::wrap_type_unknown_tag) [with T = viennacl::matrix<float, viennacl::row_major>; SEXP = SEXPREC*]’
/home/usr/R/Rcpp/include/Rcpp/internal/wrap_end.h:30:38: required from ‘SEXPREC* Rcpp::wrap(const T&) [with T = viennacl::matrix<float, viennacl::row_major>; SEXP = SEXPREC*]’
gpuUtils.cpp:25:26: required from here
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h:523:17: error: static assertion failed: cannot convert type to SEXP
static_assert(!sizeof(T), "cannot convert type to SEXP");
^~~~~~~~~~~~~
make: *** [gpuUtils.o] Error 1
ERROR: compilation failed for package ‘gpuUtils’
* removing ‘/home/usr/R/gpuUtils’
* restoring previous ‘/home/usr/R/gpuUtils’
Which indicates that viennacl::matrix need its own wrap function to be written.
So I tried to follow this very nice vignette for custom templating as and wrap functions within Rcpp, adapting it to the viennacl::matrix case.
Here's my cpp code:
#include <RcppCommon.h>
#include "viennacl/ocl/backend.hpp"
#include "viennacl/matrix.hpp"
// [[Rcpp::plugins("cpp11")]]
// Provide Forward Declarations
namespace Rcpp {
namespace traits{
// Setup non-intrusive extension via template specialization for
// 'viennacl' class
// Support for wrap
template <typename T> SEXP wrap(const viennacl::matrix<T> & obj);
// Support for as<T>
template <typename T> class Exporter< viennacl::matrix<T> >;
}
}
#include <Rcpp.h>
// Define template specializations for as<> and wrap
namespace Rcpp {
namespace traits{
// Defined wrap case
template <typename T> SEXP wrap(const viennacl::matrix<T> & obj){
const int RTYPE = Rcpp::traits::r_sexptype_traits<T>::rtype ;
return Rcpp::Matrix< RTYPE >(obj.begin(), obj.end());
};
// Defined as< > case
template<typename T> class Exporter< viennacl::matrix<T> > {
typedef typename viennacl::matrix<T> OUT ;
// Convert the type to a valid rtype.
const static int RTYPE = Rcpp::traits::r_sexptype_traits< T >::rtype ;
Rcpp::Matrix<RTYPE> mat;
public:
Exporter(SEXP x) : mat(x) {
if (TYPEOF(x) != RTYPE)
throw std::invalid_argument("Wrong R type for mapped 2D array");
}
OUT get() {
// Need to figure out a way to perhaps do a pointer pass?
OUT x(mat.size());
std::copy(mat.begin(), mat.end(), mat.begin());
return x;
}
};
}
}
//' viennacl matrix templating
//'
//' #export
// [[Rcpp::export]]
void matTemplate(Rcpp::NumericMatrix in_mat){
viennacl::matrix<double> viennacl_mat = Rcpp::as< viennacl::matrix<double> >(in_mat);
Rcpp::NumericMatrix out_mat = Rcpp::wrap(viennacl_mat);
}
Builds fine:
$ R CMD build gpuUtils
* checking for file ‘gpuUtils/DESCRIPTION’ ... OK
* preparing ‘gpuUtils’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘gpuUtils_0.0.0.9000.tar.gz’
But still getting the same error:
$ R CMD INSTALL gpuUtils_0.0.0.9000.tar.gz
* installing to library ‘/home/usr/R’
* installing *source* package ‘gpuUtils’ ...
** libs
g++ -I/opt/ohpc/pub/libs/gnu7/R/3.4.2/lib64/R/include -DNDEBUG -I/usr/local/cuda-9.0/include/ -I"/home/usr/R/Rcpp/include" -I"/home/usr/R/RViennaCL/include" -I/usr/local/include -fpic -g -O2 -c gpuUtils.cpp -o gpuUtils.o
In file included from /home/usr/R/Rcpp/include/RcppCommon.h:195:0,
from gpuUtils.cpp:1:
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h: In instantiation of ‘SEXPREC* Rcpp::internal::wrap_dispatch_unknown_iterable(const T&, Rcpp::traits::false_type) [with T = viennacl::matrix<double, viennacl::row_major>; SEXP = SEXPREC*; Rcpp::traits::false_type = Rcpp::traits::integral_constant<bool, false>]’:
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h:732:43: required from ‘SEXPREC* Rcpp::internal::wrap_dispatch_unknown(const T&, Rcpp::traits::false_type) [with T = viennacl::matrix<double, viennacl::row_major>; SEXP = SEXPREC*; Rcpp::traits::false_type = Rcpp::traits::integral_constant<bool, false>]’
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h:770:41: required from ‘SEXPREC* Rcpp::internal::wrap_dispatch_eigen(const T&, Rcpp::traits::false_type) [with T = viennacl::matrix<double, viennacl::row_major>; SEXP = SEXPREC*; Rcpp::traits::false_type = Rcpp::traits::integral_constant<bool, false>]’
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h:787:39: required from ‘SEXPREC* Rcpp::internal::wrap_dispatch_unknown_importable(const T&, Rcpp::traits::false_type) [with T = viennacl::matrix<double, viennacl::row_major>; SEXP = SEXPREC*; Rcpp::traits::false_type = Rcpp::traits::integral_constant<bool, false>]’
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h:807:52: required from ‘SEXPREC* Rcpp::internal::wrap_dispatch(const T&, Rcpp::traits::wrap_type_unknown_tag) [with T = viennacl::matrix<double, viennacl::row_major>; SEXP = SEXPREC*]’
/home/usr/R/Rcpp/include/Rcpp/internal/wrap_end.h:30:38: required from ‘SEXPREC* Rcpp::wrap(const T&) [with T = viennacl::matrix<double, viennacl::row_major>; SEXP = SEXPREC*]’
gpuUtils.cpp:68:56: required from here
/home/usr/R/Rcpp/include/Rcpp/internal/wrap.h:523:17: error: static assertion failed: cannot convert type to SEXP
static_assert(!sizeof(T), "cannot convert type to SEXP");
^~~~~~~~~~~~~
make: *** [gpuUtils.o] Error 1
ERROR: compilation failed for package ‘gpuUtils’
* removing ‘/home/usr/R/gpuUtils’
* restoring previous ‘/home/usr/R/gpuUtils’
Short answer: yes. RViennaCl only provides the ViennaCl headers but no as() and wrap() functions for communication between R and C++ data structures. And the gpuR package, which uses RViennaCl, uses a different approach by exposing suitable objects to R with appropriate functions acting on these objects. Of course, you can use these objects to do matrix multiplication on the GPU.
So you would have to write these functions yourself if you are interested in communication between R and ViennaCl data structures. This is not a simple task, though, since RViennaCL patches the ViennaCL headers to include Rcpp.h (e.g. https://github.com/cdeterman/RViennaCL/blob/master/inst/include/viennacl/ocl/backend.hpp#L29). This makes it impossible to use the usual extension mechanisms as you are trying above. I think it would be sufficient for RViennaCL to include RcppCommon.h, this way making extensions possible. But I have not tried that. I have filed https://github.com/cdeterman/RViennaCL/issues/4 for this.
Instead of writing as() and wrap() for ViennaCL you could also combine available converters with a package that converts to and from R data structures. So in a package that uses LinkingTo: Rcpp, RViennaCL, RcppEigen this function works for me:
#define VIENNACL_HAVE_EIGEN
#include <RcppEigen.h>
#include <viennacl/ocl/backend.hpp>
#include <viennacl/matrix.hpp>
//' viennacl matrix crossprod
//'
//' #export
// [[Rcpp::export]]
Eigen::MatrixXd vclCrossprod(const Eigen::MatrixXd &in_densematrix){
int rows = in_densematrix.rows();
int cols = in_densematrix.cols();
viennacl::matrix<double> viennacl_densematrix(rows, cols);
viennacl::copy(in_densematrix, viennacl_densematrix);
viennacl::matrix<double> viennacl_result = viennacl::linalg::prod(
viennacl::trans(viennacl_densematrix),
viennacl_densematrix);
Eigen::MatrixXd eigen_result(cols, cols);
viennacl::copy(viennacl_result, eigen_result);
return eigen_result;
}
However, for the examples I tried, this approach is not really efficient. This might change if you are doing something more complicated than matrix multiplication.
Alternatively you could try RcppArrayFire, which I am working on, if you are mainly interested in GPGPU and not ViennaCl in particular.
How can I insert element in the following map?
map<Longitud,multimap<Latitud, ID> > posicionGeo;
I tried to insert like this:
posicionGeo.insert(make_pair(x.getLongitude(),make_pair(x.getLatitude(),x.getID())));
but this isn't working. Throws this error:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/string:40,
from fecha.h:9,
from principal.cpp:2:
/usr/include/c++/4.8/bits/stl_pair.h: In instantiation of ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = int; _U2 = std::pair<int, int>; _T1 = const float; _T2 = std::multimap<float, unsigned int>]’:
CrimeSearch.hxx:33:51: required from here
/usr/include/c++/4.8/bits/stl_pair.h:119:39: error: no matching function for call to ‘std::multimap<float, unsigned int>::multimap(const std::pair<int, int>&)’
: first(__p.first), second(__p.second) { }
^
/usr/include/c++/4.8/bits/stl_pair.h:119:39: note: candidates are:
In file included from /usr/include/c++/4.8/map:62:0,
from CrimeSearch.h:9,
from principal.cpp:4:
/usr/include/c++/4.8/bits/stl_multimap.h:235:9: note: template<class _InputIterator> std::multimap<_Key, _Tp, _Compare, _Alloc>::multimap(_InputIterator, _InputIterator, const _Compare&, const allocator_type&)
multimap(_InputIterator __first, _InputIterator __last,
^
/usr/include/c++/4.8/bits/stl_multimap.h:235:9: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/string:40,
from fecha.h:9,
from principal.cpp:2:
/usr/include/c++/4.8/bits/stl_pair.h:119:39: note: candidate expects 4 arguments, 1 provided
: first(__p.first), second(__p.second) { }
^
In file included from /usr/include/c++/4.8/map:62:0,
from CrimeSearch.h:9,
from principal.cpp:4:
/usr/include/c++/4.8/bits/stl_multimap.h:219:9: note: template<class _InputIterator> std::multimap<_Key, _Tp, _Compare, _Alloc>::multimap(_InputIterator, _InputIterator)
multimap(_InputIterator __first, _InputIterator __last)
^
/usr/include/c++/4.8/bits/stl_multimap.h:219:9: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/string:40,
from fecha.h:9,
from principal.cpp:2:
/usr/include/c++/4.8/bits/stl_pair.h:119:39: note: candidate expects 2 arguments, 1 provided
: first(__p.first), second(__p.second) { }
^
In file included from /usr/include/c++/4.8/map:62:0,
from CrimeSearch.h:9,
from principal.cpp:4:
/usr/include/c++/4.8/bits/stl_multimap.h:177:7: note: std::multimap<_Key, _Tp, _Compare, _Alloc>::multimap(const std::multimap<_Key, _Tp, _Compare, _Alloc>&) [with _Key = float; _Tp = unsigned int; _Compare = std::less<float>; _Alloc = std::allocator<std::pair<const float, unsigned int> >]
multimap(const multimap& __x)
^
/usr/include/c++/4.8/bits/stl_multimap.h:177:7: note: no known conversion for argument 1 from ‘const std::pair<int, int>’ to ‘const std::multimap<float, unsigned int>&’
/usr/include/c++/4.8/bits/stl_multimap.h:166:7: note: std::multimap<_Key, _Tp, _Compare, _Alloc>::multimap(const _Compare&, const allocator_type&) [with _Key = float; _Tp = unsigned int; _Compare = std::less<float>; _Alloc = std::allocator<std::pair<const float, unsigned int> >; std::multimap<_Key, _Tp, _Compare, _Alloc>::allocator_type = std::allocator<std::pair<const float, unsigned int> >]
multimap(const _Compare& __comp,
^
/usr/include/c++/4.8/bits/stl_multimap.h:166:7: note: no known conversion for argument 1 from ‘const std::pair<int, int>’ to ‘const std::less<float>&’
/usr/include/c++/4.8/bits/stl_multimap.h:157:7: note: std::multimap<_Key, _Tp, _Compare, _Alloc>::multimap() [with _Key = float; _Tp = unsigned int; _Compare = std::less<float>; _Alloc = std::allocator<std::pair<const float, unsigned int> >]
multimap()
^
/usr/include/c++/4.8/bits/stl_multimap.h:157:7: note: candidate expects 0 arguments, 1 provided
I find the solution, you can do the insert in two steps:
multimap<Latitud, ID> mymap;
mymap.insert(make_pair(x.getLatitude(),x.getID()));
posicionGeo.insert(make_pair(x.getLongitude(),mymap));
but i dont know why this:
posicionGeo.insert(make_pair(x.getLongitude(),make_pair(x.getLatitude(),x.getID())));
dont work, i think that is the same.
I get this error:
$ gcc -Wall -g translate.c support.c scanner.c -o translate
support.c: In function ‘readTokens’:
support.c:66:18: warning: assignment from incompatible pointer type [enabled by default]
a[count] = token;
^
here is the readTokens():
void
readTokens(char *fileName, char** a[])
{
FILE *fp;
char *token;
int count = 0;
fp = fopen(fileName, "r");
if (fp == 0)
{
fprintf(stderr,"file %s could not be opened for reading\n", fileName);
exit(1);
}
token = readLine(fp);
while(!feof(fp))
{
a[count] = token;
++count;
free(token);
token = readLine(fp);
}
fclose(fp);
}
I asked a question similar to this earlier and thought I could figure it out from responses but it is still giving me trouble.
If
a[] is an array of strings
then
readTokens(char *fileName, char** a[])
is most certainly wrong and Fumu 7's guess type of a[] may be 'char *' is right, i. e. it has to be
readTokens(char *fileName, char *a[])
- cp. the call of the function readTokens().
I am compiling a project using MSVC2010 compiler i am compiling it against following library (Qt\4.8.1\msvc2010) In that case compilation is successful with no error.
Now I am trying to compilng a QT app using minGW using following library (Qt\4.7.4\mingw).Then i am getting following error.
devicethreadobject.h include following file event.h.
Event.h file contains few template class.
Does MinGW have problem while working with template ?
Why compilation is successfull with MSVC compiler & fails with MINGW compiler ?
Event.h file :----
#ifndef EVENT_H
#define EVENT_H
#include <QtGui>
#include <QEvent>
//String event derived class
template <typename T> class StringEvent : public QEvent
{
QString m_str;
public:
explicit StringEvent(const QString val) : QEvent(staticType()), m_str(val)
{
}
void setvalue(QString val)
{
m_str = val;
}
QString value() const
{
return m_str;
}
static QEvent::Type staticType()
{
static int type = QEvent::registerEventType();
return static_cast<QEvent::Type>(type);
/*
static int type;
if(type == 0)
{
type = QEvent::registerEventType();
}
return static_cast<QEvent::Type>(type);*/
}
static bool is(const QEvent * ev)
{
return ev->type() == staticType();
}
};
class UpdateEvent : public StringEvent<UpdateEvent>
{
public:
explicit UpdateEvent(QString val): StringEvent(val)
{
//qDebug() << "hello";
}
};
class ClearEvent : public StringEvent<ClearEvent>
{
public:
explicit ClearEvent(QString val): StringEvent(val)
{
}
};
#endif // EVENT_H
Error :---
18:23:08: Running build steps for project CanSewLyzer_vs...
18:23:08: Starting: "c:\qtsdk\desktop\qt\4.7.4\mingw\bin\qmake.exe" D:\Stryker\Stryker\Qt\AutoS\CanSewLyzer_err_mingw\CanSewLyzer_vs\CanSewLyzer_vs.pro -r -spec win32-g++ "CONFIG+=release"
18:23:09: The process "c:\qtsdk\desktop\qt\4.7.4\mingw\bin\qmake.exe" exited normally.
18:23:09: Starting: "C:\QtSDK\mingw\bin\mingw32-make.exe"
C:/QtSDK/mingw/bin/mingw32-make.exe -f Makefile.Release
mingw32-make.exe[1]: Entering directory `D:/Stryker/Stryker/Qt/AutoS/CanSewLyzer_err_mingw/CanSewLyzer_vs-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release'
c:/QtSDK/Desktop/Qt/4.7.4/mingw/bin/uic.exe ../CanSewLyzer_vs/mainwindow.ui -o ui_mainwindow.h
g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/QtGui' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/ActiveQt' -I'release' -I'.' -I'../CanSewLyzer_vs' -I'.' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/mkspecs/win32-g++' -o release/main.o ../CanSewLyzer_vs/main.cpp
In file included from ../CanSewLyzer_vs/../../geny/common/mythread/devicethreadobject.h:8,
from ../CanSewLyzer_vs/mainwindow.h:9,
from ../CanSewLyzer_vs/main.cpp:2:
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h: In constructor 'UpdateEvent::UpdateEvent(QString)':
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:56: error: class 'UpdateEvent' does not have any field named 'StringEvent'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:56: error: no matching function for call to 'StringEvent<UpdateEvent>::StringEvent()'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:18: note: candidates are: StringEvent<T>::StringEvent(QString) [with T = UpdateEvent]
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:15: note: StringEvent<UpdateEvent>::StringEvent(const StringEvent<UpdateEvent>&)
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h: In constructor 'ClearEvent::ClearEvent(QString)':
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:68: error: class 'ClearEvent' does not have any field named 'StringEvent'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:68: error: no matching function for call to 'StringEvent<ClearEvent>::StringEvent()'
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:18: note: candidates are: StringEvent<T>::StringEvent(QString) [with T = ClearEvent]
../CanSewLyzer_vs/../../geny/common/mythread/../event/event.h:15: note: StringEvent<ClearEvent>::StringEvent(const StringEvent<ClearEvent>&)
mingw32-make.exe[1]: Leaving directory `D:/Stryker/Stryker/Qt/AutoS/CanSewLyzer_err_mingw/CanSewLyzer_vs-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release'
mingw32-make.exe[1]: *** [release/main.o] Error 1
mingw32-make.exe: *** [release] Error 2
18:23:14: The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project CanSewLyzer_vs (target: Desktop)
When executing build step 'Make'*
It's not MinGW/GCC having issues with templates, it's MSVC being permissive about the syntax.
Change
explicit UpdateEvent(QString val): StringEvent(val)
...
explicit ClearEvent(QString val): StringEvent(val)
With
explicit UpdateEvent(QString val): StringEvent<UpdateEvent>(val)
...
explicit ClearEvent(QString val): StringEvent<ClearEvent>(val)
I am serializing the cl_long2 type from OpenCL which is defined like this (simplified from /usr/include/CL/cl_platform.hpp):
typedef int64_t cl_long;
typedef union
{
cl_long __attribute__ ((aligned(16))) s[2];
struct{ cl_long x, y; };
struct{ cl_long s0, s1; };
struct{ cl_long lo, hi; };
} cl_long2;
I defined the following stand-alone serialization function:
namespace boost{ namespace serialization {
template<class Archive> void serialize(Archive &ar, cl_long2 &i, const unsigned version){
ar & make_nvp("x",i.x);
ar & make_nvp("y",i.y);
}
}};
When I compile my code, which invokes a serialization of cl_long2 from as a class member, I am getting the following error, which I cannot understand (the error is the same if it is not a std::vector<cl_long2>, just a stand-alone variable):
/usr/include/boost/archive/detail/check.hpp: In function ‘void boost::archive::detail::check_object_level() [with T = cl_long2]’:
/usr/include/boost/archive/detail/iserializer.hpp:438:9: instantiated from ‘static void boost::archive::detail::load_non_pointer_type<Archive>::invoke(Archive&, T&) [with T = cl_long2, Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/detail/iserializer.hpp:592:5: instantiated from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::xml_iarchive, T = cl_long2]’
/usr/include/boost/archive/detail/common_iarchive.hpp:66:9: instantiated from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = cl_long2, Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/basic_xml_iarchive.hpp:86:9: instantiated from ‘void boost::archive::basic_xml_iarchive<Archive>::load_override(const boost::serialization::nvp<T>&, int) [with T = cl_long2, Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/xml_iarchive.hpp:93:9: instantiated from ‘void boost::archive::xml_iarchive_impl<Archive>::load_override(T&, int) [with T = const boost::serialization::nvp<cl_long2>, Archive = boost::archive::xml_iarchive]’
/usr/include/boost/archive/detail/interface_iarchive.hpp:60:9: [ skipping 5 instantiation contexts ]
/usr/include/boost/serialization/split_free.hpp:58:9: instantiated from ‘static void boost::serialization::free_loader<Archive, T>::invoke(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_iarchive, T = std::vector<cl_long2>]’
/usr/include/boost/serialization/split_free.hpp:74:5: instantiated from ‘void boost::serialization::split_free(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_iarchive, T = std::vector<cl_long2>]’
/usr/include/boost/serialization/vector.hpp:151:5: instantiated from ‘void boost::serialization::serialize(Archive&, std::vector<U, Allocator>&, unsigned int) [with Archive = boost::archive::xml_iarchive, U = cl_long2, Allocator = std::allocator<cl_long2>]’
/usr/include/boost/serialization/serialization.hpp:128:9: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::xml_iarchive, T = std::vector<cl_long2>]’
/usr/include/boost/archive/detail/iserializer.hpp:188:5: instantiated from ‘void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::xml_iarchive, T = std::vector<cl_long2>]’
myFile.cpp:368:2: instantiated from here
/usr/include/boost/archive/detail/check.hpp:60:5: error: static assertion failed: "typex::value"
The same error appears regardless of whether the archive type is xml or binary. I am using boost::serialization 1.46.
Any hints?
**EDIT: A smaller example including error is at http://ideone.com/4UgCn
If you look at the line where the assertion fails (detail/check.hpp:60), there is a comment right above it:
// trap attempts to serialize objects marked
// not_serializable
BOOST_STATIC_ASSERT(typex::value);
The documentation of this 'not_serializable' trait says that you have to enable this long2 type first. As you found out, this is done by:
BOOST_CLASS_IMPLEMENTATION(long2,boost::serialization::object_serializable)