|
I believe that typename is supposed the be prefixed to tell the compiler that invoke_result_t is defining a type:
using RetType = std::typename invoke_result_t<Func, Args...>;
|
|
|
|
|
I have tried:
using RetType = std::typename invoke_result_t<Func, Args...>;
but I got:
error C2589: 'typename': illegal token on right side of '::'
|
|
|
|
|
Try moving it to the front:
using RetType = typename std::invoke_result_t<Func, Args...>; This[^] may be of help.
|
|
|
|
|
I already did:
using RetType = typename std::invoke_result_t<Func, Args...>;
Result:
error C2760: syntax error: unexpected token '<', expected ';'
|
|
|
|
|
See "Helper types" on this page[^]. It looks like invoke_result_t is itself a type alias. Try std::invoke_result without the _t .
|
|
|
|
|
|
CString isWrong = _T(" is wrong");
CString andMsg = _T(" and ");
bool bCorrect[4];
int numOfFalse = 0;
for(int i=0; i<4; i++){
bCorrect[i] = true;
if(arrA[i] != arrB[i]){
bCorrect[i] = false;
numOfFalse++;
}
}
for(int i=0; i<4; i++){
if((bCorrect[i] == false) && (numOfFalse == 2))
{
strRslt.Format("Q%d", i+1);
m_listRslt.AddString(strRslt + isWrong);
}
}
I want to print out something like Q1 and Q2 is wrong. Can anyone teach me ?
|
|
|
|
|
if((bCorrect[i] == false) && (numOfFalse == 2))
{
strRslt.Format("Q%d%sQ%d%s", i, andMsg, i + 1, isWrong);
printf("%s\n", strRslt);
}
|
|
|
|
|
Assume the false value is random assign in the array. The program will check by itself which index has a 'false' then print out the index number.
For example,
index 0 and 3 have 'false', then
print out "Q1 and Q4 is wrong"
|
|
|
|
|
Try this, which keeps track of the number that are false:
strRslt = "";
int wrong = 0;
for(int i=0; i<4; i++)
{
if(bCorrect[i] == false)
{
if (wrong++ > 0)
strRslt.AppendFormat("%s", andMsg);
strRslt.AppendFormat("Q%d", i + 1);
}
}
if (wrong > 0)
{
strRslt.AppendFormat("%s", isWrong);
printf("%s\n", strRslt);
}
|
|
|
|
|
Try (not tested, but you get the idea)
bool bAlreadyIncorrect = false;
for(int i=0; i<4; i++)
{
if ( bCorrect[i] == false )
{
if ( bAlreadyIncorrect )
m_listRslt.AddString(" and ");
strRslt.Format("Q%d", i+1);
m_listRslt.AddString(strRslt);
bAlreadyIncorrect = true;
}
}
if (bAlreadyIncorrect)
m_listRslt.AddString( " is wrong");
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
hello
I need help
Create a class Employee that includes:
1. Three instance variables: a Name(type string), HireYear(type
integer), and a monthly Salary (type double) .
2. A constructor that initializes the three instance variables .
3. Public methods :
a. Set and Get for each instance variable. If the monthly salary
is not positive, do not set its value .
b. GetEmployeeInfo: returns a string contains the employee
name, hire year, and his/her monthly salary.
|
|
|
|
|
Member 15128932 wrote: I need help Then you need to explain exactly what help you need. If you expect someone to do your work for you, I am afraid you have come to the wrong place. This site is here to help with code that you write.
|
|
|
|
|
So..., start coding.
And post here specific questions when you're stuck.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
in c++ : (Account Class) Create an Account class that a bank might use to represent customers' bank accounts. Include a data member of type int to represent the account balance. Provide a constructor that receives an initial balance and uses it to initialize the data members. The constructor should validate the initial balance to ensure that it's greater than or equal to 0. If not, set the balance to 0 and display an error message indicating that the initial balance was invalid. Provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account's balance. If it does, the balance should be left unchanged and the function should print a message indicating "Debit amount exceeded account balance." Member function getBalance should return the current balance. Create a program that creates at least two Account objects and tests the member functions of class Account.
task 1:derived two classes from Account class
one is bank albilad data members visa, points o
ther is alrajhi bank data members mastercard, points
set and get functions and chargevisa and chargemastercards.
set not exceed account balance.
charge not exceed visa or mastercards balance.
set points visa 10%, mastercards 15%. get functions
Make appropriate member functions const.
task 2:disinherite the classes
classes should share set and get function.
|
|
|
|
|
...and you had question about this? What part of this don't you understand, or are having a problem with.
Just posting your homework assignment is not asking a question.
|
|
|
|
|
I am trying to get ``sort(begin, end, compare)`` to work, where begin and end are iterators and compare is a boolean function. The Asset constructor is commented out because it generates an error.
I have tried to follow the example in Beginners guide to the std::sort() funct - C++ Articles[^]
This is the code:
class RDA_Names {
struct Asset { long long int asset_No = 0; string asset_Name = ""; };
struct {
bool compare(const Asset& lhs, const Asset& rhs) { return lhs.asset_No < rhs.asset_No; }
} comp;
bool compare(const Asset& x, const Asset& y);
array<Asset, 500> asset_Data;
};
bool RDA_Names::compare(const Asset& lhs, const Asset& rhs) {
return (lhs.asset_No < rhs.asset_No);
};
void RDA_Names::post_process_names() {
sort(asset_Data.begin(), asset_Data.end()) sort(asset_Data.begin(), asset_Data.end(), compare); sort(asset_Data.begin(), asset_Data.end(), comp); };
For each case, the other two cases are commented out. The format of the errors is Error number(repetition), Error text, lines within
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\ where the error was reported.
Case 1:
C2893 (5) Failed to specialize function template
\include\algorithm Lines 7361, 7419, 7423, 7434, 7447
C2672 (5) 'operator __surrogate_func': no matching overloaded function found
\include\xutility Line 1622
Case 2:
C2780 (1) 'void std::sort(const _RanIt,const _RanIt)': expects 2 arguments - 3 provided
C2672 (1) 'sort': no matching overloaded function found
C3867 (1) 'RDA_Names::compare': non-standard syntax; use '&' to create a pointer to member
Case 3:
C2064 (5) term does not evaluate to a function taking 2 arguments
\include\xutility Line 1622
\include\algorithm Lines 7419, 7423, 7434, 7447
|
|
|
|
|
Case 1 fails because there's no comparison function for sort . It might work if Asset defined operator< , but I'd have to check.
EDIT: If you define bool Asset::operator<(const Asset& that) , then case 1 also compiles.
Case 2 fails because compare takes a this pointer. If a comparison function is going to be part of a class, it needs to be declared static .
Case 3, I'd have to investigate. I've used this struct technique, but only for a set I believe. It's likely that array doesn't use it.
Try making your compare function a free function (outside the class) or static and see what happens in case 2. In the meantime, I'll look at this further.
* * *
Fixing case 2 looks like the way to go. I compiled the following successfully, and the constructor didn't give me any problems:
class RDA_Names {
struct Asset {
long long int asset_No = 0;
string asset_Name = "";
Asset(long long int asset, string name): asset_No(asset), asset_Name(name) {}
};
static bool compare(const Asset& x, const Asset& y);
void post_process_names();
array<Asset, 500> asset_Data;
};
bool RDA_Names::compare(const Asset& lhs, const Asset& rhs) {
return (lhs.asset_No < rhs.asset_No);
};
void RDA_Names::post_process_names() {
std::sort(asset_Data.begin(), asset_Data.end(), compare); };
modified 26-Mar-21 8:55am.
|
|
|
|
|
Although it's a waste of time to say this,
And I know you've heard it before.
But your answers were most pointed
And it clearly went to the fore.
We thank you all
My wife because I don't grumble
I with nothing to grumble about.
I have tried to follow all of your suggestions (my following is full). I now have pointers or references everywhere and the thing seems to compile without complaint. Now to get rid of those damn'd nuisance run-time errors.
thanks!
|
|
|
|
|
To complement what Greg was saying:
- case 3 fails because comp is a structure not a function returning a bool. Try changing it to:
bool comp (const RDA_Names::Asset& lhs, const RDA_Names::Asset& rhs)
{
return lhs.asset_No < rhs.asset_No;
}
You will have to also make Asset structure public.
As a case 4 you can try this:
sort (asset_Data.begin (), asset_Data.end (), [](auto& l, auto& r)->bool {
return l.asset_No < r.asset_No; });
where the comparison function is an anonymous lambda.
The next step is to look at how efficient your code will be. If you are sorting only 500 objects and you don't do it too often, it's probably OK. In general however your solution is highly inefficient. Every time sort has to exchange two elements it makes copies of the asset_Name string.
There are various ways to improve it:
- keep a unique_ptr pointer to string and provide a move constructor for the Asset structure.
- create an array of indexes in RDA_Names and sort it inseted of sorting the asset_Data array.
Mircea
modified 26-Mar-21 9:14am.
|
|
|
|
|
Your first point is right on, and gets me back to Case 2. You're second point is incorrect. What is being sorted is an object of some type. The sort routine ignores the type and whether the object containing the type is private or public. It only works on comparison, I assume signed char comparison if there is no comparison function, and the ability to index and swap. The comparison function has no cognizance of privacy, it is only passed pointers/objects and returns a bool.
At the sort base level, the input object need not be public, and if the input object contains objects, these contained objects need not be public.
Just guessing.
thanks
|
|
|
|
|
Quote: Just guessing.
Well, you didn't guess right
Pretty much all you said is valid for the old C qsort function. std::sort is an entirely different beast. I suggest you carefully read the description at std::sort - cppreference.com[^]
Quote: The sort routine ignores the type and whether the object containing the type is private or public.
I'm not sure I understand what you mean by "the object containing the type". In C++ objects don't contain types. Besides the sort routine is not ignoring the object type at all. According to C++ reference: "The type of dereferenced RandomIt must meet the requirements of MoveAssignable and MoveConstructible". That requires at least a copy constructor for the object (either user defined or generated by the compiler).
Quote: The comparison function has no cognizance of privacy, it is only passed pointers/objects and returns a bool.
Of course not, the comparison function needs to access the members of the object in order to compare them. Either it is a member function or a friend function or the members in question are public.
The example on the C++ reference page mentioned above shows all the different methods you can use to invoke the std::sort algorithm.
Mircea
|
|
|
|
|
My knowledge of the low level implementation of C++ structures and classes is limited.
But I want to ask someone who might know, is it theoretically possible to derive a class from a Win32 structure and add methods and still be able to pass the new "structure" to a Win32 API?
I do know that adding class data members would be out of the question, because that would change the size of the structure.
But is adding methods to a structure something I could do and still use it in the windows api?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Yes, it is very much possible. You can write something like this:
class MyPen : public LOGPEN {
public:
MyPen ();
void use_pen ();
private:
int stuff;
}
MyPen the_pen;
HPEN hp = CreatePenIndirect ((LOGPEN*)&the_pen);
Note that you can also extend your object with additional data members. They will be placed after the base class (Windows structure) and they will do no harm.
Mircea
|
|
|
|
|
Thanks for your reply. I'm excited to try this.
Your example is great. But if I wanted to use an array of MyPen's then that would surely cause problems because of the larger size, no?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|