Invalid Use of Member (Did you forget the '&'?)

Started by
5 comments, last by Inmate2993 18 years, 6 months ago
I have gotten far in my program that I'm making an am now stuck with this line of code:
if (Script.substr(Pos, GTAddress.length + 1) == GTAddress + ":")
My log says:
C:\CPP\PowerScript\Compiler.cpp:46: error: invalid use of member (did you forget the `&' ?)
Not sure about this, and I never have read anything about having a & symbol. Script and GTAddress are strings.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Advertisement
GTAddress.length(), maybe?
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Yeah, that worked out. If you can't figure out a problem, try the parentheses! Thanks.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
no. You need to learn the basics. Throwing symbols at bugs may work sometimes but it is a horrible, horrible, horrible, horrible way to code. Yes, all four "horribles" are needed. I strongly recommend that you purchase of "The C++ Programming Language" by Bjarne Stroustrup, the creator of C++. I also strongly recommend that you stop adapting example source code, as I suspect that is how you are teaching yourself.
Now let's pretend you didn't have the internet and were without the friendly people on gamedev. So now we'll do a basic lesson on problem solving. So you're given a compiler error that you can't figure out, but you are told what line it's on. You've got a pretty long statement there, so you can't figure out exactly what the compiler is complaining about. So one way to tackle it is to break the statement down to smaller parts and see which part the compiler doesn't like. In this case you might try:

std::string sub = Script.substr(Pos, GTAddress.length + 1);
if (sub == GTAddress + ":")

Now the compiler error migrates up the line. So try to break that down further.

int len = GTAddress.length + 1;
std::string sub = Script.substr(Pos, len);

Compiler error migrates up a line again. Now you'd probably see for yourself what the problem was.
Quote:Original post by Glak
I also strongly recommend that you stop adapting example source code, as I suspect that is how you are teaching yourself.


For your information, I actually GOOGLED it first, second, without finding any help, I TRIED to fix the code. I didn't just throw a bunch of symbols at it, I messed around with it and figured out that it had to be something to do with the length member of the string, but the compiler told me that it had something to do with a ampersand, AND NOT PARENTHESES. I read PLENTY of sources to learn my coding, and I am still a newbie in training. I don't have the money to shell out for a new book every time someone recommends it to me.

And, SiCrane, thanks for the suggestion, as I will try that next time.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Quote:Original post by orcfan32
I have gotten far in my program that I'm making an am now stuck with this line of code:
if (Script.substr(Pos, GTAddress.length + 1) == GTAddress + ":")

My log says:
C:\CPP\PowerScript\Compiler.cpp:46: error: invalid use of member (did you forget the `&' ?)

Not sure about this, and I never have read anything about having a & symbol.

Script and GTAddress are strings.


GTAddress + ":" ? Does the object have operator+ defined?

If so, try parenthesis around it, so that the == doesn't take precidence in the order of operations. This is one of those situations where if (a&&b==c&&d) is interpreted by the compiler as if(a&&(b==c)&&d) when you probably meant if((a&&b)==(c&&d)).
william bubel

This topic is closed to new replies.

Advertisement