|
Andraw111 wrote: I don't where should I put my Fortran question, so I put it here. Have you tried 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
|
|
|
|
|
Dear all,
Consider the following code:
class Person
{
Person *parent;
public:
Person(Person *p){parent = p;}
};
int main()
{
Person grandfather(nullptr);
Person father(&grandfather);
Person son(&father);
return 0;
}
The inter-object relationship works fine, as long as I don't pass the objects around as function arguments (e.g. pass by value) or clone the objects. If I want to pass them as function parameters, or make a clone of the three objects, without breaking their relationships, what should I do?
Thanks.
Milan.
|
|
|
|
|
Maybe a std::shared_ptr can be helpful for you.
You can use it as follows:
#include <memory>
using namespace std;
class Person
{
shared_ptr<Person> parent;
public:
Person(shared_ptr<Person> p) {parent = p;}
};
void DoSomething(shared_ptr<Person> p)
{
}
int main()
{
shared_ptr<Person> grandfather = make_shared<Person>(nullptr);
shared_ptr<Person> father = make_shared<Person>(grandfather);
shared_ptr<Person> son = make_shared<Person>(father);
DoSomething(father);
return 0;
}
|
|
|
|
|
You just beat me to it.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
Not sure what exactly is not working for you, but if object lifetime is the problem, you could use shared_ptr .
class Person
{
shared_ptr<Person> parent;
public:
Person(shared_ptr<Person> p){ parent = p; }
};
int main()
{
auto grandfather(make_shared<Person>(nullptr));
auto father(make_shared<Person>(grandfather));
auto son(make_shared<Person>(father));
return 0;
}
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
If you want to use them as function parameters, pass them by reference, not by value.
void function1(Person p)
{
}
void function2(Person& p)
{
}
If you use shared_ptr , remember that you have to make every Person who will be a parent a shared_ptr , otherwise its child will try to delete it (which it has no right to do) when the last goes out of scope.
If you want to create a clone of a Person, and clone its relations, you will need to think hard about lifetime management and ownership. What should happen if you tried to clone father? Should it create a clone of grandfather? That is quite simple to do, but it means father will have to delete its grandfather clone in its destructor. Then what happens if another Person has that grandfather as a parent?
|
|
|
|
|
I can't possibly be ClistCtrl because if I do
(ClistCtrl *)GetDlgItem(...) and then try to insert items and columns (via the documented member functions of CListCtrl, absolutely nothing happens. The list box control remains resolutely blank.
|
|
|
|
|
The MFC class corresponding to the listbox control is CListBox .
If you look carefully you will find there are 2 different controls - List Box and List Control.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
|
|
|
|
|
<blockquote class="quote"><div class="op">Quote:</div>
#include<stdio.h>
#include<stdlib.h>
int main(int argc,int *argv[])
{
srand(12345);
int i,j;
float a=0.2;
double x;
double sqrt(double);
printf("========================================================\n");
printf("Generated random number\t || Squaring of random number:\n");
printf("=========================================================\n");
for(i=0;i<100;i++)
{
double f= ((float)rand()/(float)(RAND_MAX))*a;
printf("%f\t",f);
printf("\t\t%f\n",x*f);
}
FILE *fout;
fout=fopen("R_Number","w");
j=0;
srand(12345);
while(j<100)
{
double f= ((float)rand()/(float)(RAND_MAX))*a;
fprintf(fout,"%f\t\t\t%f\n",f,f*f);
j++;
}
fclose(fout);
return 0;
}
double sqrt (double f)
{
double x, z, tempf;
unsigned long *tfptr = ((unsigned long *)&tempf) + 1;
tempf = f;
*tfptr = (0xbfcdd90a - *tfptr)>>1;
x = tempf;
z = f*0.5;
x = (1.5*x) - (x*x)*(x*z);
x = (1.5*x) - (x*x)*(x*z);
return x*f;
}
</blockquote>
|
|
|
|
|
Where did you find that obscure sqrt() function? I guess it was originally using float rather than double .
Remove that and use the C standard library sqrt() function by including math.h.
|
|
|
|
|
actually i changed it to double n include math.h also but output seem same as before.
source for square function
|
|
|
|
|
You must remove your version of the sqrt() function. Otherwise it will be used instead of the standard library version.
Your version uses ugly programming that directly accesses a single precision (float ) value. By changing the type to double it won't work any more.
[EDIT]: Forget that. It is an approximation that might be from http://www.azillionmonkeys.com/qed/sqroot.html[^].
But such hardware dependant code should not be used.
modified 29-Jul-14 6:41am.
|
|
|
|
|
Quote: double sqrt (double f)
{
double x, z, tempf;
unsigned long *tfptr = ((unsigned long *)&tempf) + 1;
tempf = f;
*tfptr = (0xbfcdd90a - *tfptr)>>1;
x = tempf;
z = f*0.5;
x = (1.5*x) - (x*x)*(x*z); //The more you make replicates of this statement
//the higher the accuracy, here only 2 replicates are used
x = (1.5*x) - (x*x)*(x*z);
return x*f;
}
That is really messy!
You are using uninitialized variables (that is garbage).
It looks you want to implement the Babylonian method[^] but you do nothing for computing the initial guess.
The following code is based on that very Wikipedia page.
void initial_guess(double r, int *pa, int *pn)
{
*pn = 0;
while ( r < 1.0)
{
r *= 100.0;
--(*pn);
}
while ( r >= 100.0 )
{
r /= 100.0;
++(*pn);
}
*pa = (r < 10.0) ? 2 : 6;
}
double square_root(double r)
{
int a, i, n;
initial_guess(r, &a, &n);
double x=1.0;
while (n < 0)
{
x/=10.0;
++n;
}
while(n > 0)
{
x *= 10.0;
--n;
}
x *= a;
for (i = 0; i<10; ++i) {
x = 0.5 * (x + r/x);
}
return x;
}
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
|
You are welcome.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
using
for(i=0;i<5;i++)
{
float f= ((float)rand()/(float)(RAND_MAX))*a;
y=square(f);
sum +=y;
}
printf("\n summation of squared number is:=%f\t",sum);
gives output but not the exact answer..
any solution actually i want to find the summation of that squared float number..
|
|
|
|
|
How do you check the result?
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Quote:
Generated random number || Squaring of random number:
=========================================================
0.035679 ----- 0.001273
0.079935 ----- 0.006390
0.033320 ----- 0.001110
0.042424 ----- 0.001800
0.012387 ----- 0.000153
summation of squared number is:=0.010346
where the actual summation should be 0.10726
|
|
|
|
|
You are wrong, the output of the following program:
int main()
{
int i;
double sum = 0.0;
double a[] =
{
0.035679,
0.079935,
0.03332,
0.042424,
0.012387
};
for (i=0; i<sizeof(a)/sizeof(a[0]); ++i)
{
double sq = square_root(a[i]);
printf("a[%d] = %g, sqrt(a[%d])=%g\n", i, a[i], i, sq);
sum += sq;
}
printf("sum of square roots = %g\n", sum);
}
is
a[0] = 0.035679, sqrt(a[0])=0.188889
a[1] = 0.079935, sqrt(a[1])=0.282728
a[2] = 0.03332, sqrt(a[2])=0.182538
a[3] = 0.042424, sqrt(a[3])=0.205971
a[4] = 0.012387, sqrt(a[4])=0.111297
sum of square roots = 0.971422
That is correct (at least according to Excel ).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
this is different program its squaring of number not square root...
|
|
|
|
|
How do you compute the squares?
This program:
#include <stdio.h>
int main()
{
double a [] =
{
0.035679,
0.079935,
0.033320,
0.042424,
0.012387
};
double sum, square;
int n;
sum = 0.0;
for (n=0; n<sizeof(a)/sizeof(a[0]); ++n)
{
square = a[n]*a[n];
sum += square;
printf("%g ----> %g\n", a[n], square);
}
printf("sum of squares: %g\n", sum);
return 0;
}
gives:
0.035679 ----> 0.00127299
0.079935 ----> 0.0063896
0.03332 ----> 0.00111022
0.042424 ----> 0.0017998
0.012387 ----> 0.000153438
sum of squares: 0.0107261
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Are you needing the sqaure or the square root?
PS and the sum of your numbers is 0.010726.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
|
|
|
|
|
Actually i need square but its solve i got it but i am not able to get the summation value of the square number correctly .plz help me out..
|
|
|
|
|
While migrating VC++ code from VS6 (1998) to VS5(later year) I had to remove __STDC__ from project properties-Predecessors. It fix lot of 'tagVariant' related errors.
if i have __STDC__ in project properties - preprpcessors
I get this
xyz.cpp
c:\program files (x86)\microsoft visual studio 8\vc\atlmfc\include\atlcomcli.h(443) : error C2039: 'vt' : is not a member of 'tagVARIANT'
c:\program files (x86)\microsoft visual studio 8\vc\platformsdk\include\oaidl.h(424) : see declaration of 'tagVARIANT'
c:\program files (x86)\microsoft visual studio 8\vc\atlmfc\include\atlcomcli.h(443) : error C2039: 'vt' : is not a member of 'tagVARIANT'
Next what happened is function definitions were not recognized in many of the project file. so I added #define
__STDC__ in problem files which also took care of many unrecognized function definition related errors.
Now the project is left with only one error in compiling one file which says "Fetal error (1017) invalid integer constant expression in ctype.h" and the code its pointing to line number 362 in ctype.h #define !__STDC__ etc Any ideas - suggestions ? Pls help
I tried to - Remove any #define __STDC__ that I added locally. Also remove __STDC__ from Preprocessor definitions if you haven't done so already. Selected all the .c files in the solution explorer and select properties in the solution explorer context menu. In the properties dialog select /Za for C/C++\Language\Disable Language Extensions property.
Which took me back to -> error C2065: ‘xyz’: undeclared identifier : error C2182: ‘pqr’ : illegal use of type 'void' : error C2065: ‘some variable’ : undeclared identifier : error C2146: syntax error : missing ';' before identifier ‘classname’ : error C2143: syntax error : missing ';' before '*' : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int : error C2040: ‘variable2’ : 'int *' differs in levels of indirection from ''unknown-type'' . . .
Reference : http://social.msdn.microsoft.com/Forums/en-US/e68b0af5-ed25-4953-80e7-e88463149b77/fatal-error-1017-invalid-integer-constant-expression-in-ctypeh-during-migration-from-vs6-to-vs5?forum=vcgeneral
modified 28-Jul-14 9:44am.
|
|
|
|
|
__STDC__ is defined by the Microsoft compiler when compiling C files with the /Za option. It is never defined when compiling C++ files. You should not define it somewhere in your code or as project global definition, or edit any system header files.
If you have a mixed setup of C and C++ files, you can try to rename the C files to *.cpp to force compilation as C++.
Finally you have to fix all errors in your source code by inspecting them and take the appropiate actions.
|
|
|
|