サイトにはこれらが不足していないことがわかりますが、これを理解するために知恵を絞っています. 3 つのファイルのみの非常に単純なプログラムです。プログラムをコンパイルしようとすると、リンク エラーが発生します。3 つのファイルすべてがソリューション エクスプローラーに表示されます。
ここに出力があります
1>------ Build started: Project: Problem 2, Configuration: Debug Win32 ------
1>Build started 5/13/2013 8:33:06 PM.
1>InitializeBuildStatus:
1> Touching "Debug\Problem 2.unsuccessfulbuild".
1>ClCompile:
1> Main.cpp
1> DayOfYear.cpp
1> Generating Code...
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>Main.obj : error LNK2019: unresolved external symbol "public: void __thiscall DayOfYear::print(void)" (?print@DayOfYear@@QAEXXZ) referenced in function _main
1>C:\Users\DigiVision\Desktop\School\CS-150-01\W11CH11\Problem 2\Debug\Problem 2.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.72
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
ここに3つのファイルがあります
メイン.cpp
#include <iostream>
#include "DayOfYear.h"
using namespace std;
int main()
{
int num;
do
{
cout << "Enter a number (-1 to escape): ";
cin >> num;
if (num != -1)
{
DayOfYear day(num);
day.print();
}
cout << endl;
} while (num !=-1);
cout << endl;
return 0;
}
DayOfYear.cpp
#include "DayOfYear.h"
#include <iostream>
using namespace std;
int number;
DayOfYear::DayOfYear(int n)
{
number = n;
}
void print()
{
const char * const month[12] = {"January","February","March","April","May","June",
"July","August","September","October","November","December"};
if (number < 32) // Jan
cout << month[0] << " " << number;
else if (number < 60) // Feb
cout << month[1] << " " << 60 - number;
else if (number < 91) // Mar
cout << month[2] << " " << 91 - number;
else if (number < 121) // Apr
cout << month[3] << " " << 121 - number;
else if (number < 152) // May
cout << month[4] << " " << 152 - number;
else if (number < 182) // Jun
cout << month[5] << " " << 182 - number;
else if (number < 213) // Jul
cout << month[6] << " " << 213 - number;
else if (number < 244) // Aug
cout << month[7] << " " << 244 - number;
else if (number < 274) // Sep
cout << month[8] << " " << 274 - number;
else if (number < 305) // Oct
cout << month[9] << " " << 305 - number;
else if (number < 335) // Nov
cout << month[10] << " " << 335 - number;
else if (number < 366) // Dec
cout << month[11] << " " << 366 - number;
}
DayOfYear.h
#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H
class DayOfYear
{
private:
int number;
public:
DayOfYear()
{
number = 0;
}
DayOfYear(int);
void print();
};
#endif