2012-12-05 172 views
0

我曾经遇到过已经难倒了我很多天了一个错误。快速谷歌并没有给我一个答案。代码,对我来说,没有错误,但是当我运行该程序时,我得到了9个未解析的外部符号(LNK2019)错误。在试图破译我的一个错误之后,我相信它发生在一个名为createMortgage的函数中。这是我对这个功能的调用。 customers是一个向量。解析的外部符号

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)" ([email protected]@[email protected]@Z) referenced in function "public: void __thiscall Customer::createMortgage(void)" ([email protected]@@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; 
    } 
+0

你的问题是你没有提供构造函数'Mortgage :: Mortgage(double){}'的定义,或者你做了,但连接器无法看到它。 –

+0

当你运行程序时,您没有得到这个错误:你得到它,当你链接。事实上,你永远不会运行该程序。一些IDE(如Visual Studio)会在您要求运行它们时自动编译和链接程序,因此这可能会让您感到困惑。 – Gorpik

+0

@Gorpik,这是谷歌给我的一个搜索,一旦我知道我仔细检查了所有的代码,确保所有#include都是正确的,并且所有东西都链接到正确的文件。 – ErrorOperator

回答

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

+0

我已经将.h和.cpp代码添加到了我的文章中,我试着保留这篇文章中的代码量并解释我的问题,但是现在看来这是不可能的:) – ErrorOperator

+0

@ user1859684我看到您的错误检查我的回答 – Aaron

+0

不要忘记将'#include“mortgage.h”'添加到'mortgage.cpp'文件中。 –

0

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

2)在你的CPP文件不得复制类定义。相反,像这样的结构:

#include "mortgage.h" 
Mortage::Mortgage(double d) { ... } 
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 */ }