hppファイルは1つ
hpp
1#pragma once 2#include <iostream> 3#include <vector> 4#include <string> 5#include <tuple> 6using namespace std; 7 8void insertNames(); 9void JCtoAC(string eraName, int eraNum); 10void ACtoJC(int inputNum); 11 12extern const int startYear; 13extern const int thisYear; 14 15vector<tuple<string, int>> names;
c++ファイルは3つ
cpp
1//era.cpp 2#include "era.hpp" 3 4int main() 5{ 6 JCtoAC("Reiwa", 1); 7}
cpp
1//names.cpp 2#include "era.hpp" 3 4const time_t now = time(nullptr); 5const tm *lt = localtime(&now); 6const int startYear = 1615 - 1; //明治元年なので1を引く 7const int thisYear = 2000 + lt->tm_year; 8const int reiwa = (thisYear - 100) - 2019 + 1; //元号のスタートは0年ではないため1を足す 9 10vector<string> erasNames = { 11 "Genna", "Kanei", "Shouhou", "Keiwan", "Jouou", "Meireki", "Manji", "Kanbun", "Enpou", "Tenna", "Joukyou", "Genroku", "Houei", 12 "Syoutoku", "Kyouhou", "Genbun", "Kanpou", "Enkyou", "Kanen", "Houreki", "Meiwa", "Anei", "Tenmei", "Kansei", "Kyouwa", "Bunka", 13 "Bunsei", "Tenpou", "Kouka", "Kaei", "Ansei", "Manei", "Bunkyuu", "Genji", "Keiou", "Meiji", "Taisyou", "Syouwa", "Heisei", "Reiwa"}; 14 15vector<int> erasYears = {10, 21, 5, 5, 4, 4, 4, 13, 9, 4, 5, 17, 8, 6, 21, 6, 4, 5, 4, 14, 9, 10, 9, 13, 4, 15, 13, 15, 5, 7, 7, 2, 4, 2, 4, 45, 15, 64, 31, reiwa}; 16 17void insertNames() 18{ 19 for (int i = 0; i < erasNames.size(); i++) 20 { 21 names.push_back(make_tuple(erasNames[i], erasYears[i])); 22 } 23}
cpp
1//convert.cpp 2#include "era.hpp" 3void JCtoAC(string eraName, int eraNum) 4{ 5 insertNames(); 6 int sumNum = 0; 7 for (int i = 0; i < names.size(); i++) 8 { 9 string key = get<0>(names[i]); 10 int value = get<1>(names[i]); 11 if (eraName == key) 12 { 13 if (eraNum > value || eraNum <= 0) 14 { 15 cout << "The year " << eraName << eraNum << " doesn't exist." << endl; 16 } 17 else 18 { 19 cout << startYear + sumNum + eraNum << endl; 20 break; 21 } 22 } 23 sumNum += value - 1; 24 } 25} 26 27void ACtoJC(int inputNum) 28{ 29 if (inputNum > thisYear) 30 { 31 cout << "The year " << inputNum << " is the future. It may not be Reiwa Era." << endl; 32 } 33 else if (inputNum < startYear) 34 { 35 cout << "Sorry, I don't know about before" << startYear << endl; 36 } 37 else 38 { 39 int subNum = 0; 40 insertNames(); 41 for (int i = 0; i < names.size(); i++) 42 { 43 string key = get<0>(names[i]); 44 int value = get<1>(names[i]); 45 int checkNum = inputNum - startYear - value - subNum; 46 if (checkNum <= 0) 47 { 48 cout << key << value + checkNum << endl; 49 } 50 else 51 { 52 subNum += value - 1; 53 } 54 } 55 } 56}
これらのファイルを作成し、
$ g++ -std=c++11 -o a.out era.cpp
を実行すると
Undefined symbols for architecture x86_64: "JCtoAC(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int)", referenced from: _main in era-ac976a.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
とエラーが出ます。
とりあえずググってincludeやタイポを確認したのですが解決策が見つかりません。コマンドが間違っているのでしょうか。
間違いが分かる方は教えてください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/07 03:08
2020/06/07 03:29
2020/06/07 03:39