Ok, I will do my best to explain my situation. I have tried looking online to try to make this work and have yet to find anything so I hope you can help me out. Thanks in advance!!!
So I have a Document class which contains a vector<DocComponent*>. These docComponents are things such as Text, Table, and Graphic objects. Each are child classes of DocComponent. In my Document class, I have a addElement method:
----------------------------------------------------------------------------------
1 2 3 4 5
|
void Document::addElement(DocComponent &component)
{
// binary search and i-sort pg. 147
components.push_back(&component);
}
|
----------------------------------------------------------------------------------
Therefore, it adds each type of object i.e. Text, Graphic, ect. Each of these classes has an overloaded operator<< method used to just display their variables. Below, I am trying to print out each object with their respectful overloaded operator<< method. However, it keeps calling the DocComponent's << method and not the respectful class.
For example I create and add each kind of child class objects to the document component array. When I print out the document it displays 1, 2, 3 (which is what the DocComponent << does) where it is actually supposed to each field.
-----------------------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10
|
void main() {
Document d;
Text* t = new Text(1, 2, 3, 4, 5, 6);
Table* tbl = new Table(1, 2, 3, 4, 5, 6, 7);
Graphics* g = new Graphics(1, 2, 3, 4, 5);
d.addElement(*t);
d.addElement(*tbl);
d.addElement(*g);
cout << d << endl;
}
|
------------------------------------------------------------------------------------
DocComponent << method:
1 2 3 4 5 6 7 8 9
|
ostream& operator<<(ostream &s, DocComponent &doc)
{
// formatting for output to the user via cout command
s << "*DOC_COMPONENT*\n" <<
"Top Margin (in inches): " << doc.top << "\n" <<
"Left Margin (in inches): " << doc.left << "\n" <<
"Right Margin (in inches): " << doc.right << "\n\n";
return s;
}
|
------------------------------------------------------------------------------------
All 3 subclasses have similar << methods, but here is the example of Text:
1 2 3 4 5 6 7 8 9 10 11
|
ostream& operator<< (ostream &s, Text &t)
{
s << "*TEXT*\n" <<
"Top Margin (in inches): " << t.top << "\n" <<
"Left Margin (in inches): " << t.left << "\n" <<
"Right Margin (in inches): " << t.right << "\n" <<
"Font Type: " << t.fontType << "\n" <<
"Font Size: " << t.fontSize << "\n" <<
"Number of Characters: " << t.noChars << "\n\n";
return s;
}
|
---------------------------------------------------------------------------------------
Below is the Document's << method:
------------------------------------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
ostream& operator<< (ostream &s, Document &d)
{
// display the Document's information via for-loop
s << "*DOCUMENT*\n\n\n";
// DocComponents
s << "components vector\n";
for(int i = 0; i < d.components.size(); i++)
{
s<< "\t" << *(d.components[i]);
s << "\n";
}
s << "\n";
return s;
}
|
----------------------------------------------------------------------------------
I read all around online and it said that it had to do something with virtual, but I cannot find a way to make << method both friend and virtual. Any help with this would be outstanding! Thank you for the help!