1

我看到网站上不乏这些,但我无法解决这个问题。这是一个非常简单的程序,只有三个文件。当我尝试编译程序时,出现链接错误。所有三个文件都显示在解决方案资源管理器中。

这是输出

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

这是三个文件

主文件

#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
4

1 回答 1

7

void print()in的定义DayOfYear.cpp应该使用::操作符

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

否则,它不会被解释为类的成员函数DayOfYear

于 2013-05-14T03:48:41.293 回答