未解决的外部符号

时间:2012-12-05 16:26:06

标签: c++ visual-studio symbols unresolved-external

我遇到了一个错误,现在已经困扰了我很多天了。一个快速的谷歌没有给我一个答案。在我看来,代码没有错误,但是当我运行程序时,我得到9个未解析的外部符号(LNK2019)错误。在尝试解读我的一个错误后,我相信它发生在一个名为createMortgage的函数中。这是我对函数的调用。 customers是一个Vector。

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

这是函数本身。

void createMortgage(){
        int amount;

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

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

在这里,一切都是荣耀,是错误。

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)

这是我的抵押.h文件。

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

class Mortgage
{

private:
    int id;
    double amount;

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

这是我的抵押.cpp文件。

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

3 个答案:

答案 0 :(得分:1)

改变这个:

class Mortgage
{
int id;
double amount;

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

int getId(){
    return id;
}

double getAmount(){
    return amount;
}

对此:

#include "mortgage.h"

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

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

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

我发现你并没有真正了解如何使用标题和源文件来创建类,本教程将使你走上正轨:http://thenewboston.org/watch.php?cat=16&number=15

答案 1 :(得分:0)

1)您没有链接(可能没有编译)您的mortgage.cpp文件。检查IDE项目配置以确保它包含mortgage.cpp作为源文件。

2)您不得在cpp文件中重现类定义。相反,结构如下:

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

答案 2 :(得分:0)

您遇到基本C ++语法问题。

#pragma once是特定于Visual Studio的,是标头保护的替代品。它永远不会出现在.cpp文件

您提供了两个不同的类Mortage定义,一个在标题中,第二个在.cpp文件中

定义类的正确语法如下:

标题文件:

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

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

.cpp文件:

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

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

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