Search code examples
c++classunresolved-external

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;
}

This code gives me this error

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

From what I can tell there isn't anything outright wrong, just looking for some fresh eyes to look at it.

I've been searching and have found no answer, Any help is appreciated.


Solution

  • Well,

    First change your class declaration to:

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

    Then setPhone method:

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