Problems with nested lambdas in VC++ - visual-c++

Does anyone know why this code is not compilable with VC++ 2010
class C
{
public:
void M(string t) {}
void M(function<string()> func) {}
};
void TestMethod(function<void()> func) {}
void TestMethod2()
{
TestMethod([] () {
C c;
c.M([] () -> string { // compiler error C2668 ('function' : ambiguous call to overloaded function)
return ("txt");
});
});
}
Update:
Full code example:
#include <functional>
#include <memory>
using namespace std;
class C
{
public:
void M(string t) {}
void M(function<string()> func) {}
};
void TestMethod(function<void()> func) {}
int _tmain(int argc, _TCHAR* argv[])
{
TestMethod([] () {
C c;
c.M([] () -> string { // compiler erorr C2668 ('function' : ambiguous call to overloaded function M)
return ("txt");
});
});
return 0;
}

This is a bug of the VC++ Compiler.
https://connect.microsoft.com/VisualStudio/feedback/details/687935/ambiguous-call-to-overloaded-function-in-c-when-using-lambdas

You did not post the error message, so by looking into my crystal ball I can only conclude that you suffer those problems:
Missing #includes
You need at the top
#include <string>
#include <functional>
Missing name qualifications
You either need to add
using namespace std;
or
using std::string; using std::function;
or
std::function ...
std::string ...
Missing function main()
int main() {}
Works with g++
foo#bar: $ cat nested-lambda.cc
#include <string>
#include <functional>
class C
{
public:
void M(std::string t) {}
void M(std::function<std::string()> func) {}
};
void TestMethod(std::function<void()> func) {}
void TestMethod2()
{
TestMethod([] () {
C c;
c.M([] () -> std::string { // compiler error C2668
return ("txt");
});
});
}
int main() {
}
foo#bar: $ g++ -std=c++0x nested-lambda.cc
Works fine.

Related

How to export a __interface derived class in visual c++?

