Errors with ->*
Hello -- long time since I posted on this list.
I'm trying to understand the use of ->* but in compiling the attached program which I thought was straightforward I am getting the following errors:
foo.cpp: In constructor ‘mystruct::mystruct()’:
foo.cpp:9:12: error: invalid conversion from ‘int*’ to ‘int’ foo.cpp: In function ‘int main()’:
foo.cpp:21:7: error: ‘xptr’ was not declared in this scope foo.cpp:22:7: error: ‘yptr’ was not declared in this scope
Please help.
Signature · 1 line
Shriramana Sharma
struct mystruct
{
public:
int x, y ;
int * xptr, yptr ;
mystruct ()
{
xptr = & x ;
yptr = & y ;
}
} ;int main ( void )
{
mystruct a ;
mystruct * aptr = & a ;
a . x = 1 ;
a . y = 2 ;
a .* xptr = 3 ;
a .* yptr = 4 ;
aptr ->* xptr = 5 ;
aptr ->* yptr = 6 ;
}1) a .* xptr = 3 ;
Above line's syntax is wrong.
Should be
*a.xptr = 3;
2) Also declare the structure member variables as
int *xptr, *yptr ; (otherwise yptr will be of type int, not of int*)
3) aptr ->* xptr = 5 ;
is also of wrong syntax.
Should be
*aptr->xptr = 5 ;
Which is equivalent to
*(aptr->xptr) = 5;
the above code DOES NOT mean this one :
(*aptr)->xptr // this one dereference aptr first, then
does nothing to xptr
Thanks.
Quoted 11 lines · Shriramana Sharma
Hello -- long time since I posted on this list.
I'm trying to understand the use of ->* but in compiling the attached program which I thought was straightforward I am getting the following errors:
foo.cpp: In constructor ‘mystruct::mystruct()’:
foo.cpp:9:12: error: invalid conversion from ‘int*’ to ‘int’ foo.cpp: In function ‘int main()’:
foo.cpp:21:7: error: ‘xptr’ was not declared in this scope foo.cpp:22:7: error: ‘yptr’ was not declared in this scope
Quoted 4 lines
Please help.
--
Shriramana Sharma
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
typo correction :
(*aptr).xptr = 5;
Quoted 21 lines · Aniruddha Bhattacharyya
1) a .* xptr = 3 ;
Above line's syntax is wrong.
Should be
*a.xptr = 3;2) Also declare the structure member variables as
int *xptr, *yptr ; (otherwise yptr will be of type int, not of int*)3) aptr ->* xptr = 5 ;
is also of wrong syntax.
Should be
*aptr->xptr= 5 ;Which is equivalent to
*(aptr->xptr) = 5;the above code DOES NOT mean this one :
Quoted 24 lines
(*aptr)->xptr // this one dereference aptr first, then does nothing to xptr
Thanks.
Quoted 15 lines · Shriramana Sharma
Hello -- long time since I posted on this list.
I'm trying to understand the use of ->* but in compiling the attached program which I thought was straightforward I am getting the following errors:
foo.cpp: In constructor ‘mystruct::mystruct()’:
foo.cpp:9:12: error: invalid conversion from ‘int*’ to ‘int’ foo.cpp: In function ‘int main()’:
foo.cpp:21:7: error: ‘xptr’ was not declared in this scope foo.cpp:22:7: error: ‘yptr’ was not declared in this scopePlease help.
--
Shriramana Sharma
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Line 9 should be:
int * xptr, * yptr ;
You forgot a '*'. About the rest, you are using the pointer to members incorrectly, look at
http://www.google.com/search?q=pointer+to+member for examples and usage. Darío
Quoted 12 lines · Shriramana Sharma
Hello -- long time since I posted on this list.
I'm trying to understand the use of ->* but in compiling the attached program which I thought was straightforward I am getting the following errors:
foo.cpp: In constructor ‘mystruct::mystruct()’:
foo.cpp:9:12: error: invalid conversion from ‘int*’ to ‘int’ foo.cpp: In function ‘int main()’:
foo.cpp:21:7: error: ‘xptr’ was not declared in this scope foo.cpp:22:7: error: ‘yptr’ was not declared in this scope
Quoted 5 lines
Please help.
--
Shriramana Sharma
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 4 lines · Shriramana Sharma
I'm trying to understand the use of ->* but in compiling the attached program which I thought was straightforward I am getting the following errors:
Quoted 15 lines
foo.cpp: In constructor ‘mystruct::mystruct()’:
foo.cpp:9:12: error: invalid conversion from ‘int*’ to ‘int’ foo.cpp: In function ‘int main()’:
foo.cpp:21:7: error: ‘xptr’ was not declared in this scope foo.cpp:22:7: error: ‘yptr’ was not declared in this scopePlease help.
Signature · 6 lines
Shriramana Sharma struct mystruct { public: int x, y ; int * xptr, yptr ;
If these are supposed to have pointer-to-member type, the correct syntax is:
int mystruct::* xptr, mystruct::* yptr;
Quoted 5 lines
mystruct ()
{
xptr = & x ;
yptr = & y ;
}There's no point in using pointer-to-member instance variables like this. They're normally used when you need to identify a specific field within a class (rather than an instance). If you want to point to a specific member of a specific instance; you would just use plain pointers.
Quoted 5 lines
a .* xptr = 3 ; a .* yptr = 4 ; aptr ->* xptr = 5 ; aptr ->* yptr = 6 ;
xptr and yptr are members of mystruct, so you need e.g.:
aptr ->* aptr->xptr = 5 ;
aptr ->* aptr->yptr = 6 ;
a .* a.xptr = 3 ;
a .* a.yptr = 4 ;The whole point about pointer-to-member is that the value is the same for all instances of the class, so you could just do:
int main ( void )
{
int mystruct::* xptr = &mystruct::x;
int mystruct::* yptr = &mystruct::y;
mystruct a, b;
mystruct *aptr = &a;
a .* xptr = 1 ;
a .* yptr = 2 ;
b .* xptr = 1 ;
b .* yptr = 2 ;
aptr ->* xptr = 1 ;
aptr ->* yptr = 2 ;
}In any case, given that this isn't specific to Linux, and has nothing to do with C, there are better places for asking such questions than the linux-c-programming list (e.g. the comp.lang.c++ newsgroup).
Signature · 5 lines
Glynn Clements <[email protected]> -- To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Thanks all for your replies. I now have a better idea of the things. And Glynn's note about comp.lang.c++ is noted.
Signature · 1 line
Shriramana Sharma