1. Why not just make general, sound and disp contained members of Setting? Why the pointers? You never seem to allocate them anyway (there is no
general = new General;
in your code, unless I missed it).
Instead, have
1 2 3 4 5 6 7 8 9
|
class Setting
{
public:
General& getGeneral() {
return general;
}
private:
General general;
};
| |
Do this for all 3 members. This way, you never need to allocate the object or delete it. You won't need your destructor at all.
BTW, if you have a constructor that does nothing, you don't need to define it. Unless your class is doing some sort of dynamic memory allocation or obtaining resources, you don't need to declare a destructor. Look up the Rule of Three (now it's the Rule of Five) to see that if you declare a destructor, you should also declare copy constructor and assignment operator (and now "move" variants of those).
2. I see that you edited your initial post while I was responding. So, my last paragraph doesn't make as much sense any more.
Here is the code I am now responding to:
1 2 3 4 5 6
|
void printall() {
int n = settingCnt;
for (int i = 0; i < n; i++) {
//Here I don't know how to get access to the elements I input to the node to print it out by using ouput() function format.
}
}
| |
1 2 3 4 5 6 7 8 9
|
template<class T>
void BST<T>::inOrder(Node<T>* aNode) {
if (aNode != NULL)
{
inOrder(aNode->left);
cout << aNode->data << " ";
inOrder(aNode->right);
}
}
| |
Don't loop on the size of the tree. That's why you have the inOrder function. That function will traverse the whole tree, so there is no need to loop on the size.
To get your code to work right now, change line 6 of inOrder to aNode->data.output(); See what that gives you. You might need to add the blank space to the end of Data::output.
3. Eventually, in order to make this work with other types (
AFTER YOU GET IT WORKING WITH YOUR CURRENT Output METHOD), restore line 6 of inOrder to the way you have it now and add the stream inserter function. This non-member function may need to be a friend function of the class, but it is declared as:
in the class:
friend std::ostream& operator<<(std::ostream& out, Setting& setting);
outside of the class:
std::ostream& operator<<(std::ostream& out, Setting& setting);
definition:
1 2 3 4 5 6 7 8 9
|
std::ostream& operator<<(std::ostream &out, Setting& setting)
{
out << setw(20) << car_name << setw(25) << car_id;
out << general;
out << sound;
out << disp;
out << " ";
return out;
}
| |
This last code snippet depends on you defining the stream insertion operator for all of the contained classes, too.