代码之家  ›  专栏  ›  技术社区  ›  Wallter

C++错误LNK2019和致命错误LNK1120:1未解决的外部问题

  •  3
  • Wallter  · 技术社区  · 16 年前

    我正在努力为学校做家庭作业,我的任务比老师要求的要高--我已经创建了一个“列表”类。在将“add()”方法和“newIncoMetax()”方法添加到程序之后,我一直遇到这两个错误。

    错误LNK2019:未解析的外部符号“public:void uuu thiscall list::add(class incometax*)”(?添加在函数\u main driver.obj中引用的@list@@qaexpavincometax@@z)

    和

    致命错误LNK1120:1个无法解析的外部变量

    我希望这足够任何人帮助我的代码:

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

    H表

    #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
    

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

    list.cpp中的“add”方法

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

    国际贸易协会

    #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
    

    英科托克斯普

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

    驱动程序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;
    };
    

    我知道有很多更好的方法来做“如果”的陈述--我只是决定使用“如果”的陈述是有效的--教授说没有必要超过这一点。

    因为这是家庭作业,我很想得到一些反馈,关于如何使用更好的编程技术。谢谢!

    我使用VS Express 2008 C++

    3 回复  |  直到 16 年前
        1
  •  2
  •   dirkgently    16 年前

    因为这是家庭作业,我很想得到一些反馈,关于如何使用更好的编程技术。谢谢!

    为类创建单独的头/实现文件。不要把它们全部放在一个标题中。把你的 main 在单独的实现文件中(称之为 main.cpp 如果你愿意的话。

    去掉所有不使用的函数,并尝试创建复制错误的最小示例——这将帮助您轻松地解决问题,而无需时不时地返回。一次添加一个函数,查看它是否编译,然后继续下一个函数,清洗并重复。

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

    您还需要了解语法——在 while . 您还将了解到成员是按照声明的顺序初始化的(因此 int 会员最好跟随您的 IncomeTax 成员)。您需要知道初始值设定项列表是什么。你需要知道转发声明。

    简而言之,你需要一本书和大量的练习。下面是使用我上面解释的一些东西重构的代码:

    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;
        }
    
        2
  •  1
  •   Francis Boivin    16 年前

    这里还有一些你没有告诉我们的事情,因为这应该是完美的链接(它在这里)。我只是将列表类声明移到main()之前,以避免在编译main时声明类,否则一切都编译良好。

    // Something declared in this order (in the same file or through .h files) should compile & link correctly
    class IncomeTax { ... };
    class List { ... };
    int main() { ... };
    
        3
  •  1
  •   Mark B    16 年前

    看起来您可能忘记了将incometax对象文件(incometax.o、incometax.obj等)添加到链接命令行中,而链接器告诉您它找不到方法定义。

    您可以通过临时创建一个“whole-project.cpp”文件来测试这一点,该文件包含按顺序粘贴的所有代码副本。如果它以这种方式正确地编译和链接,那么您只需要编辑makefile/ide项目文件/manual命令行,指定它还需要包含对象文件。

    它只显示一个错误,因为您实际上只调用了add函数,而列表类中没有其他非内联函数。

    最后,再次检查用于向列表中添加项的逻辑。我相信您正在添加的临时对象将导致问题,因为只要while循环进入下一个迭代,对象就会消失。