Array of objects from another class

S

sr98p47

I have no experience with C++ other than going through the FAQ to find
my answer but I'm stuck. I did a search on Google Books through
Deitel's C++ book but the preview is limited. I have bought Deitel's
book on C++ on Amazon but it hasn't been delivered yet. There's no
Barnes & Nobles within 50 miles from where we live and the localy
library isn't barren of computer books.

I'm trying to create a simple table in order to start learning C++,
something I can try hashing numbers or characters or strings into. I
don't know much about tables so it's possible I have the concept
wrong.

In my header file, I put the following, leaving out any directives to
save space:

class data {
public:
data(int);
~data(int);
private:
int item;
};

In my definitions file, my constructor is simple:

data::data(int x) {
item = x;
}

I then built another class to create a table, a table consisting of an
array of data items so I can hash numbers or strings into them, but I
don't think I'm constructing it correctly:

Here's my class definition file for the table I want to create:

#include "data.h"

class data_table {
public:
data_table(int);
~data_table();
private:
data *data_entries; // pointer to an array of data
items.
int table_size;
};

#include "data.h"

data_table::data_table(int size) {
table_size = size;
data_entries = new data[size]; // compiler error
...
}

The warning I'm getting is: no matching function for call to
`data::data()'

I modified my data class definition to include an array, a pointer,
and a copy constructor, but I can't seem to get this to work.

I can stop the compiler errors if I change this

data_entries = new data[size];

to this:

data_entries = new data(size};

but then when I try to write code to access the data_entires array,
there's more compiler errors.

I know I should use a vector, but that's beyond me right now. Is
there a simple solution to this error? I'm sorry if this is an easy
question.
 
J

Jim Langston

sr98p47 said:
I have no experience with C++ other than going through the FAQ to find
my answer but I'm stuck. I did a search on Google Books through
Deitel's C++ book but the preview is limited. I have bought Deitel's
book on C++ on Amazon but it hasn't been delivered yet. There's no
Barnes & Nobles within 50 miles from where we live and the localy
library isn't barren of computer books.

I'm trying to create a simple table in order to start learning C++,
something I can try hashing numbers or characters or strings into. I
don't know much about tables so it's possible I have the concept
wrong.

In my header file, I put the following, leaving out any directives to
save space:

class data {
public:
data(int);
~data(int);
private:
int item;
};

In my definitions file, my constructor is simple:

data::data(int x) {
item = x;
}

I then built another class to create a table, a table consisting of an
array of data items so I can hash numbers or strings into them, but I
don't think I'm constructing it correctly:

Here's my class definition file for the table I want to create:

#include "data.h"

class data_table {
public:
data_table(int);
~data_table();
private:
data *data_entries; // pointer to an array of data
items.
int table_size;
};

#include "data.h"

data_table::data_table(int size) {
table_size = size;
data_entries = new data[size]; // compiler error

new data[size]; would call the default constructor for data, which you don't
have. The only constructor for data you have is data(int).
You have a few options. 1. Create a default constructor for data that sets
item to some value (maybe zero?).

Basically your data_table is duplicated what std::vector does. I understand
you are doing this as a learning experience so that's fine, but in
applications you probalby want to use std::vector. I.E.
std::vector<data> MyData;
MyData.push_back( /*...*/ );

You will notice that std::vector usually doesn't try to put in blank
entries, but you can add them to the end, getting rid of your problem also.
Not having to make default constructed entries.
...
}

The warning I'm getting is: no matching function for call to
`data::data()'

I modified my data class definition to include an array, a pointer,
and a copy constructor, but I can't seem to get this to work.

I can stop the compiler errors if I change this

data_entries = new data[size];

to this:

data_entries = new data(size};

but then when I try to write code to access the data_entires array,
there's more compiler errors.

I know I should use a vector, but that's beyond me right now. Is
there a simple solution to this error? I'm sorry if this is an easy
question.
 
H

Helge Kruse

sr98p47 said:
In my header file, I put the following, leaving out any directives to
save space:

class data {
public:
data(int);
~data(int);
private:
int item;
};

Destructors should have any parameters. Typo?

Helge
 
R

rishabh

I have no experience with C++ other than going through the FAQ to find
my answer but I'm stuck.  I did a search on Google Books through
Deitel's C++ book but the preview is limited.  I have bought Deitel's
book on C++ on Amazon but it hasn't been delivered yet.  There's no
Barnes & Nobles within 50 miles from where we live and the localy
library isn't barren of computer books.

I'm trying to create a simple table in order to start learning C++,
something I can try hashing numbers or characters or strings into.  I
don't know much about tables so it's possible I have the concept
wrong.

In my header file, I put the following, leaving out any directives to
save space:

class data {
        public:
                data(int);
                ~data(int);
        private:
                int item;

};

In my definitions file, my constructor is simple:

data::data(int x) {
               item = x;

}

I then built another class to create a table, a table consisting of an
array of data items so I can hash numbers or strings into them, but I
don't think I'm constructing it correctly:

Here's my class definition file for the table I want to create:

#include "data.h"

class data_table {
        public:
                data_table(int);
                ~data_table();
        private:
                data *data_entries; // pointer to an array of data
items.
                int table_size;

};

#include "data.h"

data_table::data_table(int size) {
        table_size = size;
        data_entries = new data[size]; // compiler error
        ...
        }

The warning I'm getting is: no matching function for call to
`data::data()'

I modified my data class definition to include an array, a pointer,
and a copy constructor, but I can't seem to get this to work.

I can stop the compiler errors if I change this

        data_entries = new data[size];

to this:

        data_entries = new data(size};

but then when I try to write code to access the data_entires array,
there's more compiler errors.

I know I should use a vector, but that's beyond me right now.  Is
there a simple solution to this error?  I'm sorry if this is an easy
question.

There are few problems in the code
1. ~data(int)-->Destructor do not take any parameter.

2. I am not getting , why you set data::item in constructor. How can
you define all values of hash only at declaration time? The value will
be given by user(hash table user). At that time you will set the value
'item'.
As per my understanding you want to develop data structure like SET in
mathematics. There is already STL 'set' for this purpose. You can use
it if you do not want to write your own code.

If you donot want to use Vector, define constructor as data(int x =
SOME_DEFINE_VALUE) and enjoy 'new'.

3.
a.) data_entries = new data[size];
and
b.) data_entries = new data(size);
are different.
a.) will allocate sizeof(size*data) amount of memory and will call
default constructor.
b.) will allocate sizeof(data) amount of memory and will camm
constructor data(int)
 
S

sr98p47

Thanks, yes, typo.

I'm still in a bind, having a default constructor hasn't helped, I've
tried a few other constructors but it's still not working, maybe my
idea of a table is wrong.

The table should just be a simple array with some strings or integers:

table x[ints or strings]

x[1] = 0
x[2] = 28

and so on.

I'll wait until I get the Deitel book, as I indicated vectors are
beyond me right now, don't have much experience with programming, it
will be better to get the text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top