Hey i'm trying to multithread my program in c++/cli but i'm having problems with creating the threads the code im using is:
private: Void startThread() {
MoveProj.Velocity = Variables.Velocity;
MoveProj.ProjectilePos = Projectile1.ProjectilePos;
Thread^ MotionThread1 = gcnew Thread(gcnew ParameterizedThreadStart(MoveProj, MotionThread::MoveProjectile));
Thread^ MainThread = gcnew Thread(gcnew ThreadStart());
}
but i'm getting the errors
Error 44 error C3350: 'System::Threading::ParameterizedThreadStart' : a delegate constructor expects 2 argument(s) c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h 344
Error 89 error C3350: 'System::Threading::ParameterizedThreadStart' : a delegate constructor expects 2 argument(s) c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h 344
Error 45 error C3350: 'System::Threading::ThreadStart' : a delegate constructor expects 2 argument(s) c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h 345
Error 90 error C3350: 'System::Threading::ThreadStart' : a delegate constructor expects 2 argument(s) c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h 345
Error 43 error C3867: 'MotionThread::MoveProjectile': function call missing argument list; use '&MotionThread::MoveProjectile' to create a pointer to member c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h 344
Error 88 error C3867: 'MotionThread::MoveProjectile': function call missing argument list; use '&MotionThread::MoveProjectile' to create a pointer to member c:\users\gaz\documents\visual studio 2012\projects\projectilemotion\projectilemotion\Simulation.h 344
any help with this would be a great help as its for my college(British so senior year for americnas i think) computing project and my tutor wants it in relatively soon.
The error message is telling you what to do:
function call missing argument list;
use '&MotionThread::MoveProjectile' to create a pointer to member
^
Therefore, here's the correct syntax:
Thread^ MotionThread1 = gcnew Thread(
gcnew ParameterizedThreadStart(MoveProj, &MotionThread::MoveProjectile));
^
For the other one, you're currently trying to create a delegate, without telling it what method the delegate should point to. Try something like this:
Thread^ MainThread = gcnew Thread(gcnew ThreadStart(this, &MyClass::MainMethod));
^^^^^^^^^^^^^^^^^^^^^^^^^^
Edit
I didn't read through your full code. If you expect people to take their time to help you, you need to take some time & spend the effort to distill it down to just what's needed.
I will, however, comment on the errors you're getting.
error C2440: 'initializing' :
cannot convert from 'MotionThread' to 'MotionThread ^'
You've got a variable somewhere that's a reference type, but you're using it without the ^. This is valid C++/CLI, but none of the managed APIs will work with that easily. Switch the member to a ^ and use gcnew.
error C3352: 'float Allformvariables::CalcCurrentVelocity(System::Object ^)' :
the specified function does not match the delegate type 'void (void)'
As the error message says: You're trying to construct a delegate that doesn't take any parameters and returns void, and the method you're passing doesn't match that. Either fix the method or switch to a different delegate type.
error C3754: delegate constructor: member function 'MotionThread::MoveProjectile'
cannot be called on an instance of type 'MotionThread'
I have a feeling this one will go away when you add the missing ^ that I mentioned above.
Related
I am trying to make a Music Player In Visual studio 2010 using c++.
I am using link list in it for next and previous function. However When I try To Assign NULL to a node, It displays an error.
Here is chunk of code For LinkedList:
public ref class node {
public:
// members
static String ^ str="";
node ^ next;
node ^ previous;
int i;
};
Now When I Make next part of node to NULL It shows Error.
node ^ temp = new node();
temp->next=NULL;
Here is Error:
error C2440: '=' : cannot convert from 'int' to 'player::node ^'
NULL is an int, which is not of type "node". You can either cast it temp->next = (node*)NULL; or you can use something like nullptr, which should work without casting. Take a look at the link below:
http://en.cppreference.com/w/cpp/language/nullptr
How do you cast a COM interface pointer to void pointer and then back to the COM pointer? Here is some code to illustrate my problem. It's very similar to this sample code: _com_ptr_t assignment in VC++
CoInitialize(NULL);
COMLib::ICalcPtr pCalc = COMLib::ICalcPtr("MyLibrary.Calculator");
pCalc->doSomething();
CoUninitialize();
return 0;
Now, if I were to cast the pCalc object to void*, how would I cast it back to COMLib::ICalcPtr? For example, the second line in the following code gives me a compile error 'QueryInterface' : is not a member of 'System::Void'. Obviously, it's trying to call IUknown.QueryInterface() on the object. Preferably I would like to do this without creating a new interface (hence, without implicitly calling QueryInterface and AddRef).
void *test = pCalc;
COMLib::ICalcPtr pCalc2 = test;//'QueryInterface' : is not a member of 'System::Void'
FYI, the reason I'm doing this is that the object is going to be passed around from java to jni VC++ code as a void* type. I'm open to any suggestion on what to do or what is going on behind the scene.
Same way you pass any other opaque structure that either doesn't fit in a pointer or doesn't convert easily: by passing its address.
void* test = new COMLib::ICalcPtr(pCalc);
...
COMLib::ICalcPtr pCalc2 = *(COMLib::ICalcPtr*)test;
delete (COMLib::ICalcPtr*)test;
This will result in calls to AddRef and Release, but not QueryInterface.
I dig up an old project and wanted to compile it, but received several errors, a few of those being a c2664:
error C2664: 'std::make_pair' : cannot convert parameter 1 from 'CUser *' to 'CUser *&&'
error C2664: 'std::make_pair' : cannot convert parameter 1 from 'unsigned long' to ' unsigned long &&'
The relevant code parts are:
//typedef for the userdata map
typedef std::map<unsigned long, std::pair<CUser*,userstatus*>> UserDataMapType;
//...
Inc::incret CUserManager::AddUser(unsigned long ID, CUser* pUser, userstatus* pUserStatus)
{
//...
std::pair<UserDataMapType::iterator, bool> ret = m_mapUserData.insert(std::make_pair<unsigned long, std::pair<CUser*, userstatus*>>(ID, std::make_pair<CUser*, userstatus*>(pUser, pUserStatus)));
//...
}
I tried to make the function parameters const, but that did not help.
It did compile just fine in VS2010.
Please help me find what causes this and how to solve it.
make_pair() has been changed in VS2012 to support a new C++11 feature called move semantics and I suspect that explicitly specifying the types for make_pair() is getting in the way.
Remember that make_pair() does not need any template parameters to be explicitly specified. It deduces them from the type of each argument.
Try removing the explicit template arguments from both calls to make_pair() like so...
std::pair<UserDataMapType::iterator, bool> ret = m_mapUserData.insert(std::make_pair(ID, std::make_pair(pUser, pUserStatus)));
Explicitly providing them like this would have worked fine pre-VS2012 because of a new C++11 feature added called move semantics. You'll want to read up on that subject later since you have a shiny new compiler that supports it.
Having trouble with this compiler error, I can't figure out what its moaning about. If anyone could help I will be very grateful. Here is the error:
Error 1 error C2664: 'CPropertyPage::CPropertyPage(UINT,UINT,DWORD)' : cannot convert parameter 2 from 'CWnd *' to 'UINT' c:\users\bnason.prolec\documents\visual studio 2005\projects\autorepair1\autorepair1\customerinformationdlg.cpp 20
and here is the code that seems to be causing it:
CRepairOrderSheet::CRepairOrderSheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
:CPropertySheet(pszCaption, pParentWnd, iSelectPage)
{
this->AddPage(&dlgCustomerInformation);
this->AddPage(&dlgJobsAndParts);
this->AddPage(&dlgRepairSummary);
}
The CPropertyPage constructor takes three parameters: UINT, UINT, and DWORD. It's not clear whether your CRepairOrderSheet derives from CPropertyPage or CPropertySheet (information not provided in question), but the compiler thinks you're trying to construct a CPropertyPage. You are passing it LPCTSTR, CWnd* and UINT. The compiler cannot get the types to match up.
I am implementing a smart pointer class using generics and I wanted to force users of this class to properly construct the smart pointer using syntax such as
MyReference<TestCls>(mytest3))
or
MyReference<TestCls> mytest4(new TestCls());
so I have used the explicit keyword on the CTOR, to prevent this:
MyReference aRef = NULL;
However due to unfortunate circumstances beyond my control, I am working on code that is compiled using the ancient MSVC++ 4.1 compiler. I get the following errors when I include the explicit keyword:
MyReference.h(49) : error C2501: 'explicit' : missing decl-specifiers
MyReference.h(51) : error C2143: syntax error : missing ';' before ''
MyReference.h(52) : error C2238: unexpected token(s) preceding ':'
MyReference.h(52) : error C2059: syntax error : 'int constant'
When I add a #define explicit those errors disappear.
This was a hack on my part, just to get the compiler to ignore the keyword. I'm guessing that this means that explicit is not supported by yon olde compiler.
Can someone confirm this and is there anyone out there with knowledge of a workaround solution for this?
Merci Beaucoups,
Dennis.
This site has a workaround for this, namely:
Unfortunately, older compilers may not
support the use of "explicit", which
could be a headache. If you're stuck
working with an out-of-date compiler
and can't get one that has better
support for the C++ standard, your
best solution may be to take advantage
of the fact that only a single
implicit conversion will take place
for a given value. You can exploit
this by using an intermediate class
that implicitly creates an object of
each type, and then have your main
class implicitly create objects from
that class:
class proxy
{
public:
proxy(int x) : x(x) {} ;
getValue() { return x; }
private:
int x;
};
class String
{
// this will be equivalent of explicit
String(proxy x) { /* create a string using x.getValue(); */ }
}