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(¤t); 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}
回答2件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。