
-----------------------------------
hamid1455
Wed Mar 27, 2013 2:09 pm

modify members of a struct
-----------------------------------
I have this program, which is in file called learningc.cpp:

#include 
#include 

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.

-----------------------------------
Panphobia
Wed Mar 27, 2013 3:06 pm

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.

-----------------------------------
Tony
Wed Mar 27, 2013 4:30 pm

RE:modify members of a struct
-----------------------------------

tony@machine:/tmp$ cat test.cpp 
#include  
#include  

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


-----------------------------------
hamid1455
Wed Mar 27, 2013 5:02 pm

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..

-----------------------------------
Cancer Sol
Wed Mar 27, 2013 6:14 pm

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:

your code here

[/code]
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.

-----------------------------------
Cancer Sol
Fri Mar 29, 2013 1:25 pm

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?

#include  
using namespace std; 

struct poo
{
    int volume; 
}; 

int main() 
{ 
    poo balls; 
    balls.volume = 45; //The first value
    balls.volume = 42; //Modded value
} 

That would be fine, it would work like an int variable, for example.

#include 
using namespace std;

int main()
{
    int pooballs = 45;
    cout 