I'm trying to cout the vector temperatures, but I get the error:
"no operator "<<" matches these operands. Operand types are: std::ostream << Temps"
Would appreciate any answers. Point out any mistakes you see or things that could be done in a better way, and I'll try to implement the tips:)
temps.h
1 2 3 4 5 6
struct Temps {
vector<Temps> temperatures;
double max;
double min;
friend istream& operator>>(istream& is, Temps& t);
};
temps.cpp
1 2 3 4 5 6 7 8 9 10 11 12
istream& operator>>(istream& is, Temps& t) {
is >> t.max >> t.min;
return is;
}
ostream& operator<<(ostream& o, const Temps& t) {
vector<Temps> f = t.temperatures;
for (auto x : f) {
o << x;
}
return o;
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main(){
setlocale(LC_ALL, "norwegian");
ifstream is;
is.open("temperatures.txt");
if (!is) {
error("Could not read file", "temperatures.txt");
}
Temps temp;
while (is >> temp) {
(temp.temperatures).push_back(temp);
}
cout << temp;
return 0;
}
You also need a declaration of your stream insertion (<<) operator in temps.h; otherwise main.cpp (where it is presumably include'd - you don't show that) won't know it exists.
There's nothing in Temps that is private. I'm not sure that it matters whether the user-defined operator is a friend or not.
I'm also not sure that it makes sense to put
vector<Temps> temperatures;
inside Temps. Why don't you just declare that in your main() procedure.