Thread: Compilation problem

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    8

    Compilation problem

    First off let me say, I'm being forced to compile in Unix here but here goes:

    I'm currently testing out some classes i've made, one class (StoreList) which creates a list of another class (Store, the class that the list consists of has dynamic memory allocated in it). Now when I run the driver and get to the part where I do the 'add' function to add the Store to the StoreList, it gives me a compilation error saying,
    "ld:
    unresolved:
    StoreList::add(Store)"
    and it exits.

    Now this error only occurs when I attempt to pass the Store to the add function by reference, when I do it by value it compiles fine, but then it core dumps on me even before it gets to the first line of code in the function add.

    Is there something I missed? Do I need to put something special because the dynamic memory is in store? I assumed I would need to pass by reference because of this, but it gives the error....thanks for the help


    Arm

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I suppose it would be nice to see some code, like:

    1) The code for storelist::add
    2) The class definitions for both classes
    3) The code that is calling storelist::add
    4) Your copy constructor, if you coded one, for Store
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    8
    Here's the function, when called, causes the core dump:

    Code:
    bool StoreList::add(Store h)
    {
            cout << "in here" <<endl;
    
            Store temp;
            temp.name="void";
            int z=0, b;
            b=listsize;
    
            if(list[l-1].name == temp.name)
            {
                    while(list[z].name<h.name)
                    {
                            z++;
                    }
                    for(int i=0; i<(l-z); i++)
                    {
                            list[b+1]=list[b];
                            b--;
                    }
                    list[z]=h;
                    return true;
            }
            else
            {
                    cout << "list is full" <<endl;
                    return false;
            }
    }
    Heres the code in main.cc calling it, i cut it off where it doesn't get past:

    Code:
       Store store1;
       string storeName;
       StoreList List;
       int number;
    
       cout << "******************************** FIRST TEST" << endl;
       cin >> number;
       for (int i = 0; i < number; i++) {
          store1.read(cin);
    cout << "ran the read" <<endl;
          List.add(store1);
    Heres my copy constructor in Store:
    Code:
    Store::Store(const Store &d)
    {
            copy(d);
    }
    
    void Store::copy(const Store &d)
    {
            int i;
    
            name=d.name;
            revenue=d.revenue;
            numOwners=d.numOwners;
            for(i=0; i < numOwners; i++)
            {
                    owners[i]=d.owners[i];
            }
    }
    And lastly, my class definitions:

    Code:
    #ifndef STORE_H
    #define STORE_H
    
    #include <iostream>
    #include <string>
    #include "Owner.h"
    
    using namespace std;
    
    class Store
    {
       public:
          Store(string nameIn = "NONAME", float revenueIn = 0.0,
                int numOwnersIn = 0, Owner *ownersIn = NULL);
          Store(const Store &s);
          Store& operator=(const Store &s);
          ~Store();
    
          string getName();
          float getRevenue();
          void ownerInfo(int &numOwnersOut, Owner *&ownersOut);
    
          void read(istream &input);
          void print(ostream &output);
          string name;
    
       private:
          float revenue;
          int numOwners;
          Owner *owners;
    
          void copy(const Store &s);
          void free();
    };
    #endif
    
    
    
    #ifndef STORELIST_H
    #define STORELIST_H
    
    #include <iostream>
    #include "Store.h"
    #include "Owner.h"
    #include <string>
    
    using namespace std;
    
    class StoreList
    {
    
            public:
                    StoreList();
                    StoreList(const StoreList &w);
                    StoreList& operator=(const StoreList &w);
                    ~StoreList();
                    bool full();
                    bool empty();
                    bool add(Store h);
                    bool remove(string p);
                    void removeAll();
                    int size();
                    void print(ostream &out);
                    void reset();
                    void goNext();
                    bool atEnd();
                    void getCurrent(Store &p);
                    void setCurrent(Store &p);
    
            private:
                    static const int maxsize=80;
                    int l;
                    Store list[maxsize];
                    int listsize;
                    Store *curr;
                    int d;
    };
    
    #endif
    If I change the add fct to pass by reference, thats when I get the error msg I listed in the first post....as it is written here it compiles,and dumps

    Lets hope i did those code tags right

    Arm
    Last edited by Armatura; 11-11-2003 at 04:47 PM.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    8
    I did some debugging and narrowed it down to the loop I have in my copy constructor to copy over each owner in the array (in Store)...but I am still unsure why this doesnt work, it seems logical to me...


    Arm

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Where do you allocate the memory for "owners"? Certainly not in your copy constructor. You need to free the old memory, if any, and use new[] to allocate the right amount.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    8
    That was it, it was writing on memory not allocated, thanks =)

    Arm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  2. Compilation problem
    By OSDever in forum C++ Programming
    Replies: 10
    Last Post: 09-08-2005, 06:42 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM