LNK 2019错误

时间:2013-06-18 01:26:43

标签: c++ visual-studio-2010 unsatisfiedlinkerror

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

 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:\Users\Desktop\stats\bagObject\Debug\bagObject.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

2 个答案:

答案 0 :(得分:0)

您的CPP文件中BAG_ELEMENT_TYPE设置为int,因此您的实施正在使用int,但main中的来电正在寻找bankAccount价值功能。您需要功能签名才能匹配。

答案 1 :(得分:0)

  1. BAG_ELEMENT_TYPE类型有两个不同的typedef。您应该像其他人已经说过的那样解决这个问题。
  2. 你的链接器实际上是抱怨的,因为在add.c和big.cpp文件中定义的主文件中使用了“add”和“currentItem”方法,但它们没有在bag.h文件中声明。你必须在课堂上添加它们。
  3. 希望它有所帮助!