LNK 2019 ERRORS

LNK 2019 ERRORS

本文关键字:ERRORS 2019 LNK      更新时间:2023-10-16

我有一些链接错误,让我发疯。所有的测试都有效,除了这个。这条声明存在一个未解决的外部符号的问题。对我来说,除了第三个之外,每个测试main都工作是没有意义的。

 1>  All outputs are up-to-date.
    1>test11c.obj : error LNK2019: unresolved external symbol "public: class bankAccount __thiscall bag::currentItem(void)const " (?currentItem@bag@@QBE?AVbankAccount@@XZ) referenced in function _main
    1>test11c.obj : error LNK2019: unresolved external symbol "public: void __thiscall bag::add(class bankAccount)" (?add@bag@@QAEXVbankAccount@@@Z) referenced in function _main
    1>C:UsersDesktopstatsbagObjectDebugbagObject.exe : fatal error LNK1120: 2 unresolved externals
    1>
    // 11C Test bag.sort  (answer in bag.cpp)
    #include <iostream>
    using namespace std;
    #include "COMPFUN.H"// For decimals
    #include "BACCOUNT.H"// Must include baccount before the typedef
    typedef bankAccount BAG_ELEMENT_TYPE;
    #include "bag.h" // For the bag class
    int main()
    {
      bag account;
      account.add( bankAccount("Mellisa", 400) );
      account.add( bankAccount("Miguel", 200) );
      account.add( bankAccount("Bob", 300) );
      decimals(cout, 2);
      account.sort();
      bankAccount anAcct;
      for( account.first(); ! account.isDone(); account.next() )
      {
account= account.currentItem();       // Output:
        cout.width(8);                        // 300.00 Bob
        cout << anAcct.balance();             // 400.00 Mellisa
        cout << " " << anAcct.name() << endl; // 200.00 Miguel
      }
      return 0;
    }
    //------------------------------------------------------------------
    // INTERFACE FILE: baccount.h
    //
    // Defines class bankAccount
    // Declares the relational operators so bankAccount objects 
    //   can be stored in standard containers such as list
    //
    //-------------------------------------------------------------------
    // SAFEGUARDS AND INCLUDES
    #ifndef BACCOUNT_H   // Avoid redeclaring class bankAccount.
    #define BACCOUNT_H   // This code is compiled only once
    #include <string>    // for class string
    using namespace std; // avoid having to write std:: as in std::string
    ///////////////////////////////////////////
    /////// class bankAccount defintion ///////
    ///////////////////////////////////////////
    class bankAccount {
    public:  // class member functions
    //--constructors
      bankAccount();
      bankAccount(string initName, double initBalance);
        // post: A bankAccount with two arguments when called like this:
        //       bankAccount anAcct("Hall", 100.00);
    //--modifiers
        void deposit(double depositAmount);
        // post: depositAmount is credited to this object's balance
        void withdraw(double withdrawalAmount);
        // post: withdrawalAmount is debited from this object's balance
    //--accessors
      double balance() const;
        // post: return this account's current balance
        string name() const;
       // post return the account name
    private: 
        string my_name;    // Uniquely identify an object
        double my_balance; // Store the current balance (non-persistent)
    };
    //--Auxilliary functions
    // With these two functions, bankAccount objects can be
    // sorted and searched by the standard algorithms
    bool operator <  (const bankAccount & left, const bankAccount & right);
    bool operator == (const bankAccount & left, const bankAccount & right);
    bool operator != (const bankAccount & left, const bankAccount & right);
    bool operator <= (const bankAccount & left, const bankAccount & right);
    bool operator >  (const bankAccount & left, const bankAccount & right);
    bool operator >= (const bankAccount & left, const bankAccount & right);
    #endif   // ifndef BACCOUNT_H
