简体   繁体   中英

Overloading ostream friend operator with shared_ptr vector

I am trying to overload the ostream operator as a friend in a class to build components of a circuit, however it keeps returning the address.

In a series-circuit class in the file "Circuit_classes.h":

friend ostream& operator<< (ostream& os, series_circuit const& myCircuit);

In the file "Circuit_classes.cpp":

ostream& operator<<(ostream& os, series_circuit const& myCircuit){
    os << "Output: " << myCircuit.frequency << endl;
    return os;
}

where frequency is defined in the class header file as 60.

In my main program, "AC Circuits.cpp"

vector<shared_ptr<circuit>> circuit_vector;
circuit_vector.push_back(shared_ptr<circuit>(new series_circuit));
cout << circuit_vector[0] << endl;

Output in the command line when the program is run:

0325E180
cout << circuit_vector[0] << endl;

circuit_vector[0] yields a std::shared_ptr which is what is being printed.

You must dereference it to get to the object itself.

cout << *circuit_vector[0] << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM