I want to use object M in the function body but It dose not implement,when I create another object from MobileNode class and equal two objects again it dose not implement, what is wrong with it?
==================================================================
void MacTdma::NodeLocation(MobileNode* M)
{
double a=0.0;
a=M->X();
printf("the X position is: %f \n ",a);
}
==================================================================
void MacTdma::NodeLocation(MobileNode* M)
{
MobileNode* m3=new MobileNode;
m3=M;
double a=0.0;
a=m3->X();
printf("the X position is: %f \n ",a);
}
If M is null (C++ doesn't recognise "nil"; if your pointer is deliberately set to not be pointing at something, it is a NULL pointer), then you never set it to point at a MobileNode object. Make it point at the appropriate MobileNode object.
You need to create an object of type MobileNode, and then if you want a pointer to that object, create a pointer of type MobileNode* and then make that pointer point at the MobileNode object.
I suspect that you actually don't need to use pointers at all for what you're trying to do. I also suspect that you don't understand pointers. Here's something that explains them in simple terms: http://www.cplusplus.com/articles/EN3hAqkS/
void MacTdma::NodeLocation( MobileNode* M )
{
double a = 0.0;
a = M->X();
printf( "the X position is: %f \n ", a );
}
However:
* We don't know whether MobileNode has member function X().
* You don't check whether the pointer is null (nullptr).
* You do use C-library's I/O, rather than C++ iostreams.
* Unnecessary statements. Why initialize to 0.0 and then assign a new value?
1 2 3 4 5 6 7
void MacTdma::NodeLocation( MobileNode* M )
{
if ( M ) {
double a = M->X();
printf( "the X position is: %f \n ", a );
}
}
Or
1 2 3 4 5 6
void MacTdma::NodeLocation( MobileNode* M )
{
if ( M ) {
std::cout << "the X position is: " << M->X() << '\n';
}
}
The other one is a horror story:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void MacTdma::NodeLocation(MobileNode* M)
{
MobileNode* m3 // you declare a local variable m3 that is a pointer
= new MobileNode; // you create object in free store
// then you initialize the pointer m3 with the address of the object
// that is ok in itself
m3 = M; // you assign address stored in M into m3
// this overwrites the address that the m3 was initialized with
// you now longer have the address of the object that is in free store
// you have "leaked" the memory of the object
double a=0.0;
a=m3->X();
printf("the X position is: %f \n ",a);
}
We could make that shorter:
1 2 3 4 5 6 7 8 9 10
void MacTdma::NodeLocation( MobileNode* M )
{
new MobileNode; // useless allocation of memory,
// construction of object,
// and a leak
double a = 0.0;
a = M->X();
printf( "the X position is: %f \n ", a );
}
If you would like to copy value from object pointed to by M into object pointed to by m3, you would:
1 2 3 4 5 6 7 8 9 10 11 12
void MacTdma::NodeLocation(MobileNode* M)
{
if ( M ) {
MobileNode* m3 = new MobileNode( *M ); // copy construction
// or
*m3 = *M; // copy assignment of dereferenced objects
double a = m3->X();
printf("the X position is: %f \n ",a);
delete m3; // deallocate memory of the object
}
}