I’m having a hard time figuring out why this code won’t run. Can somebody help? I am trying to print all the information about the Phone objects with the info function which calls the operator<< for the device part of the phone object, but I’m pretty sure the issue lies in the way I redefined the operator<< for the Device class.
Device class:
template<typename T>
std::ostream& operator<<(std::ostream& output_stream, Device<T> const& device) {
output_stream << device.brand << std::endl;
for (size_t i = 0; i < APPS_SIZE - 1; i++)
{
output_stream << device.apps[i];
}
return output_stream;
}
Phone class:
template<typename T>
void Phone<T>::info() const {
const Device<T>& device_part = *this;
std::cout << device_part;
std::cout << this->price;
}
main method:
#include "phone.hpp"
#include "device.hpp"
int main() {
char brand[41] = "Generic Brand";
char apps[30] = "App1, App2, App3";
Phone<char> phone(12.1, brand, apps);
phone.info();
return 0;
}
I tried asking chatgpt and looking up some info on my lecturer’s presentation presentation on templates but I couldn’t figure anything out. It’s not an issue with my compiler either so it’s either some really dumb mistake but I’d love it if anyone can help me fix it!
Bogdan Hristov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.