Computer Science Canada modify members of a struct |
Author: | hamid1455 [ Wed Mar 27, 2013 2:09 pm ] |
Post subject: | modify members of a struct |
I have this program, which is in file called learningc.cpp: #include <iostream> #include <string> using namespace std; struct poo { int volume; }; int main() { poo balls; balls.volume = 45; return 0; } However it won't let me modify balls.volume, visual studio says: a non-static member reference must be relative to a specific object I have no idea what that means. Any help is appreciated. |
Author: | Panphobia [ Wed Mar 27, 2013 3:06 pm ] |
Post subject: | RE:modify members of a struct |
It is working totally fine for me, I copied and pasted your code into netbeans ide, and no errors, everything did what it was supposed to do. I have never tried Visual Studio but I can vouche for netbeans. |
Author: | Tony [ Wed Mar 27, 2013 4:30 pm ] |
Post subject: | RE:modify members of a struct |
Quote: tony@machine:/tmp$ cat test.cpp #include <iostream> #include <string> using namespace std; struct poo { int volume; }; int main() { poo balls; balls.volume = 45; return balls.volume; } tony@machine:/tmp$ g++ test.cpp tony@machine:/tmp$ ./a.out tony@machine:/tmp$ echo $? 45 |
Author: | hamid1455 [ Wed Mar 27, 2013 5:02 pm ] |
Post subject: | RE:modify members of a struct |
a struct isn't const, but sometimes it gives me that error when I try to modify one of its members. I just tried struct poo and it works when I try to change its members' values, maybe I had a syntax error somewhere else.. |
Author: | Cancer Sol [ Wed Mar 27, 2013 6:14 pm ] | ||
Post subject: | Re: modify members of a struct | ||
*Off topic* Ahemm really? A structure called Poo? Ohmagawd, poo balls? o_O Change the names to make it look better Sorry for being so immature though x.x Anyways, next time when you post a code, use this:
It'll make longer codes easier to read, and it looks better. Sometimes when I see long codes without it, I don't even bother looking at it. |
Author: | Cancer Sol [ Fri Mar 29, 2013 1:25 pm ] | ||||
Post subject: | Re: modify members of a struct | ||||
Can you please show how you're trying to change it to so that we know what's going on? The code right now is fine, which I don't think you're talking about the posted code causing problems. Are you trying to do something like this?
That would be fine, it would work like an int variable, for example.
I'm not really sure what your problem is, but I'm just guessing it might be that since it kinda sounds like it P.S. you don't need that string header file since there's only integers (and poo). |