Class Question

Started by
11 comments, last by Esap1 23 years, 10 months ago
Can I do this? class Object_class { int hi; }; int LoadFile(char *file,Object_class *Object) { //Is this ok? Object = new Object(10); //Those are brackets } void Whatever() { Object_class *Object; LoadFile("hi.txt",Object); } ---------------------------------------------------- Can I do that, it doesnt seem to work. Thanks for the help.
Advertisement
Ok First, use the SOURCE brackets, like this:
    class Object_class {    int hi;};int LoadFile(char *file,Object_class *Object){    //Is this ok?    Object = new Object(10); //Those are brackets    //It should be this :  //Object = new Object_class[10];    // I think this is why you have an error.}void Whatever(){    Object_class *Object;     LoadFile("hi.txt",Object);}    


Try this and if you have an error again, tell us what is exactly the error it gives to you.

Programming is:A.The art of debugging a blank sheet of paper (or an empty file).B.A pastime similar to banging one's head against a wall, but with fewer opportunities for reward.C.The most fun you can have with your clothes on (although clothes are not mandatory).
Now I know what I'm made of, and I'm afraid of it...
quote: Original post by Esap1

Can I do this?
class Object_class {
int hi;
};
int LoadFile(char *file,Object_class *Object)
{
//Is this ok?
Object = new Object(10); //Those are brackets
}

void Whatever()
{
Object_class *Object;
LoadFile("hi.txt",Object);
}


----------------------------------------------------
Can I do that, it doesnt seem to work. Thanks for the help.



Should get it working with this (if I understand your problem right..):
        int LoadFile(char *file,Object_class **Object){    *Object = new Object_class[10]; }void Whatever(){ Object_class *Object;  LoadFile("hi.txt",&Object);}        



Edited by - _2Late_ on June 15, 2000 12:00:15 AM
- It''s never 2 late

Its not a compiling error, its just it doesnt work. What Im trying to do is I have a MAP class that holds a number of Object Classes. What I want to do is have my LoadASE() function be able to alocate the right number of Object classes in the Map class and then load the data into the object classes.

class Map_class {
Object_class *Object;//An array of Objects
};

int LoadASE(char *file, Object_class *Object)
{
//Find out how many object are in the file
FindObjects();
Object = new Object_class[numObjects];
}


Im quite sure on this, i cant get to a mashine with vc6++ right now to test it.


void Whatever()
{
Object_class *whateverObject;
LoadFile("hi.txt",whateverObject);
}

int LoadFile(char *file,Object_class *loadfileObject)
{
//Is this ok?
loadfileObject = new Object(10); //Those are brackets
}


you are sending what whateverObject pointers points to, to the loadFile call. But when you move the loadfileObject pointer (with new), you DONT move the whateverObject pointer, it still points to the memory area it did from the start.

So here are three posibilities
1: do the thing with double pointers.
2: return the pointer
[source]
Object_class * LoadFile(char *file)
{
Object_class * loadfileObject;
loadfileObject = new Object(10); //Those are brackets
....
...
..
return loadfileObject;
}

void Whatever()
{
Object_class *whateverObject;
whateverObject = LoadFile("hi.txt");
}

return NULL for error.

3:
send a pointer to the Map_class object


class Map_class {
Object_class *Object;//An array of Objects
};

int LoadASE(char *file, Map_class *Map)
{
//Find out how many object are in the file
Map->Object = new Object_class[numObjects];
}

void Whatever()
{
Map_class Map;
LoadFile("hi.txt", &Map);
}

Edited by - claus hansen ries on June 16, 2000 3:13:18 AM


Edited by - Claus Hansen Ries on June 16, 2000 3:15:01 AM
Ries
quote:Original post by Esap1
Its not a compiling error, its just it doesnt work. What Im trying to do is I have a MAP class that holds a number of Object Classes. What I want to do is have my LoadASE() function be able to alocate the right number of Object classes in the Map class and then load the data into the object classes.

class Map_class {
Object_class *Object;//An array of Objects
};

int LoadASE(char *file, Object_class *Object)
{
//Find out how many object are in the file
FindObjects();
Object = new Object_class[numObjects];
}



Your problem has to do with a common misunderstanding of how parameters are handled. I understand it but have a difficult time explaining it effectively, so maybe some sample code will illustrate:

    void by_value(int x) {   x = 1;}void by_pointer(int* px) {   *px = 2;}void by_new_pointer(int* px) {   px = new int;   *px = 3;}void by_new_double_pointer(int** ppx) {   *ppx = new int;   **ppx = 4;}void by_reference(int& rx) {              // C++ only   x = 5;}void by_pointer_reference(int*& rpx) {    // C++ only   rpx = new int;   *rpx = 6;}int main(){   int a = 0, *b = &a   printf("%d %d\n", a, *b);   //output: 0 0   by_value(a);   printf("%d %d\n", a, *b);   //output: 0 0   by_pointer(b)   printf("%d %d\n", a, *b);   //output: 2 2   by_new_pointer(b);   printf("%d %d\n", a, *b);   //output: 2 2   by_new_double_pointer(&b);   printf("%d %d\n", a, *b);   //output: 2 4   delete b;   //ok, b not pointing at a   b = &a   by_reference(a);   printf("%d %d\n", a, *b);   //output: 5 5   by_pointer_reference(b);   printf("%d %d\n", a, *b);   //output: 5 6   delete b;   //ok, b not pointing at a   return 0;}    


Also note that the call to by_new_pointer() has leaked memory which cannot be recovered.



---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
you could just return the Object_Class pointer from LoadFile()

eg:

Object_class* LoadFile(const char* pszFilename)
{
Object_class* pArray = new Object_Class[10];

return pArray;
}

this way, NULL is returned if the newing failed

then you say:

void Whatever()
{
Object_class* p = LoadFile("some.map");

if(p == NULL)
cout << "HANDLE THE ERROR!";
}
What about doing this.

class Map_class {
Object_class *Object;
void LoadFile(char *file);
};




void Map_class::LoadFile(char *file)
{
Object = new Object_class[numObject];
//Load in the stuff
}


Is there a way I can get it to work when the function is in Map_class, It seems a more logical place to put it.

Edited by - Esap1 on June 17, 2000 10:50:58 PM
Should it work???
no, you cant.
when u use new, you need a variable type, not a perticular variable

it should be:

int LoadFile(char *file,Object_class *Object)
{
//Is this ok?
Object = new Object_class[10]; //Those are brackets
}



- Infested Furby

infestedfurby.cjb.net







Can I do this?

class Object_class {
int hi;
};

int LoadFile(char *file,Object_class *Object)
{
//Is this ok?
Object = new Object(10); //Those are brackets
}

void Whatever()
{
Object_class *Object;
LoadFile("hi.txt",Object);
}


----------------------------------------------------
Can I do that, it doesnt seem to work. Thanks for the help.



This topic is closed to new replies.

Advertisement