Question

I have some Link errors that are driving me crazy. all the tests work except for this one. this one states that there is an issue with an unresolved external symbol. It doesn't make sense to me that every test main works except the third.

 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
Was it helpful?

Solution

You have BAG_ELEMENT_TYPE set to an int in your CPP file so your implementations are using ints but your calls in main are looking for bankAccount value functions. You need the function signatures to match.

OTHER TIPS

  1. The type BAG_ELEMENT_TYPE has two different typedefs. You should fix that as already remarked by others.
  2. Your linker actually complains because the methods "add" and "currentItem" are used in the main file, defined in the bag.cpp file, but they are not declared in the bag.h file. You must add them inside your class.

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top