未解决的外部符号错误 w/ 类

Unresolved External Symbol Error w/ Classes

#include <iostream>
#include <string>
using namespace std;

class Phone
{
public:
    int countryCode, areaCode, mainNum;
    string type;
    Phone::Phone();
    void Phone::setPhone();
    void getPhone();
};

Phone::Phone()
{
    countryCode = 0;
    areaCode = 0;
    mainNum = 0;
    type = "HOME";
}

void setphone()
{
    Phone phone;
    cout << "Enter a country code: ";
    cin >> phone.countryCode;
    cout << "Enter an area code: ";
    cin >> phone.areaCode;
    cout << "Enter a number: ";
    cin >> phone.mainNum;
    cout << "Enter a type (HOME, OFFICE, FAX, CELL, or PAGER): ";
    cin >> phone.type;
}

int main()
{
    Phone p;
    Phone();
    p.setPhone();
    cout << p.countryCode << "-" << p.areaCode << "-" << p.mainNum << " " << p.type << endl;
}

这段代码给我这个错误

Error   1   error LNK2019: unresolved external symbol "public: void __thiscall Phone::setPhone(void)" (?setPhone@Phone@@QAEXXZ) referenced in function _main    c:\Users\Adam\documents\visual studio 2013\Projects\ConsoleApplication22\ConsoleApplication22\Source.obj    ConsoleApplication22
Error   2   error LNK1120: 1 unresolved externals   c:\users\adam\documents\visual studio 2013\Projects\ConsoleApplication22\Debug\ConsoleApplication22.exe 1   1   ConsoleApplication22

据我所知,没有什么完全错误的地方,只是想找一些新鲜的眼光来看待它。

我一直在搜索,但没有找到答案,我们将不胜感激。

嗯,

首先将您的 class 声明更改为:

Phone(); // remove Phone::
void setPhone(); // Phone::

然后setPhone方法:

void Phone::setPhone() // add Phone:: and correct name to setPhone, not setphone
{
    Phone phone;
    cout << "Enter a country code: ";
    ...