|
I had a memory allocated out side a try block and I am trying to delete in finally?
Should there be any issue with this. I am getting access violation while deleting
char *heap = new char [50];
try
{
......
}
finally(...)
{
delete[] heap;
}
|
|
|
|
|
It should work when not modifying (which includes deleting) the heap pointer inside the try block.
If you are not doing such (just comment the code inside the try block to check it), you should tell us which compiler you are using because finally is not defined by the C++ standard.
|
|
|
|
|
I am using RAD Studio code gear
|
|
|
|
|
So it is the BCC compiler. According to the __finally (C++) - RAD Studio[^] it should be __try and __finally . When omitting the leading underscores the code should not even compile because try requires a catch and __try requires an __except or __finally .
Is it still not working with the correct syntax and an empty __try block?
Then you might ask this in a BCC specific forum like Recent Topics - Forum - Embarcadero Community[^].
|
|
|
|
|
I am getting an error in a windows include winioctl.h
typedef union {
USN_RECORD_COMMON_HEADER Header;
USN_RECORD_V2 V2; <===
USN_RECORD_V3 V3; <===
USN_RECORD_V4 V4; <====
} USN_RECORD_UNION, *PUSN_RECORD_UNION;
for the 3 lines pointed to by the errors the code built find with the windows sdk 8 I migrated the application to windows 10 and am using the windows 10 sdk
Thanks
|
|
|
|
|
ForNow wrote: ...for the 3 lines pointed to by the errors... And that error would be what?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
C2143 syntax error missing ':' before constant the error might make sense if
USN_RECORD_COMMON_HEADER wasn't defined but it is a few lines above
|
|
|
|
|
But are
USN_RECORD_V2
USN_RECORD_V3
USN_RECORD_V4
defined? That's where I'd look, given your original error messages.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
typedef USN_RECORD_V2 USN_RECORD, *PUSN_RECORD;
typedef struct {
DWORD RecordLength;
WORD MajorVersion;
WORD MinorVersion;
FILE_ID_128 FileReferenceNumber;
FILE_ID_128 ParentFileReferenceNumber;
USN Usn;
LARGE_INTEGER TimeStamp;
DWORD Reason;
DWORD SourceInfo;
DWORD SecurityId;
DWORD FileAttributes;
WORD FileNameLength;
WORD FileNameOffset;
WCHAR FileName[1];
} USN_RECORD_V3, *PUSN_RECORD_V3;
ypedef struct {
USN_RECORD_COMMON_HEADER Header;
FILE_ID_128 FileReferenceNumber;
FILE_ID_128 ParentFileReferenceNumber;
USN Usn;
DWORD Reason;
DWORD SourceInfo;
DWORD RemainingExtents;
WORD NumberOfExtents;
WORD ExtentSize;
USN_RECORD_EXTENT Extents[1];
} USN_RECORD_V4, *PUSN_RECORD_V4;
|
|
|
|
|
ForNow wrote: C2143 syntax error missing ':' before constant the error might make sense if
USN_RECORD_COMMON_HEADER wasn't defined but it is a few lines above
Could you post these "few lines"?
|
|
|
|
|
Here is the definition USN_RECORD_COMMON_HEADER
typedef struct {
DWORD RecordLength;
WORD MajorVersion;
WORD MinorVersion;
} USN_RECORD_COMMON_HEADER, *PUSN_RECORD_COMMON_HEADER;
|
|
|
|
|
Have a look in Winioctl.h to check the definitions of these types. My version compiles them fine on VS2017 and Windows 10.
|
|
|
|
|
Check the typedef s of the three structures that fail for mutual non standard members.
A candidate is the USN member.
Do you have a define directive using the same name anywhere in your project?
If that does not help enable the creation of preprocessor output files. Then you can check what the compiler "sees".
|
|
|
|
|
|
i was going through a article about friend function and then in i came across this code:
#include <iostream>
using namespace std;
class Distance
{
private:
int meter;
public:
Distance(): meter(0) { }
friend int addFive(Distance);
};
int addFive(Distance d)
{
d.meter += 5;
return d.meter;
}
int main()
{
Distance D;
cout<<"Distance: "<< addFive(D);
return 0;
}
in the above code what is
Quote: Distance(): meter(0) { }
and how is a object of class distance is able to access a private member ?Quote: int addFive(Distance d)
{
//accessing private data from non-member function
d.meter += 5;
return d.meter;
}
Thank you.
|
|
|
|
|
|
You should get hold of a good C++ book. Probably the best would be the one written by Bjarne Stroustrup[^]
|
|
|
|
|
Welcome to C++ .
I mean:
- constructors are the very basics of an OOP language (you must be sure to grasp them before even attempting to code).
- accessing an otherwise unaccessible member is the very purpose of the
friend declaration.
|
|
|
|
|
Problem statement: You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations on in each move:
1: If we take 2 integers a and b where N=aXb(a!=1,b!=1) then we can change N=max(a,b).
2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0
Input Format
The first line contains the integer Q.
The next Q lines each contain an integer,N .
Output Format
Output Q lines. Each line containing the minimum number of moves required to reduce the value of N to 0.
Sample Input
2
3
4
Sample Output
3
3
Explanation
For test case 1, We only have one option that gives the minimum number of moves. Follow 3->2 -> 1->0 .
Hence, 3 moves.
For the case 2, we can either go 4->3 ->2 ->1 ->0 or4 -> 2-> 1->0 . The 2nd option is more optimal. Hence, 3 moves.
My algo:
if a number is N then
I do looping until sqrt(N) to find if it is a prime or not.
if it is a prime number then N=N-1
if it not a prime number,one of the largest factor(say a) is <=sqrt(N) then other will be b=N/a now b>a then put N=b;
increment count(pass by value).
then next iteration for N ,until it is greater than 1.
return count.
algorithms works fine with small value but predicts less optimal solution for large values.why?
|
|
|
|
|
See here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
i want to make a program that can convert from class a type to class b & vice versa..
i already made a program that does one type of conversion..
#include <iostream>
#include <cmath>
using namespace std;
class Polar;
class Rectangle{
float length, bredth;
public:
Rectangle(){}
Rectangle(float ,float);
int getLength(){return(length);}
int getBredth(){return(bredth);}
void putLength(float x){length = x;}
void putBredth(float y){bredth = y;}
void putData(){cout<<"Length : "<<length<<"\tBreadth : "<<bredth<<endl;}
~Rectangle(){}
};
Rectangle::Rectangle(float x, float y){
length = x;
bredth = y;
}
class Polar{
float angle, rad;
public:
Polar(){}
Polar(float , float);
operator Rectangle(){
Rectangle temp;
float l = rad*cos(angle), b =rad*sin(angle) ;
temp.putLength(l);
temp.putBredth(b);
return(temp);
}
int getAngle(){return(angle);}
int getRadius(){return(rad);}
void putData(){cout<<"Angle : "<<angle<<"\tRadius : "<<rad<<endl;}
~Polar(){}
};
Polar::Polar(float x, float y){
angle = x;
rad = y;
}
int main(){
Rectangle r1(40, 20.23);
Polar p1(120, 25);
r1 = p1;
r1.putData();
return 0;
}
as in the above code i have converted class Polar to class Rectangle, but i also want to have a conversion of class Rectangle to class Polar.
Thank you
|
|
|
|
|
Tarun Jha wrote: as in the above code i have converted class Polar to class Rectangle,
Did you test it?
Does it work?
I ask because I see neither the assignment operator
Rectangle& Rectangle::operator =(const Polar& polar) nor the ctor to create Rectangle instance from the Polar one... 
|
|
|
|
|
but i am not using any variables with reference to heap, so i thought it shouldn't be needed for ex:- new, *variable etc.
|
|
|
|
|

