-
February 24th, 2009, 11:18 AM
#1
[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.
-
February 24th, 2009, 11:21 AM
#2
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.
-
February 24th, 2009, 11:27 AM
#3
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.
-
February 24th, 2009, 11:30 AM
#4
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
-
February 24th, 2009, 11:31 AM
#5
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
-
February 24th, 2009, 11:43 AM
#6
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)
-
February 24th, 2009, 11:44 AM
#7
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
-
February 24th, 2009, 11:45 AM
#8
Re: Storing Objects in an Array
You can remove the global one in the implementation file to avoid confusion.
-
February 24th, 2009, 11:48 AM
#9
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.
-
February 24th, 2009, 11:49 AM
#10
Re: Storing Objects in an Array
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.
-
February 24th, 2009, 11:51 AM
#11
Re: Storing Objects in an Array
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|