未解决的外部符号错误(带类)

Unresolved External Symbol Error w/ Classes

提问人:Adam G 提问时间:5/4/2015 更新时间:5/4/2015 访问量:45

问:

#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

据我所知,没有什么完全不对劲的地方,只是在寻找一些新鲜的眼睛来看待它。

我一直在寻找,但没有找到答案,任何帮助都是值得赞赏的。

C++ unresolved-external

评论


答:

0赞 hlscalon 5/4/2015 #1

首先将类声明更改为:

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: ";
    ...