Passing stuff between objects

Greetings!

I am having problems with passing stuff between. This is what I have.
Keep in mind this is translated partly from spanish. There is no "collision" or problems with reserved words. "list" is a class written by us before and included in both ClassA and ClassB.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

//in "ClassA.h"
class ClassA
{
private:
       list<string*>* objectA;
public:
       A();
       list<string*>* methodA();
}

//in "ClassA.cpp"
A::A()
{
   this->objectA=new list<string*>*;
}

void A::methodA()
{
   ClassB objectB;
   string filename="worksheet v1.txt";

   (this->objectA)=objectB.get(filename);
}


//in "ClassB.cpp"
list<string*>* ClassB::get(string filename)
{
   list<string*>* extraList;
   
   ifstream worksheet;
   worksheet.open(filename.c_str());
   if (worksheet != NULL)
   //other methods and declarations

   return extraList;
}


My problem is that once it has all ran, objectA is empty (I check this via cout). How can I fix it?


Also sorry if the names are not descriptive. I am not good for names and getting the problem out of context, with the original variable names would've made it even more confusing.
Can you show more of the ClassB::get method?
I dont see any manipulation on 'extraList' before passing it to objectA
Last edited on
> I check this via cout
maybe your check was wrong, can't tell as you haven't provided it http://www.eelis.net/iso-c++/testcase.xhtml
Also, consider using a debugger to step through your code and watch your variables.

You haven't initialized `extraList' in `ClassB::get()'


> (this->objectA)=objectB.get(filename);
that's a memory leak, you forgot to deallocate what `objectA' was pointing.
You wouldn't have that kind of issues if you use objects instead of pointers ¿why are you using pointers?


> Passing stuff between objects
you don't seem to be using the object state in `ClassB::get()', ¿why do you need an object?
Aye, how do I quote?

Bigorenski:

The file contains "paths" (lists of strings), and extraList is supposed to store said paths after they are lifted from the .txt file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

    list<string*>* extraList;

    ifstream worksheet;
    worksheet.open(filename.c_str());
    if (worksheet != NULL)
    {
        string line,lineSubs;
        while(!worksheet.eof())
        {
            Lista<string>* currentPath;
            getline(worksheet,line);

            //if it's the start of a path
            if (((line != "")) and (line.substr(0,2)=="PP"))
            {
                currentPath = new List<string>();
                currentPath->add(line);
            }

            //if not a comment or end of path
            else if (((line.substr(0,2)!= "PL"))and (line.substr(0,2)!= "--"))
                currentPath->add(line);

            //if path is over
            if(line.substr(0,2) == "PL")
            {
                currentPath->add(line);
                extraList->add(currentPath);
            }
        }
    }
    worksheet.close();
    return extraList;



ne555:
Sorry, I mis-copied it when translating.
The correct line is:
list<string*>* extraList = new list<string*>;

As for the memory leak, I do not quite understand you.
It's not pointing to anything before it calls objectB.get, and is empty after the constructor of ClassA is used. Where is the error?



As for the last point... Three newbies to objects and C++ making a multi-tasking program. I am the least instructed of the three, so I can't quite tell you about design decisions, honestly. It's like it is, and it worked before I tried to split the main file in parts. It's upwards of 2k lines of code so I can't rewrite it from scratch.
See this, maybe it'll give you a better idea of what I am aiming to do:

http://s678.photobucket.com/user/SpiralPegasus/media/Problema_zps54d3e5e7.png.html
Last edited on
If worksheet=NULL you'll return an empty extraList to objectA, check for that.

Add this: else cout<<"worksheet = NULL!"

And do some tests on extraList before returning it.
Last edited on
Doesn't show up. The file isn't empty - that much it's interpreting correctly.

What I did notice (and also noticed, didn't write here) is that objectB is declared twice - once in methodA, and another in the private part of ClassA.

I may know little but I know that can't be right.
If I eliminate either, the program compiles, runs, and crashes.

It's meant to be local to the process. How could I fix that?

Add this to ClassB::get() list<string*>* extraList = new list<string*>;
Last edited on
It's in my previous reply, where I responded to ne555 :)

Thanks for all the help this far.
> As for the memory leak, I do not quite understand you.
> It's not pointing to anything before it calls objectB.get,
> and is empty after the constructor of ClassA is used. Where is the error?
It is pointing to something. in the constructor you do this->objectA=new list<string*>*;, that the list does not have any elements is irrelevant you have reserved memory for the list.

run your program through valgrind


> What I did notice (and also noticed, didn't write here) is that objectB is declared twice
> once in methodA, and another in the private part of ClassA.
it is not declared twice, those are two different objects
I'll insist, please provide a testcase http://www.eelis.net/iso-c++/testcase.xhtml it should show how your class are defined and how they are used.

> If I eliminate either, the program compiles, runs, and crashes.
http://www.cplusplus.com/forum/general/112111/


> "list" is a class written by us
¿are you sure that that works? ¿would it be too much trouble to replace it with std::list?


> See this, maybe it'll give you a better idea of what I am aiming to do:
Ni por asomo. O bien la solución me parece trivial, o bien no entendí el problema.
Tampoco entiendo por qué utilizar una image cuando simplemente podrías escribir el pseudocódigo.
(TN: No, it doesn't help)
Topic archived. No new replies allowed.