| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Seg fault writing to an object
Im storing data in an object by passing it to a method of the class. (is that what you say?).
The debuger is happy right through passing values to the function but raises the error when it moves on to creating the data storage bits of the object. i.e. string letter and string name. What is going on? class AminoAcids { public: string getData(string pLetter, string pName) { letter = pLetter; name = pName; } //error here string letter; string name; }; int main(int nNumberofArgs, char* pszArgs[]) { //now passing arguments to a function rather than the constructor AminoAcids array[4]; for (int i = 0;i < 4;i++) { string x = "v"; string y = "val"; array[i].getData(x , y); } } |
|
#2
|
||||
|
||||
|
If you dont mind me asking kolokol what are you trying to accomplish with this code(are you making a registry or database for Amino acids)? NVM im gonna test it with my compiler so scratch that last question if you read it already =Þ
Last edited by Geo.Garnett : September 1st, 2005 at 03:12 PM. Reason: Scratched a question |
|
#3
|
|||
|
|||
|
It looks okay, besides some compiler warnings (GetData should return a value).
Personally I would never use the string datatype (I'm more the char kinda type), but that is just me... When you single step from the beginning, at what value of i does the error occure? |
|
#4
|
||||
|
||||
|
Illegal referance to a memory location!!!!
The program says that you make an illegal referance to a memory location. Well thats what visual studio says. I would think of re-writing the code itself. If you let me know of what you are trying to acomplish I could possibly help figure this out with you.
|
|
#5
|
|||
|
|||
|
Im trying to use the class amino acids to store the properties of aminoacids. Each object will store a letter, a name and another letter. Eventually the idea is to have it read these properties from a text file. At the moment im just trying to get it to accept the values i specify. Im trying to write the program bit by bit so i get a better idea of how to do the basics.
Thanks for your help! (as an aside and unrelated to the current problem, is it possible to get the program to create an object with a name you specify? I was thinking of getting it to use a letter e.g. v as the name of the object, then i could use v.name = val and so on. I couldn't figure out how to get the program to create objects in this way, e.g. it reads the first letter, creates an object of that name and goes on to read in the associated properties. Is that even possible? Because i couldn't do this i used an array of objects with an arbitrary index) |
|
#6
|
||||
|
||||
|
kolokol
well, to tell you the truth I am not that good at I/O stuff and MichaelSoft is much more knowledgeable at this than I but I'm gonna go over some I/O stuff in my book, because in helping you I can always learn something that's for sure and in the mean time I might be able to help you out. I understand the basics but I just have never had the need to use it yet. It almost seems as if you could make it a little simpler by just writing the data of the Amino Acids into your program and just eliminate the text file all together, unless this is just for practice sake, or if the file needs to be updated frequently. So if this is not time sensitive then let me take a day to read up and get back to ya. |
|
#7
|
|||
|
|||
|
Yeah this is just for practice. Ive got a book which goes through I/O stuff also, but im not quite ready to write the I/O system until i get the code for storing the data right. Ill post my efforts as soon as i solve this problem. Is my basic approach wrong? It looks ok to me but if theres a better way please please tell me.
This problem is really getting me now, I can't figure out whats wrong. Its so frustrating to have it compile just fine and not work for a reason that is not apparent to me. Please could somone give me a hand writing something that works. |
|
#8
|
|||
|
|||
|
The following works in my Visual C++ .Net as an Win32 Console Application:
Code:
#include "stdafx.h"
#include "string.h"
using namespace std;
class AminoAcids
{
public:
void getData(string pLetter, string pName)
{
letter = pLetter;
name = pName;
}
//error here
string letter;
string name;
};
int _tmain(int argc, _TCHAR* argv[])
{
//now passing arguments to a function rather than the constructor
AminoAcids array[4];
for (int i = 0;i < 4;i++)
{
string x = "v";
string y = "val";
array[i].getData(x , y);
}
return 0;
}
Basically I changed the return type of the class function. To answer your 'as an aside and unrelated to the current problem' question: NO, it is not possible to create objects and naming them in run-time. This is because C++ is not a scripting language. The compiler needs to know all variable/object names at design-time, give them all a number/place in memory and the linker puts it all together. So in run-time there are no names at all! Only memory-addresses... You need to solve this in the class itself. You can either develop this yourself, or (if using Visual C++) use a container class. Regards |
|
#9
|
|||
|
|||
|
Doh!
^^probably the only possible comment. I went through various attempts at workarounds but decided to blame it to blame it working on different causes. Thank you MichaelSoft! I would be lost without you. Geo.Garnett im just starting to work on the I/O stuff ill post it when i have something fit to be seen. Thank you both! |
|
#10
|
||||
|
||||
|
Ya sorry I couldnt have been more help, but I just havent spent a lot of time with IO stuff, I have been concentrating most of my time on windows applications lately and I dont think my little brain can hold that much information at one time, I think I need a bigger hard drive or something =O lol. I mean I understood it somewhat at one point but then since I didnt use it for a while its like I have to learn it all over again if you know what I mean... =\
|
|
#11
|
|||
|
|||
|
Right ive had a crack at input and output from a file. Amazingly it seems to work
. Ive also linked it up with my class and its writing to and from it nicely. As i am posting here though there are ofc some small niggles, though not worth starting a new thread over. I have seen the setw function used to set the width of input on stuff like cin. How do i apply this to a filestream? At the moment im using whatever.getline(pbuffer, storeforthedata, 3). Three is the size of the charachter array storeforthedata. I was hoping this would effectively set the width of the input to two. But trying to set how many characters it reads like this dosen't work in all cases though. Is there a better way of doing this? Next on the menu is cleaning up the code and making aminoacids fully self sufficient, i might even start throwing errors around. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Seg fault writing to an object |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|