1

I see there is no shortage of these on the site, but I am at my wits end to figure this out. It is a very simple program with only three file. When I attempt to compile the program I get the link error. All three files show in the solution explorer.

Here is the output

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 ==========

Here are the three files

Main.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
user1934286
  • 1,732
  • 3
  • 23
  • 42
  • [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). Specifically, "A common mistake is forgetting to qualify the name." – Raymond Chen May 14 '13 at 05:36

1 Answers1

7

Definition of void print() in DayOfYear.cpp should use the :: operator

void DayOfYear::print()
{
  //function body goes here
}

Otherwise, it is not interpreted as member function of the class DayOfYear.

user1934286
  • 1,732
  • 3
  • 23
  • 42
taocp
  • 23,276
  • 10
  • 49
  • 62