CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2009
    Posts
    14

    [RESOLVED]Storing Objects in an Array

    Hi,

    Thanks for the help earlier on on another problem, now for a new one....

    I wish to store objects (I think I actually mean object pointers, but I am sure you will tell me if I'm wrong) in an array. Through my searching people seem to suggest the following:


    MyClass *test[10];
    test[0] = new MyClass();
    test[1] = new MyClass();

    // etc....

    test[0]->add_info("foo");
    test[1]->add_info("bar");

    // etc....

    cout << "Info for test0: " << test[0]->get_info();
    cout << "Info for test1: " << test[1]->get_info();



    Now, I have tried this and essentially each time I do an add_info call, it replaces my existing data - regardless of me choosing a different array index. (So, a test[1] add_info call, replaces the data in test[0] as well as test[1])

    I'm sure that I am doing something utterly stupid here, would anyone mind telling me where I am going wrong??
    Last edited by skycrazy123; February 24th, 2009 at 12:02 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Storing Objects in an Array

    What is the definition of the MyClass class? You might want to post the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2009
    Posts
    14

    Re: Storing Objects in an Array

    The MyClass header file is as follows:

    #include "stdafx.h"
    #include <iostream>
    #include <string.h>
    using namespace std;

    class MyClass
    {
    public:

    void add_info(std::string);
    std::string get_info();

    };


    the implementation file:


    #include "stdafx.h"
    #include <iostream>
    #include "ProductInfo.h"
    #include <string.h>
    using namespace std;

    std::string info;

    void MyClass::add_info(std::string input_info) {
    info = input_info
    }


    std::string MyClass::get_info() {
    return info;
    }




    So its basically that stuff above..

    The original code posted being from the driver program.

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Storing Objects in an Array

    You probably don't really need to use pointers.
    Code:
    MyClass test[10]; // Declare 10 MyClass objects, each constructed with the default constructor.
    
    test[0].add_info("foo");  
    test[1].add_info("bar");
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Storing Objects in an Array

    You have a global std::string info;
    Declare it in MyClass.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Feb 2009
    Posts
    14

    Re: Storing Objects in an Array

    No Dice.

    The same problem occurs.

    the call's:

    cout << test[1].get_info() << "\n";
    cout << test[0].get_info() << "\n";

    both return "bar"

    In response to your other reply: Declare it in the implementation or the header?? (it already exists in the implementation)

  7. #7
    Join Date
    Feb 2009
    Posts
    14

    Re: Storing Objects in an Array

    I declared them private in the header, and it seemed to work. I'll test it with the rest and get back to you

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Storing Objects in an Array

    You can remove the global one in the implementation file to avoid confusion.

  9. #9
    Join Date
    Feb 2009
    Posts
    14

    Re: Storing Objects in an Array

    Thanks for the explanation why Lindley. And thanks for identifying the problem JohnW@Wessex.

    Problem sorted. I officially love this forum.

    I also officially appologise for my sucky ability when it comes to c++ and OOP theory in general, and for the numerous other threads that will surely appear over the next few weeks!!

    Dave.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Storing Objects in an Array

    Quote Originally Posted by skycrazy123
    I declared them private in the header, and it seemed to work.
    Yes, that is a correct fix since you want info to a non-static member variable so that each MyClass has its own copy.

    By the way, you should #include <string>, not <string.h> since you are using std::string instead of the string functions inherited from C (in which case you should #include <cstring>). Also, you should remove the using directive at file scope (using namespace std; ) from the header file. To be const-correct, your get_info() member function should be declared const.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Feb 2009
    Posts
    14

    Re: Storing Objects in an Array

    Quote Originally Posted by laserlight View Post
    you should #include <string>, not <string.h> since you are using std::string instead of the string functions inherited from C (in which case you should #include <cstring>). Also, you should remove the using directive at file scope (using namespace std; ) from the header file.

    I know, I just hadnt got around to updating the files yet (but thanks for reminding me)

    Dave.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured