0

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;
    }
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
ErrorOperator
  • 51
  • 2
  • 8
  • Your problem is you did not provide definition for the constructor `Mortgage::Mortgage(double){}`, or perhaps you did but the linker cannot see it. – Alok Save Dec 05 '12 at 16:27
  • You don't get this error when you run the program: you get it when you link it. In fact, you never get to run the program. Some IDEs (such as Visual Studio) automatically compile and link programs when you ask to run them, so this may have confused you. – Gorpik Dec 05 '12 at 16:30
  • @Gorpik , This is what a searching google given me, once I knew that I carefully went through the whole of the code making sure all #include's were right and that everything linked to the correct file. – ErrorOperator Dec 05 '12 at 16:33

3 Answers3

1

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.

Name
  • 2,037
  • 3
  • 19
  • 28
  • I have included the .h and .cpp code to my post, I tried to keep the amount of code in the post down and explain my problem, but it seemed that wasn't possible now :) – ErrorOperator Dec 05 '12 at 16:41
  • Don't forget to add `#include "mortgage.h"` to the `mortgage.cpp` file. – Robᵩ Dec 05 '12 at 16:41
0

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) { ... }
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

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 */ }
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151