0
#pragma once

#include <iostream>


class Duration {
private:
  int hours, minutes, seconds;

public:
  Duration() : hours(0), minutes(0), seconds(0) {}

  Duration(int s) : hours(s / 3600), minutes((s % 3600) / 60), seconds(s % 60) {}

  int get_s() const { return seconds; }

  int get_m() const { return minutes; }

  int get_h() const { return hours; }

  friend std::ostream& operator<<(std::ostream&, Duration&);

};

#include "duration.hpp"
#include <iostream>


std::ostream& operator<<(std::ostream& os, Duration& duration) { 
    os<< duration.get_h()<<duration.get_m()<<duration.get_s();
    return os; }
#include <iostream>
#include "duration.hpp"

int main() {
  
  Duration d1{};
  Duration d2{2222};

  std::cout << d1 <<d2;

}

I get the undefined reference to `operator<<(std::ostream&, Duration&)' error. How do I implement a << operator that shows values of the constructors ?

I am a newbie and just started learning about Classes, how do I solve this? Thanks

tyler99
  • 11
  • 3
  • 2
    how do you compile and link the code ? – 463035818_is_not_an_ai Mar 21 '23 at 14:00
  • Program [works here](https://onlinegdb.com/GjzYyw-l7). See [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Jason Mar 21 '23 at 14:03
  • You need to link both `duration.cpp` and `main.cpp` together. – orhtej2 Mar 21 '23 at 14:03
  • If you are using VSCode remember that it only builds the active file by default. The official documentation tells you that and how to change your tasks.json to build all source files in your folder. If you are not using VSCode and building from some other IDE like Visual Studio Community, Code::Blocks, Dev c++ make sure all source files are in your project. If you are building from the command line make sure you list all files. – drescherjm Mar 21 '23 at 14:12
  • I used Docker and vim in exams before but now I wanted to try out VSCode, so that is the problem. I will check it out, thank you! – tyler99 Mar 21 '23 at 14:20
  • If you are on MS Windows the instructions for installing and using MinGW with VSCode are here: [https://code.visualstudio.com/docs/cpp/config-mingw](https://code.visualstudio.com/docs/cpp/config-mingw) and the section that describes the building only the active file is here: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Mar 21 '23 at 14:50
  • As an aside, in Duration.hpp it would be sufficient to [`#include `](https://stackoverflow.com/questions/4300696/what-is-the-iosfwd-header) (which just forward-declares the type `iostream` instead of containing all the code that comes with it). That shortens compilation times if you include Duration.hpp in many places where you don't actually perform any output. – Peter - Reinstate Monica Mar 22 '23 at 09:00

0 Answers0