Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't I make objects in C++ have data-members that are other objects?
    text
    copied!<p>I was bored one day, so I made this library:</p> <pre><code>template &lt;class X, class Y&gt; class HASH { public: HASH(X element, Y key) { elements=new X[0]; keys=new Y[0]; keys[0]=key; elements[0]=element; num_elements=0; }; ~HASH () { delete[] elements; delete[] keys; }; void Add(X element, Y key) { for (int x=0; x&lt;=num_elements; x++) { if (keys[x]==key) { throw(key); } } //make copy variables X *copy_elements=new X[num_elements]; Y *copy_keys=new Y[num_elements]; //copy all the elements for (int x=0; x&lt;=num_elements; x++) { copy_elements[x]=elements[x]; copy_keys[x]=keys[x]; } //delete the originals delete[] elements; delete[] keys; //resize the originals elements=new X[num_elements+1]; keys=new Y[num_elements+1]; //copy everything back to the originals for (int x=0; x&lt;=num_elements; x++) { elements[x]=copy_elements[x]; keys[x]=copy_keys[x]; } //delete the copies delete[] copy_keys; delete[] copy_elements; //increment num_elements num_elements++; //add the new elements elements[num_elements]=element; keys[num_elements]=key; }; X operator [] (Y key) { int num=0; for (int x=0; x&lt;=num_elements; x++) { if (keys[x]==key) { num=x; break; } } return elements[num]; }; Y KeyNum(int x) { return keys[x]; }; int NumElements() { return num_elements; }; private: int num_elements; X *elements; Y *keys; }; </code></pre> <p>and then I tested it, and it worked. So now I am trying to create an experiment whose source code is this:</p> <pre><code>#include &lt;hash.cpp&gt; #include &lt;iostream&gt; using namespace std; class OBJECT { public: OBJECT(string name, int number) { int_properties=new HASH &lt;string, int&gt;; }; ~OBJECT() { delete int_properties; }; HASH &lt;string, int&gt; *int_properties; }; int main() { OBJECT a("name", 5); return 0; } </code></pre> <p>and it is returning the error:</p> <pre> brain.cpp: In constructor ‘OBJECT::OBJECT(std::string, int)’: brain.cpp:10: error: no matching function for call to ‘HASH&lt;std::basic_string&lt;char, std::char_traits&lt;char>, std::allocator&lt;char> >, int>::HASH()’ /usr/include/hash.cpp:4: note: candidates are: HASH&lt;X, Y>::HASH(X, Y) [with X = std::basic_string&lt;char, std::char_traits&lt;char>, std::allocator&lt;char> >, Y = int] /usr/include/hash.cpp:2: note: HASH&lt;std::basic_string&lt;char, std::char_traits&lt;char>, std::allocator&lt;char> >, int>::HASH(const HASH&lt;std::basic_string&lt;char, std::char_traits&lt;char>, std::allocator&lt;char> >, int>&) </pre> <p>I have made a whole lot of tests, and no matter what I do, I can't seem to initialize an object that is the data-member of another object. I just don't understand why.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload