I'm creating a "date" class in c++, which holds day, month and year variables and bunch of operator functions to use it with.
I have a date.h header and date.cpp for my class and one of the operator functions in date.cpp is giving me bunch of errors.
date.cpp
(I want this operator-function to count days added and return a new date object and avoid changes to the original date object.)
date date::operator+(long days) const{
date dTemp( date.getDay(), date.getMonth(), date.getYear() );
for(int i=0;i<days;i++){
//If days go over a months day count.
if(dTemp.getDay() >= daysInMonth[dTemp.getMonth()]){
dTemp.setDay(1);
if(dTemp.getMonth() < 12){
dTemp.setMonth(dTemp.getMonth() + 1);
}
else{
//Changing a year.
dTemp.setMonth(1);
dTemp.setYear(dTemp.getYear() + 1);
}
}
else{
dTemp.setDay(dTemp.getDay() + 1);
}
}
return dTemp;
}
Errors:
1>h:\c++\teht21\teht20\date.cpp(74): error C2143: syntax error : missing ')' before '.'
1>h:\c++\teht21\teht20\date.cpp(74): error C3484: syntax error: expected '->' before the return type
1>h:\c++\teht21\teht20\date.cpp(74): error C2061: syntax error : identifier 'getDay'
1>h:\c++\teht21\teht20\date.cpp(79): error C2065: 'dTemp' : undeclared identifier
1>h:\c++\teht21\teht20\date.cpp(79): error C2228: left of '.getDay' must have class/struct/union
1> type is ''unknown-type''
Line 74 is:
date dTemp( date.getDay(), date.getMonth(), date.getYear() );
Any help is hugely appreciated.
If you need to see more code, let me know.
If getDay(), getMonth(), getYear() are member functions and you want to call them on this then change:
date dTemp( date.getDay(), date.getMonth(), date.getYear() );
to:
date dTemp( getDay(), getMonth(), getYear() );
Probably you want to call static methods here:
date dTemp( date.getDay(), date.getMonth(), date.getYear() );
So:
date dTemp( date::getDay(), date::getMonth(), date::getYear() );
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.
How do I pass an object, defined in Main, into a function?
My main function looks like this:
// Initialising the players
Player Joe;
Player Sid;
Joe.setPlayerName("Joe");
Sid.setPlayerName("Sid");
Joe.setPercentage(71);
Sid.setPercentage(73);
Joe.setTotal(501);
Sid.setTotal(501);
Joe.setThrows(0);
Sid.setThrows(0);
// Starts Joe's turn (and the game)
turn(Joe);
Within another function called calculateTotal, I have the following section of code:
else if (potentialTotal < 2 && potentialTotal != 0)
{
potentialTotal = total; // Total remains the same
player.setThrows(0); // Resets their throws
// Switch player's turn
if (player.getPlayerName() == "Joe")
{
turn(Sid);
}
else if (player.getPlayerName() == "Sid")
{
turn(Joe);
}
}
The problem is the Sid in turn(Sid) as well as Joe in turn(Joe) do not work, this is because they are declared in the main and not the calculateTotal function.
I have passed the Player object through as seen in calculateTotal function - player.getPlayerName() for example, but if I put turn(player), this would call turn(Joe) as he goes first which is not what I need, I need it to switch to Sid.
The functions in the program.
void turn(Player& player);
void bullseye(Player& player);
void singleThrow(int requiredValue, Player& player);
void doubleThrow(int dartThrow, Player& player);
void trebleThrow(int dartThrow, Player& player);
void calculateTotal(Player& player);
This is the error:
1>f:\abertay\programming\coursework\coursework\source.cpp(149): error C2065: 'Sid' : undeclared identifier
1>f:\abertay\programming\coursework\coursework\source.cpp(153): error C2065: 'Joe' : undeclared identifier
1>f:\abertay\programming\coursework\coursework\source.cpp(169): error C2065: 'Sid' : undeclared identifier
1>f:\abertay\programming\coursework\coursework\source.cpp(173): error C2065: 'Joe' : undeclared identifier
Pass a Reference
If you create your objects in the main and don't want copies of the objects (which appear to be the case), pass a reference to the objects in your main to your calculations function:
void calculations(Player& Joe, Player& Sid)
{
//...
else if (potentialTotal < 2 && potentialTotal != 0)
{
potentialTotal = total; // Total remains the same
player.setThrows(0); // Resets their throws
// Switch player's turn
if (player.getPlayerName() == "Joe")
{
turn(Sid);
}
else if (player.getPlayerName() == "Sid")
{
turn(Joe);
}
}
//...
}
If you are not familiar, a reference is simply another name for an object, it will not copy your Player objects, instead, calculations function will refer to the objects in the main function.
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 am having problems referencing an Enumeration from one header file to another.
I have "Unit.h" which contains:
enum CombatRating
{
CR_HASTE_MELEE = 17,
CR_HASTE_RANGED = 18,
CR_HASTE_SPELL = 19,
};
Then "Object.h" which contains:
void ApplyPercentModFloatValue(uint16 index, float val, bool apply)
{
float value = GetFloatValue(index);
ApplyPercentModFloatVar(value, val, apply);
if (apply && index == CR_HASTE_MELEE && value > 130.86f)
value = 130.86f;
SetFloatValue(index, value);
}
When I build my project, I receive "error C2065: 'CR_HASTE_MELEE' : undeclared identifier". I tried an include for "Unit.h" within "Object.h" but that gives me loads of errors too.
Is there any way to reference the Enum across header files so that a percentage can be applied to "CR_HASTE_MELEE"?
Thanks
I'm doing some tests to a LinkedList, that has two pointers: one point to the next item in the list and the other point to a random node within the list.
Here is the code:
struct Node
{
Node* pNext; // a pointer to the next node in the list
Node* pReference; // a pointer to a random node within the list
int number; // an integer value
};
/**
* This version works for small/medium lists, using recursion.
*/
Node* duplicateList(Node* n)
{
if (n == NULL) return NULL;
return new Node()
{
number = n->number,
pNext = duplicateList(n->pNext),
pReference = duplicateList(n->pReference)
};
}
I'm getting the following errors (VS2010):
d:\dornad\my documents\visual studio 2010\projects\test\test.cpp(21): error C2143: syntax error : missing ';' before '{'
1>d:\dornad\my documents\visual studio 2010\projects\test\test.cpp(22): error C2065: 'number' : undeclared identifier
1>d:\dornad\my documents\visual studio 2010\projects\test\test.cpp(23): error C2065: 'pNext' : undeclared identifier
1>d:\dornad\my documents\visual studio 2010\projects\test\test.cpp(24): error C2065: 'pReference' : undeclared identifier
1>d:\dornad\my documents\visual studio 2010\projects\test\test.cpp(25): error C2143: syntax error : missing ';' before '}'
Thanks.
This bit is not valid C++:
return new Node()
{
number = n->number,
pNext = duplicateList(n->pNext),
pReference = duplicateList(n->pReference)
};
Change it to this:
Node* pNode = new Node();
pNode->number = n->number;
pNode->pNext = duplicateList(n->pNext);
pNode->pReference = duplicateList(n->pReference);
return pNode;
Add a constructor to Node:
struct Node
{
Node(Node* next, Node* ref, int number) : pNext(next), pReference(ref), number(number) { }
// ...
};
then
return new Node(a, b, c);