.CPP FILE
    #include <iostream>
    #include <vector>
    #include <cctype>
    #include <string>
    #include "BACCOUNT.H"
    #include "COMPFUN.H"
    using namespace std;
    typedef int BAG_ELEMENT_TYPE;
    #include "bag.h"
    //--constructors

     bag::bag(int initCapacity)
      // pre:  initCapacity >= 1
      // post: size of this bag is bag to 0 with the capacity 
      //       to store initCapacity BAG_ELEMENT_TYPE objects 
      {
          my_size = 0; my_index = 0; 
      my_capacity = initCapacity;
      my_element.resize(my_capacity);
      }
    //--modifiers
      int bag::occurrencesOf(BAG_ELEMENT_TYPE matchValue)
    {
      int results = 0;
      for (first(); !isDone(); next())
      {
         if (matchValue == currentItem())
         {
                results++;
         }
      }
       return results;         
    }
      void bag::add(BAG_ELEMENT_TYPE newElement)
      // post: Add newElement to this bag and increase 
      //       the size of this bag object increased by +1.
     //       Note: If capacity < size, the bag doubles it capacity
      { if (my_size >= my_capacity)
      { 
          my_element.resize(2 * my_capacity);
      }     my_element[my_size] = newElement;
            my_size++;

      }
      bool bag::remove(BAG_ELEMENT_TYPE removalCandidate)
      // post: If found, removalCandidate is removed from this bag.
      {
          int subscript =0;
         while((subscript < my_size) && (my_element[subscript] != removalCandidate))
      {
         subscript++;
      }
     if(subscript == my_size)
         {// removalCandidate not found
        return false;
         }
      else 
      { // move last element to removalCandidate's spot
        my_element[subscript]= my_element[my_size-1];
        // and then decreaase size by one
        my_size--;
        return true;
      }
      }
      void bag::sort ()
      // post: sort in ascending order
      {
            BAG_ELEMENT_TYPE Bag2;
         for(int top = 0; top < my_size-1; top++)
      {
        for(int j = top+1; j < my_size; j++)
        {if(my_element[j] < my_element[top])
         {
          Bag2 = my_element[top];
          my_element[top] = my_element[j];
          my_element[j] = Bag2;
         }
        }
      } 
      }
    //--accessors
      int bag::capacity() const
      // post: return the maximum number of elements that could be stored in this bag
      {
          return my_capacity;
      }
      int bag::size() const
      // post: return the number of elements that are currently in this bag
      //       the number of objects added but not removed.
      {
          return my_size;
      }
      bool bag::isEmpty () const
      // post: Returns true if there are zero items in the bag.
      //       Returns false if there is one more added elements
      {  if(my_size !=0)
        return my_size==0;
      }
    //--iterator functions
      void bag::first() const
      // post: my_index points to the first item
      // Cast away const so this appears to not modify the object
        // This is the only situation this trick should be used to subvert the meaning of const
         //((bag*)this)->my_index = 0;
      {
          if(my_size >= 0)
              ((bag*)this)->my_index = 0;
      }
      void bag::next() const
      // post: my_index points to the next item
      // Cast away const so this appears to not modify the object
      // This is the only situation this trick should be used to subvert the meaning of const
      // ((bag*)this)->my_index++;
      {
          ((bag*)this)->my_index++;
      }
      bool bag::isDone() const
      // post: Returns true if the collection has been traversed 
      {
          return my_index >= my_size;
      }
     BAG_ELEMENT_TYPE bag::currentItem() const
      // pre:  ! isDone && my_size > 0
      // post: Returns the item pointed to by the my_index
      {
          return my_element[my_index];
      }

Bag.h
    #ifndef BAG_H
    #define BAG_H
    #include <iostream>
    #include "BACCOUNT.H"
    #include "COMPFUN.H"
    #include <vector>
    using namespace std;
    const int DEFAULT_INITIAL_BAG_CAPACITY = 16;
    class bag {
    public:
    //--constructors
      bag();
      // post: Size of this bag is 0. 
      //       Initial capacity == DEFAULT_INITIAL_BAG_CAPACITY 
      bag(int initCapacity);
      // pre:  initCapacity >= 1
      // post: size of this bag is bag to 0 with the capacity 
      //       to store initCapacity BAG_ELEMENT_TYPE objects 
    //--modifiers
      int occurrencesOf(BAG_ELEMENT_TYPE matchValue);
      void add(BAG_ELEMENT_TYPE newElement);
      // post: Add newElement to this bag and increase 
      //       the size of this bag object increased by +1.
     //       Note: If capacity < size, the bag doubles it capacity
      bool remove(BAG_ELEMENT_TYPE removalCandidate);
      // post: If found, removalCandidate is removed from this bag.
      void sort ();
      // post: sort in ascending order
    //--accessors

      int capacity() const;
      // post: return the maximum number of elements that could be stored in this bag
      int size() const;
      bool isEmpty () const;
      // post: Returns true if there are zero items in the bag.
      //       Returns false if there is one more added elements

      void first() const;
      // post: my_index points to the first item
      // Cast away const so this appears to not modify the object
        // This is the only situation this trick should be used to subvert the meaning of const
         //((bag*)this)->my_index = 0;
      void next() const;
      // post: my_index points to the next item
      // Cast away const so this appears to not modify the object
      // This is the only situation this trick should be used to subvert the meaning of const
      // ((bag*)this)->my_index++;
      bool isDone() const;
      // post: Returns true if the collection has been traversed 
      BAG_ELEMENT_TYPE currentItem() const;
      // pre:  ! isDone && my_size > 0
      // post: Returns the item pointed to by the my_index
    private:
    int occurrencesOf(BAG_ELEMENT_TYPE) const;
      int my_size;
      int my_capacity;
      int my_index;   // an internal cursor for iterating over all elements
      vector <BAG_ELEMENT_TYPE> my_element;
    };
    #endif  // #ifndef BAG_H

您将BAG_ELEMENT_TYPE设置为CPP文件中的int,因此您的实现正在使用int s,但您在main中的调用正在寻找bankAccount值函数。您需要匹配函数签名

    BAG_ELEMENT_TYPE类型有两种不同的类型。你应该像其他人已经说过的那样解决这个问题。
  1. 你的链接器实际上抱怨,因为方法"add"answers"currentItem"在主文件中使用,在bag.cpp文件中定义,但它们没有在bag.h文件中声明。你必须在你的类中添加它们。

希望有帮助!