ErrorOperator
ErrorOperator

Reputation: 51

Unresolved External Symbol

I have encountered an error which has stumped me for many days now. A quick google hasn't given me an answer. The code, to my eyes, has no errors however when I run the program I get 9 Unresolved External Symbol(LNK2019) error. After trying to decipher one of my errors, I believe it is happening in a function named createMortgage. Here is my calling of the function. customers is a Vector.

for (unsigned int i = 0; i < customers.size(); i++)
            {
                Customer tempcust = customers.at(i);
                if (tempcust.getId() == id)
                {
                    customers.at(i).createMortgage();
                }
            }

Here is the function itself.

void createMortgage(){
        int amount;

        cout << "Amount?";
        cin >> amount;

        Mortgage mort(amount);
        mortgages.push_back(mort);
    }

And here, in all it's glory, is the error.

Error   4   error LNK2019: unresolved external symbol "public: __thiscall Mortgage::Mortgage(double)" (??0Mortgage@@QAE@N@Z) referenced in function "public: void __thiscall Customer::createMortgage(void)" (?createMortgage@Customer@@QAEXXZ) F:\C++ assignment (Real)\C++ assignment (Real)\Driver.obj   C++ assignment (Real)

Here is my mortgage .h file.

#pragma once
//#include <iostream>
//#include <String>

class Mortgage
{

private:
    int id;
    double amount;

public:
    Mortgage(double amount);
    double getAmount();
    int getId();
};

And here is my mortgage .cpp file.

#pragma once

extern int idcreation;

class Mortgage
{

    int id;
    double amount;

    Mortgage(double amount)
    {
        this -> amount = amount;
        this -> id = idcreation;
        idcreation++;
    }

    int getId(){
        return id;
    }

    double getAmount(){
        return amount;
    }

Upvotes: 0

Views: 265

Answers (3)

Šimon T&#243;th
Šimon T&#243;th

Reputation: 36423

You have issues with basic C++ syntax.

#pragma once is Visual Studio specific and is a replacement for header guards. It should never appear in a .cpp file

You are providing two different definitions of class Mortage one is in the header, the second one is in the .cpp file

The correct syntax for defining class is the following:

The header file:

/* something.h */
#ifndef SOMETHING_H_
#define SOMETHING_H_

class Something
{
public:
  Something();
  void some_method();
};
#endif

The .cpp file:

/* something.cpp */
#include "something.h"

Something::Something() { /* implementation */ }

void Something::some_method() { /* implementation */ }

Upvotes: 0

Name
Name

Reputation: 2045

Change this:

class Mortgage
{
int id;
double amount;

Mortgage(double amount)
{
    this -> amount = amount;
    this -> id = idcreation;
    idcreation++;
}

int getId(){
    return id;
}

double getAmount(){
    return amount;
}

To this:

#include "mortgage.h"

Mortgage::Mortgage(double amount)
{
    this -> amount = amount;
    this -> id = idcreation;
    idcreation++;
}

int Mortgage::getId(){
    return id;
}

double Mortgage::getAmount(){
    return amount;
}

I see you don't really get how to use headers and source files to make classes, this tutorial will get you on track: http://thenewboston.org/watch.php?cat=16&number=15.

Upvotes: 1

Robᵩ
Robᵩ

Reputation: 168596

1) You aren't linking (and probably not compiling) your mortgage.cpp file. Check your IDE project configuration to ensure that it includes mortgage.cpp as a source file.

2) You must not reproduce the class definition in your cpp file. Rather, structure it like this:

#include "mortgage.h"
Mortage::Mortgage(double d) { ... }

Upvotes: 0

Related Questions