質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
g++

g++はGNUコンパイラコレクション(gcc)のC++コンパイラーです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

MinGW

MinGW(ミン・ジー・ダブリュー)は GNUツールチェーンのWindows移植版です。 ランタイムライブラリと開発ツールで構成されています。

Q&A

解決済

2回答

688閲覧

C++のコンパイルにてundefined reference to `operator<<(std::ostream&, Date const&)'のエラーが表示される

hashibiro

総合スコア10

g++

g++はGNUコンパイラコレクション(gcc)のC++コンパイラーです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

MinGW

MinGW(ミン・ジー・ダブリュー)は GNUツールチェーンのWindows移植版です。 ランタイムライブラリと開発ツールで構成されています。

0グッド

0クリップ

投稿2022/11/27 04:51

編集2022/11/27 06:42

C++のコンパイルにてundefined reference to `operator<<(std::ostream&, Date const&)'のエラーが表示されます。
リンク関係のエラーに見えますが解決方法がわかりません。
環境はvisual studio でMinGW-w64を使ってます。

C++

1g++ -lstdc++ .\Date.cpp .\DateTest1.cpp 2C:\Users\user\AppData\Local\Temp\ccE3QDsf.o:DateTest1.cpp:(.text+0x3d): undefined reference to `operator<<(std::ostream&, Date const&)' 3C:\Users\user\AppData\Local\Temp\ccE3QDsf.o:DateTest1.cpp:(.text+0x81): undefined reference to `operator<<(std::ostream&, Date const&)' 4C:\Users\user\AppData\Local\Temp\ccE3QDsf.o:DateTest1.cpp:(.text+0xd5): undefined reference to `operator<<(std::ostream&, Date const&)' 5collect2.exe: error: ld returned 1 exit status

コード

C++

1#ifndef ___Class_Date 2#define ___Class_Date 3 4#include <string> 5#include <iostream> 6 7class Date{ 8 int y; 9 int m; 10 int d; 11 12public: 13 Date(); 14 Date(int yy, int mm = 1, int dd =1); 15 16 int year() const { return y; } 17 int month() const { return m; } 18 int day() const { return d; } 19 20 Date preceding_day() const; 21 22 std::string to_string() const; 23 24 int day_of_week() const; 25}; 26 27std::ostream& operator<<(std::ostream& s, const Date& x); 28std::istream& operator>>(std::istream& s, Date& x); 29 30#endif

C++

1#include <ctime> 2#include <sstream> 3#include <iostream> 4#include "Date.h" 5 6using namespace std; 7 8Date::Date(){ 9 time_t current = time(NULL); 10 struct tm* local = localtime(&current); 11 12 y = local->tm_year + 1900; 13 m = local->tm_mon + 1; 14 d = local->tm_mday; 15} 16 17Date::Date(int yy, int mm, int dd){ 18 y = yy; 19 m = mm; 20 d = dd; 21} 22 23Date Date::preceding_day() const{ 24 int dmax[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 25 Date temp = *this; 26 27 if (temp.d > 1) 28 temp.d--; 29 else{ 30 if(--temp.m < 1){ 31 temp.y--; 32 temp.m = 12; 33 } 34 temp.d = dmax[temp.m - 1]; 35 } 36 return temp; 37} 38 39string Date::to_string() const{ 40 ostringstream s; 41 s << y << "年" << m << "月" << d << "日"; 42 return s.str(); 43} 44 45int Date::day_of_week() const{ 46 int yy = y; 47 int mm = m; 48 if ( mm == 1 || mm == 2){ 49 yy--; 50 mm += 12; 51 } 52 return (yy + yy / 4 - yy / 100 + yy / 400 + (13 * mm + 8) / 5 + d) % 7; 53}

C++

1#include <iostream> 2#include "Date.h" 3 4using namespace std; 5 6int main(){ 7 Date today; 8 9 cout << "今日" << today << "\n"; 10 cout << "昨日" << today.preceding_day() << "\n"; 11 cout << "昨日" << today.preceding_day().preceding_day() << "\n"; 12}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

to_string が定義されているのだから、operator<< の定義を書くのは簡単。

C++

1std::ostream& operator<<(std::ostream& s, const Date& x) 2{ 3 return s << x.to_string(); 4}

投稿2022/11/27 23:19

kazuma-s

総合スコア8224

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

undefined reference to `operator<<(std::ostream&, Date const&)'

文字通り operator<<(std::ostream&, Date const&) が見つからないのでしょう。
定義しましたか? コード見せて。

投稿2022/11/27 05:02

episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hashibiro

2022/11/27 06:42

コード追加しました。
episteme

2022/11/27 09:20

呈示されたコード中に operator<<(std::ostream&, Date const&) は見当たりません。定義しましたか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問