Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 modify members of a struct
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hamid1455




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Panphobia




PostPosted: 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.
Tony




PostPosted: 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
hamid1455




PostPosted: 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..
Cancer Sol




PostPosted: 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 Smile
Sorry for being so immature though x.x

Anyways, next time when you post a code, use this:
code:

[syntax="cpp"]
your code here
[/syntax]

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




PostPosted: 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?
c++:

#include <iostream>
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.
c++:

#include <iostream>
using namespace std;

int main()
{
    int pooballs = 45;
    cout << pooballs << endl; //It outputs 45
    pooballs = 42;
    cout << pooballs << endl; //This outputs 42.
}

I'm not really sure what your problem is, but I'm just guessing it might be that since it kinda sounds like it Razz

P.S. you don't need that string header file since there's only integers (and poo).
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: