C ++错误LNK2019&&致命错误LNK1120:1未解析外部 [英] C++ error LNK2019 && fatal error LNK1120: 1 unresolved externals

查看:130
本文介绍了C ++错误LNK2019&&致命错误LNK1120:1未解析外部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为学校做家庭作业,并且超越老师要求的作业 - >我创建了一个列表类。我添加了'add()'方法和'newIncomeTax()'方法后,我一直遇到这两个错误

I am trying to work on a homework assignment for school and am going above what the teacher is asking for the assignment -> I have created a "list" class. I keep running into these two errors after adding the 'add()' method to the program - along with the 'newIncomeTax()' methods


error LNK2019:在函数_main driver.obj中引用的未解析的外部符号public:void __thiscall List :: add(class IncomeTax *)(?add @ List @@ QAEXPAVIncomeTax @@@ Z)

error LNK2019: unresolved external symbol "public: void __thiscall List::add(class IncomeTax *)" (?add@List@@QAEXPAVIncomeTax@@@Z) referenced in function _main driver.obj


致命错误LNK1120:1未解析的外部

fatal error LNK1120: 1 unresolved externals

我希望这对于任何试图帮助我的人都是足够的代码:

I hope this will be enough code for anyone trying to help me:

注意:下面的函数不是按照它们在原始代码中出现的顺序
(如果这可能是我可以提供我使用的所有代码的问题)

note: the functions below are not in the order that they appear in the original code (if that may be the problem I can provide all the code that i'm using)

#ifndef LIST_H
#define LIST_H

#include "IncomeTax.h"
class List
{
private:
    IncomeTax * First;
    IncomeTax * Last;
    int num_in_list;
public:
    List () { num_in_list = 0; First = NULL; Last = NULL; }
    int get_num_in_list() { return num_in_list; }
    IncomeTax * getFirst() { return First; }
    IncomeTax * getLast() { return Last; }
    void del_frnt ();
    void push_front (IncomeTax *);
    void push_back (IncomeTax *);
    void del_last ();
    void add (IncomeTax*);
    IncomeTax * pop_back ();
    IncomeTax * pop_front ();
    IncomeTax * get (int);
};
#endif

注意:从我看到的列表中, ve **的行为与默认行为类似

note: from what I've seen the list that **I've** made behaves similarly to the default

void List:: add (IncomeTax * IncomeTax_to_be_added) {
    if (num_in_list == 0) { First = IncomeTax_to_be_added; Last = IncomeTax_to_be_added; }
    else if (num_in_list != 0 ) {
        Last->setNext(IncomeTax_to_be_added);
        IncomeTax_to_be_added->setPrevous(Last);
        Last = IncomeTax_to_be_added;
    }
    num_in_list++;
}



IncomeTax.h



IncomeTax.h

#ifndef INCOME_TAX
#define INCOME_TAX

#include <iostream>
#include <string>
#include "conio.h"
#include <cassert>
using namespace std;

class IncomeTax {
private:
    double incm;
    double ajIncm;
    double subtract;
    double taxRate;
    double add;
    bool married;

    void calcIncome_m ();
    void calcIncome_s ();
public:
    IncomeTax () { incm = 0; subtract = 0; taxRate = 0; add = 0; add = false; }
    // married -> is by default false
    void setmarried ( bool stats ) { married = stats; }
    void setIncm (double in ) { incm = in; }
    void setSubtract ( double sub ) { subtract = sub; }
    void setTaxRate ( double rate ) { taxRate = rate; }
    void setAdd ( double Add ) { add = Add; }
    void setAjIncome ( double AJincome ) { ajIncm = AJincome; }

    bool getmarried () { return married; }
    double getIncm () { return incm; }
    double getsubtract () { return subtract; }
    double getTaxRate () { return taxRate; }
    double getAdd () { return add; }
    double getAJincome () { return ajIncm; }
    void calcIncome ();
    void pintIncome ();
};

#endif



IncomeTax.cpp



IncomeTax.cpp

#include "IncomeTax.h"
using namespace std;

void IncomeTax::calcIncome(){
    assert (incm != 0);
    if (married) { calcIncome_m(); }
    if (!married) { calcIncome_s(); }

    ajIncm = (incm - subtract);
    ajIncm -= (ajIncm * taxRate);
    ajIncm += add; 
}

void IncomeTax::calcIncome_m() {
    assert (incm != 0);
    ... huge nested if statements ... 
    they set subtract, add, taxRate...
}

void IncomeTax::calcIncome_s() {
    assert (incm != 0);
    ... huge nested if statements ... 
    they set subtract, add, taxRate...
}

void IncomeTax::pintIncome () {
    assert (incm != 0);
    assert (ajIncm != 0);

    std::cout.precision(2);
    cout << "\tTaxable Income: " << incm << endl;
    cout << "\tAjusted Income: " << ajIncm << endl;
    cout << "\tTax: " << (incm - ajIncm) << "\n" << endl;
}



Driver.cpp



Driver.cpp

#include <conio.h>
#include <iostream>
#include <string>
#include <cassert>
#include "IncomeTax.h"
#include "List.h"
using namespace std;

void getMaritalStatus( IncomeTax new_tax) {
    bool done = false;
    char stt = ' ';
    while ( !done ) {
        cout << "\nPlease declare weather you are filing taxes jointly or single" << "\n";
        cout << "\t's' = single\n\t'm' = married" << endl;
        stt = getch();
        if ( stt == 's' || stt == 'm' ) { done = true; }
        if ( stt == 's' ) { new_tax.setmarried(true); }
        if ( ! (stt == 's' || stt == 'm') ) { cout << "\nyou have entered an invald symbol... \n" << endl; }
        if(cin.fail()) { cin.clear(); }
        }
    }


void get_Income ( IncomeTax new_tax) {
    double _incm = 0;
    char status = ' ';
    bool done = true;
    while ( done ) {
        cout << "Please enter your TAXABLE INCOME:" << endl;
        cin >> _incm;
        if ( _incm > 0 ) { new_tax.setIncm(_incm); done = false; }
        if ( _incm <= 0 ) { cout << "\nthe number you entered was less than zero\nplease enter a valad number...\n" << endl; } 
        if(cin.fail()) { cin.clear(); }
    }
    }

IncomeTax newIncomeTax () {
    IncomeTax new_tax;
    IncomeTax * temp;
    get_Income(new_tax);
    getMaritalStatus(new_tax);
    new_tax.calcIncome();
    return new_tax;
}

bool again () {
    bool done = false, answer = false;
    char yn = ' ';
    while ( !done ) {
        cout << "\nWould you like to calculate another Income tax? (y/n)" << endl;
        yn = getch();
        if ( yn == 'y' || yn == 'n' ) { done = true; }
        if ( yn == 'y' ) { return false; }
        if ( yn == 'n' ) { return true; }
        if ( ! (yn == 's' || yn == 'n') ) { cout << "\nyou have entered an invald symbol... \n" << endl; }
        if(cin.fail()) { cin.clear(); }
        }
    }

int main () {
    IncomeTax new_tax;
    List L;
    bool done = false;
    while (!done) {
        IncomeTax temp = newIncomeTax();
        IncomeTax * ptr = &temp;
        L.add(ptr);
        done = again();
        };

    return 0;
};

我知道有很多更好的方法来做'if'语句 - >我刚刚决定如果只是使用if语句 - >教授已经说过没有必要超越这一点,这将是有效的。

I know that there are many better ways to do the 'if' statements -> i just decided that it would be efficient to just use the if statments -> the professor has stated that there is no need to go beyond this.

因为这是家庭作业,我很想得到一些反馈我可以使用更好的编程技术的方法。谢谢!

Since this is homework I would love to get some feed back on ways that I can use better programing techniques. thanks!

我正在使用VS express 2008 C ++

I'm using VS express 2008 C++

推荐答案


由于这是家庭作业,我很想得到一些反馈,我可以使用更好的编程技术。谢谢!

Since this is homework I would love to get some feed back on ways that I can use better programing techniques. thanks!

为您的类创建单独的标题/实现文件。不要将所有这些都放在一个标题中。把你的 main 放在一个单独的实现文件中(如果你愿意的话,调用 main.cpp )。

Create separate headers/implementation files for your classes. Don't put all of them in a single header. And put your main in a separate implementation file (call it main.cpp, if you will).

剪下所有未使用的函数,并尝试创建一个重现错误的最小示例,以帮助您轻松找出问题,而无需回到SO每时每刻。

Cut out all the functions that you are not using, and try to create the minimal example that reproduces your error -- that'll help you figure out your issues easily and without needing to come back to SO every now and then. Add one function at a time and see if it compiles, then move on to the next, rinse and repeat.

添加函数时的另一个好主意是使用stubs - 空函数只是为了检查你是否没有遇到任何这些未解决的符号错误(当你开始)。

Another good idea when adding functions is to use stubs -- empty functions just to check if you don't hit any of these unresolved symbol errors (when you're starting out).

您还需要了解语法 - 结束括号和之后不需要分号。您还将了解成员按照声明的顺序初始化(因此您的 int 成员应该理想地遵循您的 IncomeTax 成员)。你需要知道什么是初始化列表。你需要知道前向声明。

You will also need to learn about the syntax -- you don't need a semi-colon after the ending brace of while. You will also learn that members are initialized in the order in which they are declared (so your int member should ideally follow your IncomeTax members). You will need to know what initializer-lists are. And you will need to know about forward declaring.

总之,你需要一本书和很多练习。这里是你的代码重构了一些使用一些我上面解释的事情:

In short, you will need a book and lots of practice. Here's your code refactored a bit using some of the things I explained above:

class IncomeTax {};

class List
    {
    private:
        IncomeTax * First;
        IncomeTax * Last;
        int num_in_list;
    public:
        List () : 
            First(NULL), 
            Last(NULL),
                        num_in_list (0)
        {}
        ~List() 
        {
            // free First and Last ?
        }
        void add (IncomeTax*) {}    
};

    int main () {
        IncomeTax new_tax;
        List L;
        bool done = false;
        while (!done) {
            IncomeTax temp;
            L.add(&temp);
                done = true;
        }

        return 0;
    }

这篇关于C ++错误LNK2019&amp;&amp;致命错误LNK1120:1未解析外部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