#include <iostream>
#include <cmath>
using namespace std;
class Polar;
class Rectangle
{
float length, bredth;
public:
Rectangle(){}
Rectangle(float ,float);
operator Polar();
int getLength(){return(length);}
int getBredth(){return(bredth);}
void putLength(float x){length = x;}
void putBredth(float y){bredth = y;}
void putData(){cout<<"Length : "<<length<<"\tBreadth : "<<bredth<<endl;}
~Rectangle(){}
};
class Polar{
float angle, rad;
public:
Polar(){}
Polar(float , float);
operator Rectangle();
int getAngle(){return(angle);}
int getRadius(){return(rad);}
void putAngle(float a){angle=a;}
void putRadius(float r){rad = r;}
void putData(){cout<<"Angle : "<<angle<<"\tRadius : "<<rad<<endl;}
~Polar(){}
};
Rectangle::Rectangle(float x, float y)
{
length = x;
bredth = y;
}
Rectangle::operator Polar()
{
Polar temp;
float a = atan2(bredth, length), r = sqrt(length*length + bredth*bredth);
temp.putAngle(a);
temp.putRadius(r);
return(temp);
}
Polar::Polar(float x, float y)
{
angle = x;
rad = y;
}
Polar::operator Rectangle()
{
Rectangle temp;
float l = rad*cos(angle), b =rad*sin(angle);
temp.putLength(l);
temp.putBredth(b);
return(temp);
}
int main()
{
float radians = 120 * M_PI / 180;
Polar p1(radians, 25);
p1.putData();
Rectangle r1 = p1;
r1.putData();
Polar p2 = r1;
p2.putData();
return 0;
}
|
|
|
|
|
thank you 
|
|
|
|