Error C2248 in Fstream.h - visual-studio

This is the error message I'm getting:
error C2248:
'std::basic_ios<_Elem,_Traits>::basic_ios'
: cannot access private member
declared in class
'std::basic_ios<_Elem,_Traits>' C:\Program
Files\Microsoft Visual Studio
10.0\VC\include\fstream
this is the line it says the error is
template<class _Elem,
class _Traits> inline
void swap(basic_ofstream<_Elem, _Traits>& _Left,
basic_ofstream<_Elem, _Traits>& _Right)
{ // swap _Left and _Right basic_ofstreams
_Left.swap(_Right);
}
this is the code where I access fstream
char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(saveFileNameString).ToPointer();
ofstream sessionFile;
sessionFile.open(stringPointer, std::ios_base::in);
Marshal::FreeHGlobal(IntPtr(stringPointer));`
Thank you in advance!!!!

The problem is not on Fstream.h but on your code.
Check if your not passing a private member variable of a class to ofstream or some other method.

I just run into that problem, and thanks to Luron's comment I figured it out.
I post the explanation in case someone would have the same problem:
There is no copy constructor for stream objects (ostream, istream and derivates), and passing one of these objects to a function will call the copy constructor and so will cause the error.

Related

C++/CLI Compilation error with generic delegate

I'm currently doing some work with C++/CLI, and am encountering a strange compilation error with the following code:
private ref class LinqHelper abstract sealed
{
public:
generic<typename T, typename U> static Func<T, U>^ Cast()
{
return gcnew Func<T, U>(&LinqHelper::Cast);
}
private:
generic<typename T, typename U> static U Cast(T val)
{
return safe_cast<U>(val);
}
};
error C3352: 'Cast' : the specified function does not match the delegate type 'U (T)'
To be more specific, the problematic line of code is:
return gcnew Func<T, U>(&LinqHelper::Cast);
I find myself at a loss to explain this error. The Cast static member function that I am passing to the Func delegate constructor has the required signature, i.e. U Cast(T val).
Could anyone help shed some light on this for me please?
If it is of any help: i am working with VS 2015 and the C++/CLI project is referencing the .NET framework v4.5.2.
Thank you
The compiler is not happy about having to infer the type arguments for the Cast method. Lousy error message, not uncommon in C++/CLI. Fix:
return gcnew Func<T, U>(&LinqHelper::Cast<T, U>);

How I can use std::bind to bind class member function as function pointer?

I have Base and Dervided classes. In the Base class I have a typedef for specific function pointer (I think this is function pointer, im not sure):
typedef void (responseHandler)(BaseClass* instance,int resultCode, char* resultString);
And in the same base class I have several functions which accepts this typedef:
unsigned sendDescribeCommand(responseHandler* responseHandler, Authenticator* authenticator = NULL);
In my custom derived class from this Base class I have my response handler function, like this:
void continueAfterDESCRIBE(RTSPClient* rtspClient, int resultCode, char* resultString);
How I can use this method as input for sendDescribeCommand?
I tried this:
DerivedClass->sendDescribeCommand(DerivedCLass->continueAfterDescribe, MyAuth), but this did not build with error:
"error C3867: 'DerivedClass::continueAfterDESCRIBE': non-standard syntax; use '&' to create a pointer to member"
I also tried to use std::bind:
auto responseCallback = std::bind(&DerivedClass::continueAfterDESCRIBE, DerivedClassInstance);
DerivedClass->sendDescribeCommand(responseCallback, ourAuthenticator);
It also give me an error: no suitable conversion from std::bind to my response handler.
I know there is a way to make my method static, but Im curious if there is another way?

Using Concurrency::concurrent_queue together with std::unique_ptr

I want to use the Concurrency library of Visual Studio 2010 to pass actions between threads.
I have my class SimpleAction and pointers to it are stored in the Concurrency::concurrent_queue.
Using this definition, and 'consumption' logic it works:
typedef Concurrency::concurrent_queue<SimpleAction *> ActionQueue;
while (true)
{
SimpleAction *action = nullptr;
while (m_queue.try_pop(action))
{
action->process();
delete action;
}
Sleep(100);
}
However, when I change this to an std::unique_ptr, like this:
typedef Concurrency::concurrent_queue<std::unique_ptr<SimpleAction>> ActionQueue;
while (true)
{
std::unique_ptr<SimpleAction> action;
while (m_queue.try_pop(action))
{
action->process();
}
Sleep(100);
}
The compiler gives the following error message:
F:\DevStudio\Vs2010\VC\INCLUDE\concurrent_queue.h(366) : error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
with
[
_Ty=`anonymous-namespace'::SimpleAction
]
F:\DevStudio\Vs2010\VC\INCLUDE\memory(2347) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'
with
[
_Ty=`anonymous-namespace'::SimpleAction
]
F:\DevStudio\Vs2010\VC\INCLUDE\concurrent_queue.h(365) : while compiling class template member function 'void Concurrency::concurrent_queue<_Ty>::_Copy_item(Concurrency::details::_Concurrent_queue_base_v4::_Page &,size_t,const void *)'
with
[
_Ty=std::unique_ptr<`anonymous-namespace'::SimpleAction>
]
test.cpp(138) : see reference to class template instantiation 'Concurrency::concurrent_queue<_Ty>' being compiled
with
[
_Ty=std::unique_ptr<`anonymous-namespace'::SimpleAction>
]
It seems the compiler does not like this construction in concurrent_queue:
/*override*/ virtual void _Copy_item( _Page& _Dst, size_t _Index, const void* _Src )
{
new( &_Get_ref(_Dst,_Index) ) _Ty(*static_cast<const _Ty*>(_Src));
}
Which seems logical (we don't want an std::unique_ptr to be copied (it must be moved instead).
Questions:
Is this a known problem/limitation/feature of the Concurrency/PPL library of Visual Studio 2010?
Is this problem solved in Visual Studio 2012?
Or am I doing something wrong?
thanks,
Patrick

Serial Ports in a .DLL file

I have created .dll file in Visual C++.
It includes a function which takes two arguments and send data through serial port. Next i wanted to include this .dll in my application and call these functions. But i am unable to call these functions. Kindly Help.
Here is header file for my .dll
namespace positioncontrol
{
using namespace std;
using namespace System;
using namespace System::IO::Ports;
public ref class control
{
static int rotate(char a, String^ b);
};
}
And here is .cpp for my .dll
#include "goniometer.h"
namespace positioncontrol
{
void control::rotate(char a, String^ b)
{ SerialPort^ serialPort = gcnew SerialPort(L"COM5",9600,Parity::None,1,StopBits::One);
int inp_rotation;
array<unsigned char>^ inp_c = gcnew array<unsigned char>(2);
String^ inp_string;
inp_c[0] = a;
inp_string= b;
inp_rotation=Int32::Parse(inp_string);
inp_c[1] = (unsigned char)inp_rotation;
serialPort->Write(inp_c,0,2);
}
}
I am using this .dll in a desktop application. I have include the header file
#ifndef goniometer_h
#define goniometer_h
#include "goniometer.h"
#endif
Added the path for include directories and added .dll as a reference.
Now i am using the function defined in .dll for a click event
private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e) {
char dir;
dir = 0x42;
String^ inp_string;
inp_string=enter_degree->Text;
positioncontrol::control::rotate(dir,inp_string);
}
Now when i build my desktop application i am getting following error
1>C:\Users\DELL\Desktop\Final\Motor_Dual_API\Debug\goniometer.h(10): error C2011: 'positioncontrol::control' : 'class' type redefinition
1> c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(530): error C2027: use of undefined type 'positioncontrol::control'
1> c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(530): error C3861: 'rotate': identifier not found
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(540): error C2027: use of undefined type 'positioncontrol::control'
1> c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(540): error C3861: 'rotate': identifier not found
Kindly help me in figuring out error.
Thanks and Regards
In C++, the namespace separator is :: rather than .. Check your using statement.
Your code does not define serialPort. Add the definition to the global namespace, to the namespace positionControl or as an auto variable in your function
From the semantic point of view, serialPort is a pointer. So must also create an instance of the object, where serialPort points to.

Problems with reading into buffer using boost::asio::async_read

Good day.
I have a Types.hpp file in my project. And within it i have:
....
namespace RC
{
.....
.....
struct ViewSettings
{
....
};
.....
}
In the Server.cpp file I'm including this Types.hpp file, and i have there:
class Session
{
.....
RC::ViewSettings tmp;
boost::asio::async_read(socket_, boost::asio::buffer(&tmp, sizeof(RC::ViewSettings)),
boost::bind(&Session::Finish_Reading_Data, shared_from_this(), boost::asio::placeholders::error));
.....
}
And during the compilation i have an errors:
error C2825: 'F': must be a class or namespace when followed by '::'
: see reference to class template instantiation 'boost::_bi::result_traits<R,F>'
being compiled with
[
R=boost::_bi::unspecified,
F=void (__thiscall Session::* )(void)
]
: see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>'
being compiled with
[
R=boost::_bi::unspecified,
F=void (__thiscall Session::* )(void),
L=boost::_bi::list2<boost::_bi::value<boost::shared_ptr<Session>>,boost::arg<1>>
]
error C2039: 'result_type' : is not a member of '`global namespace''
And the code like this works in proper way:
int w;
boost::asio::async_read(socket_, boost::asio::buffer(&w, sizeof(int)),
boost::bind(&Session::Handle_Read_Width, shared_from_this(), boost::asio::placeholders::error));
Please, help. What's the problem here?
Thanks in advance.
You are trying to transfer a struct, not just a simple type. That's why it works with int, and won't work with the struct.
I think this is in the area of serialization, so i think you will need boost.serialization.
http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/examples.html
Check this serialization example, maybe that will help you out.
You need to inherit Session from boost::enable_shared_from_this<Session>.

Resources