I want to export a class type that derives from a __interface(only available for Visual c++),which aiming to create c#-like inteface-oriented experience.
//This wiil be defined in a "Famous.h"
__interface IFoo {
int GetNum();
};
//And this is my implementation.
#include "...Famous.h"
class FooImpl : public IFoo
{
public:
FooImpl(int a);
private:
int a;
};
FooImpl::FooImpl(int a)
{
this->a = a;
}
The problem is ,How could I export the 'FoolImpl' type to use it as follows in another project without refrencing the project directly.I tried dllexport,and tons of errors make me exthausted :(.
#include "..Impl.h"
#include "..Famous.h"
void main(){
IFoo* foo = new FooImpl(2);
...
}
Considering of there's no interface in c++,I replaced the __interface with pure virtual class.
//This wiil be defined in a "Famous.h"
#ifdef FAMOUS_EXPORTS
#define XYZAPI __declspec(dllexport)
#else
#define XYZAPI __declspec(dllimport)
#endif
class XYZAPI IFoo {
virual int GetNum() = 0;//void type also valid.
};
//And this is my implementation(Impl.h).
#ifdef IMPL_EXPORTS
#define XYZAPI __declspec(dllexport)
#else
#define XYZAPI __declspec(dllimport)
#endif
#include "...Famous.h"
class XYZAPI FooImpl : public IFoo
{
public:
FooImpl(int a);
private:
int a;
};
FooImpl::FooImpl(int a)
{
this->a = a;
}
And this is my invocation.
#include "..Impl.h"
#include "..Famous.h"
void main(){
IFoo* foo = new FooImpl(2);
...
}

C++: Migrating dll from VS 2008 to VS 2010: error C2678

I'm migrating the code of an dll, developed with C++, from Visual Studio version 2008 to 2010.
I'm facing this error below which is related to the 'C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xstring'.
Error C2678: binary operator '!=': no operator found which takes a left-hand operand of type 'c_allocator<_Ty>' (or there is no acceptable conversion).
The error occurs with the line 'class c_allocator: public std::_Allocator_base<_Ty>{'.
#include <stdlib.h>
#include <vector>
#include "Synchro.h"
template<class _Ty>
class c_allocator: public std::_Allocator_base<_Ty>{
public:
typedef _Allocator_base<_Ty> _Mybase;
typedef typename _Mybase::value_type value_type;
typedef value_type _FARQ *pointer;
typedef value_type _FARQ& reference;
typedef const value_type _FARQ *const_pointer;
typedef const value_type _FARQ& const_reference;
typedef _SIZT size_type;
typedef _PDFT difference_type;
template<class _Other>struct rebind{
typedef c_allocator<_Other> other;
};
pointer address(reference _Val) const {
return (&_Val);
}
const_pointer address(const_reference _Val) const {
return (&_Val);
}
c_allocator() _THROW0() {
}
c_allocator(const c_allocator<_Ty>&) _THROW0() {
}
template<class _Other>c_allocator(const c_allocator<_Other>&) _THROW0() {
}
template<class _Other>c_allocator<_Ty>& operator=(const c_allocator<_Other>&) {
return (*this);
}
void deallocate(pointer _Ptr, size_type) {
CustomAlloc::LinearAllocHolder<1>::alloc.freemem(_Ptr);
}
pointer allocate(size_type _Count) {
return (pointer)CustomAlloc::LinearAllocHolder<1>::alloc.getmem(_Count*sizeof(value_type));
}
pointer allocate(size_type _Count, const void _FARQ *) {
return (allocate(_Count));
}
void construct(pointer _Ptr, const _Ty& _Val) {
std::_Construct(_Ptr, _Val);
}
void destroy(pointer _Ptr) {
std::_Destroy(_Ptr);
}
_SIZT max_size() const _THROW0() {
_SIZT _Count = (_SIZT)(-1) / sizeof (_Ty);
return (0 < _Count ? _Count : 1);
}
};
Any hint much appreciated -
thanks in advance -
surplus

Issue with thread_specific_ptr data deletion at thread end

I use the thread local storage with boost.
I have a global variable :
boost::thread_specific_ptr<MyDataClass> p_timeline_ctx;
and I have the following class, which encapsulates a boost::thread object and contains an additionnal data object :
class MyThread {
private :
boost::thread t;
MyDataClass d;
public :
MyThread():c() {}
void start(void) {
ptr.reset(this->d);
this->t = boost::thread(&MyThread::worker, this);
}
void worker(void) {
// do something
}
};
I do not get any error when compiling. But on runtime, when the worker function exits and the thread ends, I get a "glibc ... free ... invalid pointer" error.
I guess this comes from the fact that, according to the boost doc, the thread_specific_ptr tries to delete the object it points to when threads end. But I do not see how to solve the problem.
The thread specific pointer takes ownership. You could reset it:
p_timeline_ctx.reset(0);
or intialize it with a deep copy in the first place:
ptr.reset(new MyDataStruct(d));
However, you'd be far better off just passing the reference as an argument to the thread pointer.
In fact, the worker is already an instance member function, so, why do you need a thread-specific copy of this:
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>
struct MyDataClass { };
class MyThread {
private :
boost::thread t;
MyDataClass d;
public :
MyThread(): d() {}
void start(void) {
t = boost::thread(&MyThread::worker, this);
}
void worker() {
// just use this->d here
}
};
int main()
{
}
Or using a static thread function:
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>
struct MyDataClass { };
class MyThread {
private :
boost::thread t;
MyDataClass d;
public :
MyThread(): d() {}
void start(void) {
t = boost::thread(&MyThread::worker, boost::ref(d));
}
static void worker(MyDataClass&) {
// do something
}
};
int main()
{
}

PCL 1.6: generate pcd files from each frame of a oni file

I need to process each frame of a ONI file. For now I want just to save each frame of a file.oni in file.pcd. I follow this code but it works only with PCL 1.7 and I'm using v1.6.
So I changed a bit the code in this manner
#include <pcl/io/openni_grabber.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/oni_grabber.h>
#include <pcl/io/pcd_io.h>
#include <vector>
int i = 0;
char buf[4096];
class SimpleOpenNIViewer
{
public:
SimpleOpenNIViewer () : viewer ("PCL OpenNI Viewer") {}
void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &cloud)
{
//if (!viewer.wasStopped())
//{
// viewer.showCloud (cloud);
pcl::PCDWriter w;
sprintf (buf, "frame_%06d.pcd", i);
w.writeBinaryCompressed (buf, *cloud);
PCL_INFO ("Wrote a cloud with %zu (%ux%u) points in %s.\n",cloud->size (), cloud->width, cloud->height, buf);
++i;
//}
}
void run ()
{
pcl::Grabber* interface = new pcl::OpenNIGrabber("file.oni");
boost::function<void (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr&)> f = boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);
interface->registerCallback (f);
interface->start ();
while (!viewer.wasStopped())
{
boost::this_thread::sleep (boost::posix_time::seconds (1));
}
PCL_INFO ("Successfully processed %d frames.\n", i);
interface->stop ();
}
pcl::visualization::CloudViewer viewer;
};
int main ()
{
SimpleOpenNIViewer v;
v.run ();
return 0;
}
But it crash when I run it. Why?
I solved my problem about getting each frame from a ONI file. I need to use the ONIGrabber function set in the trigger mode.
This is the modified code:
#include <pcl/io/openni_grabber.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/oni_grabber.h>
#include <pcl/io/pcd_io.h>
#include <vector>
class SimpleOpenNIViewer
{
public:
SimpleOpenNIViewer () : viewer ("PCL OpenNI Viewer") {}
void cloud_cb_ (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr &cloud)
{
//if (!viewer.wasStopped())
//{
// viewer.showCloud (cloud);
pcl::PCDWriter w;
sprintf (buf, "frame_%06d.pcd", i);
w.writeBinaryCompressed (buf, *cloud);
PCL_INFO ("Wrote a cloud with %zu (%ux%u) points in %s.\n",cloud->size (),
cloud->width, cloud->height, buf);
++i;
//}
}
void run ()
{
pcl::Grabber* interface = new pcl::ONIGrabber("file.oni",false,false); //set for trigger
boost::function<void (const pcl::PointCloud<pcl::PointXYZ>::ConstPtr&)> f = boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);
interface->registerCallback (f);
interface->start ();
while (!viewer.wasStopped())
{
interface->start()//to update each frame from the oni file
boost::this_thread::sleep (boost::posix_time::seconds (1));
}
PCL_INFO ("Successfully processed %d frames.\n", i);
interface->stop ();
}
pcl::visualization::CloudViewer viewer;
};
int main ()
{
SimpleOpenNIViewer v;
v.run ();
return 0;
}`

Undefined reference to a function defined by me

When I call readTransition() function in readDPDA() function declaration I get linker error: undefined reference.
How can I use a function defined by me in another function's declaration ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readTransitionRules(char * temp);
void readDPDA(char * filename);
int main()
{
// irrelevant
}
void readTransitionRules(char * temp)
{
char *tempToken;
tempToken=strtok(temp," ,:");
int i;
for(i=0; i<5; i++)
{
printf("%s",tempToken);
strtok(NULL," ,:");
}
}
void readDPDA(char * filename)
{
/*This function tries to open DPDA text file to read
states,alphabet,stack symbols and transition rules
of the DPDA that we will use as Word Checker. */
extern void readTransitionRules(char * temp);
char * temp;
FILE * readerForDPDA;
readerForDPDA = fopen(filename,"r");
if(readerForDPDA!=NULL)
{
fgets(temp,30,readerForDPDA);
if(temp[0]=='T')
{
readTransitionRule(temp);
}
}
else
{
}
}
In ReadDPDA you refer to a readTransitionRule and not a readTransitionRules as you have defined. You are missing the letter s.
You are calling readTransitionRule, but your function is named readTransitionRules.
You probably had a warning about implicit function declaration. Don't ignore warnings.

Resources