[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??
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.
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.
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");
Re: Storing Objects in an Array
You have a global std::string info;
Declare it in MyClass.
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)
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
Re: Storing Objects in an Array
You can remove the global one in the implementation file to avoid confusion.
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.
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.
Re: Storing Objects in an Array
Quote:
Originally Posted by
laserlight
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.