I have a c++, sfml program and upon trying to implement multi threading have ran into a problem. First off I have little experience with multi threading. When I go to assign the thread tasks it is throwing 22 new errors all relating to objects being const but the objects are not const. Thanks.
sorry if the title is poorly worded.
here's some code to look over
class NewKey {
public:
sf::Image Img;
sf::Texture Tex;
sf::Sprite Sprite;
sf::Vector2i Velocity;
sf::Vector2i Acceleration;
sf::Vector2i NextPos;
bool isMovingLeft() {
if (Velocity.x < 0)
return true;
else
return false;
}
bool isMovingRight() {
if (Velocity.x > 0)
return true;
else
return false;
}
void velocityCap() {
if (Velocity.x >= 130)
Velocity.x = 130;
if (Velocity.y >= 130)
Velocity.y = 130;
if (Velocity.x <= -130)
Velocity.x = -130;
if (Velocity.y <= -130)
Velocity.y = -130;
}
void loseSpeed(int x) {
if (isMovingLeft()) {
Acceleration.x += x;
}
else if (isMovingRight())
Acceleration.x -= x;;
}
void StepVelocity() {
Velocity += Acceleration;
Acceleration = sf::Vector2i(0, 0);
}
};
void foo() {
NewKey Key;
std::thread thread1;
std::unique_ptr <sf::RenderWindow> window = std::make_unique<sf::RenderWindow>(sf::VideoMode(100, 100, 32), "Main Window", sf::Style::None);
Key.NextPos = Gravity(Key, window, thread1);
thread1.join();
}
sf::Vector2i Gravity(NewKey& Key, std::unique_ptr <sf::RenderWindow>& window, std::thread& thread1) {
int windSpeed = 2;
int dropRate = 10;
thread1 = std::thread([=] {
if (Key.Velocity.x == 1)
Key.loseSpeed(1);
else
Key.loseSpeed(windSpeed);
Key.StepVelocity();
Key.velocityCap();
if (Key.Velocity.y < 10 && Key.Velocity.y > 0) {
Key.Velocity.y = 0;
}
});
sf::Vector2i Result = sf::Vector2i(window->getPosition() + Key.Velocity);
return Result;
}
int main()
{
foo();
}
the errors
Severity Code Description Project File Line Suppression State
Error (active) E1086 the object has type qualifiers that are not compatible with the member function "NewKey::loseSpeed" Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 61
Error (active) E1086 the object has type qualifiers that are not compatible with the member function "NewKey::loseSpeed" Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 63
Error (active) E1086 the object has type qualifiers that are not compatible with the member function "NewKey::StepVelocity" Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 64
Error (active) E1086 the object has type qualifiers that are not compatible with the member function "NewKey::velocityCap" Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 65
Error (active) E0137 expression must be a modifiable lvalue Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 67
Error C3861 'Gravity': identifier not found Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 53
Error C2662 'void NewKey::loseSpeed(int)': cannot convert 'this' pointer from 'const NewKey' to 'NewKey &' Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 61
Error C2662 'void NewKey::loseSpeed(int)': cannot convert 'this' pointer from 'const NewKey' to 'NewKey &' Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 63
Error C2662 'void NewKey::StepVelocity(void)': cannot convert 'this' pointer from 'const NewKey' to 'NewKey &' Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 64
Error C2662 'void NewKey::velocityCap(void)': cannot convert 'this' pointer from 'const NewKey' to 'NewKey &' Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 65
Error C3490 'y' cannot be modified because it is being accessed through a const object Error C:\Users\Jacob Krumholz\source\repos\FallingKeys\Error\Error.cpp 67
Related
I have a Visual C++ 6.0 project which I need to update and I import it to Visual Studio 2012 with no problems, however, during compilation, I get the following four errors:
Error 1 error C2440: 'static_cast' : cannot convert from 'void
(__thiscall CTrendDlg::* )(int)' to 'void (__thiscall CCmdTarget::*
)(UINT)' c:\users\nima\desktop\ffls_scode\trenddlg.cpp 89
Error 6 error C2440: 'static_cast' : cannot convert from 'void
(__thiscall CManualDlg::* )(int)' to 'void (__thiscall CCmdTarget::*
)(UINT)' c:\users\nima\desktop\ffls_scode\manualdlg.cpp 175
Error 7 error C2440: 'static_cast' : cannot convert from 'void
(__thiscall CManualDlg::* )(int)' to 'void (__thiscall CCmdTarget::*
)(UINT)' c:\users\nima\desktop\ffls_scode\manualdlg.cpp 177
Error 8 error C2440: 'static_cast' : cannot convert from 'void
(__thiscall CManualDlg::* )(int)' to 'void (__thiscall CCmdTarget::*
)(UINT)' c:\users\nima\desktop\ffls_scode\manualdlg.cpp 178
The first error is originating from the following line of code, and the rest are similar:
ON_COMMAND_RANGE(IDC_CHECK_PEN, IDC_CHECK_GRID, OnCheckButtons)
ON_COMMAND_RANGE(IDC_REF_L1, IDC_REF_L16, OnCarriagePos)
ON_COMMAND_RANGE(IDC_VALVE_L1, IDC_VALVE_L4, OnValve)
ON_COMMAND_RANGE(IDC_SAMPLE_L_A, IDC_SAMPLE_L_B, OnDetector)
Where (as an instance) OnCheckButtons function is defined in TrendDlg.cpp as follows:
void CTrendDlg::OnCheckButtons(int id)
{
UINT state;
RECT rect = {m_rect.left-60, m_rect.top-10, m_rect.right+40, m_rect.bottom+30};
state = ((CButton*)GetDlgItem(id))->GetState();
if ((state & 0x0003) == 1)
{
switch (id)
{
case IDC_CHECK_PEN:
m_pen = TRUE;
break;
case IDC_CHECK_LINE:
m_line = TRUE;
break;
case IDC_CHECK_BUBBLES:
m_bubble = TRUE;
break;
case IDC_CHECK_GRID:
m_grid = TRUE;
}
}
else
{
switch (id)
{
case IDC_CHECK_PEN:
m_pen = FALSE;
break;
case IDC_CHECK_LINE:
m_line = FALSE;
break;
case IDC_CHECK_BUBBLES:
m_bubble = FALSE;
break;
case IDC_CHECK_GRID:
m_grid = FALSE;
}
}
InvalidateRect(&rect);
}
my message map range is also defined as:
BEGIN_MESSAGE_MAP(CTrendDlg, CDialog)
Why is the compiler trying to cast CTrendDlg and CManualDlg type to CCmdTarget type? is there a change in class structure from VC 6.00 to VS2012?
I appreciate your help.
Your handler should be defined like this:
void CTrendDlg::OnCheckButtons(UINT id)
So basically change int to UINT
See here how this macro ON_COMMAND_RANGE should be used:
1) You need a proper signature, return void and the parameter should be of type UINT
2) Qualify your method with class name:
ON_COMMAND_RANGE(IDC_CHECK_PEN, IDC_CHECK_GRID, &CTrendDlg::OnCheckButtons)
^^^^^^^^^^^^
VS 6.0 is quite old, modern VS versions are more standard conformant.
I am trying to create an array of boolean functions, this is currently where I am at.
typedef bool(*fn)(DIYObject*, DIYObject*);
static fn collisionfunctionArray[] =
{
DIY::sphere2Sphere
};
bool DIY::sphere2Sphere(DIYObject* obj1, DIYObject* obj2)
{
DIYSphere *sphere1 = dynamic_cast<DIYSphere*>(obj1);
DIYSphere *sphere2 = dynamic_cast<DIYSphere*>(obj2);
if (sphere1 != NULL && sphere2 != NULL)
{
float X;
X= sphere1->m_position.x - sphere2->m_position.x;
X = X *X;
float Y;
Y = sphere1->m_position.y - sphere2->m_position.y;
Y = Y *Y;
float distance;
distance = sqrt(X + Y);
float distanceCompare;
distanceCompare = sphere1->m_radius + sphere2->m_radius;
if (distance < distanceCompare)
{
sphere1->m_velocity = vec3(0,0,0);
sphere2->m_velocity = vec3(0, 0, 0);
}
}
return false;
}
So at the moment I am only trying to insert one function into the array but I am receiving the following error
Error 2 error C2440: 'initializing' : cannot convert from 'bool (__thiscall DIY::* )(DIYObject *,DIYObject *)' to 'fn'
I think I'm taking in the same arguments so I don't really understand what the issue is here. Thanks
The problem is that sphere2Sphere is a member function of the DIY class and needs an object to put in its this pointer.
As your sphere2Sphere function doesn't use the this pointer (I think), you can make it static, which means it will then match the fn type (as the compiler will know it doesn't need the (hidden) this parameter).
Note: The static keyword goes in the method declaration in your class definition, which you haven't shown here.
Basically I have the next function that I need to call Async:
void waitForFrames(){
CMFTWrapper::IsRunning = true;
while (CMFTWrapper::IsRunning){
result = WaitForSingleObjectEx(CMFTWrapper::FrameEvent, INFINITE, true);
if (result != WAIT_OBJECT_0){
// capture aborted, quit.
}
else if (CMFTWrapper::count > 0){
// copy the bitmap data
}
}
}
Now I tried doing it like this:
create_task(waitForFrames())
.then([this](task<void> frameTask)
{
XTRACE(L"=================================FINISHED WITH FRAME TASK\n");
});
This gives me the next error:
Error 26 error C2228: left of '.then' must have class/struct/union C:\Users\Alin Rosu\Workspace\vidyomobile_windows_phone\Vidyo.DeviceManager\WinRT\DeviceDetection\LmiVideoCapturerWinRTImplementation.cpp 181 1 Vidyo.DeviceManager
Error 24 error C2784: 'Concurrency::task<_Ty> Concurrency::create_task(const Concurrency::task<_Ty> &)' : could not deduce template argument for 'const Concurrency::task<_Ty> &' from 'void' C:\Users\Alin Rosu\Workspace\vidyomobile_windows_phone\Vidyo.DeviceManager\WinRT\DeviceDetection\LmiVideoCapturerWinRTImplementation.cpp 180 1 Vidyo.DeviceManager
Error 25 error C2784: 'Concurrency::task<details::_TaskTypeFromParam<_Ty>::_Type> Concurrency::create_task(_Ty,Concurrency::task_options)' : could not deduce template argument for '_Ty' from 'void' C:\Users\Alin Rosu\Workspace\vidyomobile_windows_phone\Vidyo.DeviceManager\WinRT\DeviceDetection\LmiVideoCapturerWinRTImplementation.cpp 180 1 Vidyo.DeviceManager
Error 45 error LNK1104: cannot open file 'C:\Users\Alin Rosu\Workspace\vidyomobile_windows_phone\Build\ARM\Release\Vidyo.DeviceManager\LmiDeviceManagerWinRT.lib' C:\Users\Alin Rosu\Workspace\vidyomobile_windows_phone\Vidyo.DeviceManager.Test\LINK Vidyo.DeviceManager.Test
Now I tried changing the return value of the function to other stuff from void (Dword, int) but still I get a similar error.
Watching over examples I found on the net, all the functions that use this, that I found return back IAsyncAction.
Example:
create_task(m_pMediaCapture->StartRecordToStorageFileAsync(m_EncodingProfile, m_recordStorageFile))
.then([this](task<void> recordTask)
{
XTRACE(L"=================================will try to get record task\n");
}
How can I do this with my normal function, so that it can run async?
You can pass a lambda directly to the create_task function:
create_task( [](){ /* code here */ } ).
So in your scenario the following should work:
create_task([](){ waitForFrames(); })
.then([this](task<void> frameTask){
XTRACE(L"=================================FINISHED WITH FRAME TASK\n");
});
I have a c++ program that I'm trying to port from VS98 to VS2003 (incremental steps). One error that occurs throughout is "Error 2275"
For instance: k:\RR\chart\chartdlg.cpp(2025): error C2475: 'CRrDoc::cFldFilter' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
The offending code is shown below:
void CDataPage::OnBtnLabelField()
{
FLDID fid ;
LPMFFIELD f ;
CRrApp *pApp = (CRrApp *)AfxGetApp();
CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
CRrDoc *pDoc = (CRrDoc *)pFrame->GetActiveDocument();
CSelectFieldDlg dlg;
//**************************************************
//BOOL CRrDoc::*zcFldFilter = &CRrDoc::cFldFilter;
//dlg.ck = CRrDoc->*zcFldFilter;
//**************************************************
dlg.ck = pDoc->cFldFilter ;
dlg.TitleTextID = IDS_2676;
fid = (FLDID)dlg.DoModal();
if (fid != NOID)
{
f = pDoc->m_pComposite->mfbyndx(fid);
// find index
int i, iCount;
iCount = m_lboxLabel.GetCount();
for (i = 0; i < iCount; i++)
{
if(fid == m_lboxLabel.GetItemData(i))
{
m_lboxLabel.SetCurSel(i);
OnSelchangeComboLabel();
}
}
}
}
I tried handling it according to a Microsoft page: But that just generated a set of other problems (the commented code between the asterisks). Note that I also commented out the following line:
dlg.ck = pDoc->cFldFilter
Unfortunately, this leads to a new error: k:\RR\chart\chartdlg.cpp(2022): error C2440: 'initializing' : cannot convert from 'BOOL (__cdecl )(LPMFFIELD)' to 'BOOL CRrDoc:: '
The definition in the .H file looks like:
public:
static BOOL cFldFilter(LPMFFIELD f);
Any ideas how to handle the pointer-to-member issue?
since you have:
static BOOL CRrDoc::cFldFilter(LPMFFIELD f);
its type is not a member variable but a function:
//BOOL CRrDoc::*zcFldFilter = &CRrDoc::cFldFilter; // doesn't work
BOOL (*zcFldFilter)(LPMFFIELD) = &CRrDoc::cFldFilter; // works
Since dlg.ck is of a correct type, you should do
dlg.ck = &CRrDoc::cFldFilter;
I am using the MArgList class from the Maya API to retrieve arguments entered in the Maya command line. According to the class reference MArgList::get should be able to take an int or double as its second argument but it seems to be expecting a bool only and so throws a conversion error during compiling. The following is the code section and the errors generated. Any thoughts on what might be causing this would be much appreciated. The code was typed straight out of a tutorial on Maya plugin development, so it is a mystery why it is not working.
const int nPosts = 5;
const double radius = 0.5;
const double height = 5.0;
unsigned index;
index = args.flagIndex( "n", "number" );
if( MArgList::kInvalidArgIndex != index )
args.get( index + 1, nPosts );
unsigned index;
index = args.flagIndex( "r", "radius" );
if( MArgList::kInvalidArgIndex != index )
args.get( index + 1, radius );
unsigned index;
index = args.flagIndex( "h", "height" );
if( MArgList::kInvalidArgIndex != index )
args.get( index + 1, height );
1>Posts1Cmd.cpp(37): error C2664: 'MStatus MArgList::get(unsigned int,bool &) const' : cannot convert parameter 2 from 'const int' to 'bool &'
1>Posts1Cmd.cpp(39): error C2086: 'unsigned int index' : redefinition
1> Posts1Cmd.cpp(34) : see declaration of 'index'
1>Posts1Cmd.cpp(42): error C2664: 'MStatus MArgList::get(unsigned int,bool &) const' : cannot convert parameter 2 from 'const double' to 'bool &'
1>Posts1Cmd.cpp(44): error C2086: 'unsigned int index' : redefinition
1> Posts1Cmd.cpp(34) : see declaration of 'index'
1>Posts1Cmd.cpp(47): error C2664: 'MStatus MArgList::get(unsigned int,bool &) const' : cannot convert parameter 2 from 'const double' to 'bool &'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If you are going to get new values from the get function, you cannot have the target variables const.
Try
int nPosts = 5;
double radius = 0.5;
double height = 5.0;
Also, you should not declare a new index variable for each call. Just declare it once and reuse it.