Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,019 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,496 people online right now. Registration is fast and FREE... Join Now!




C++ member functions help (needing more help)

 
Reply to this topicStart new topic

C++ member functions help (needing more help), See last post... please?

bennyboy371
4 Dec, 2006 - 07:23 AM
Post #1

New D.I.C Head
*

Joined: 4 Dec, 2006
Posts: 17


My Contributions
I'm having some problems with an assignment due tonight. I spent the better part of yesterday (though the code doesn't show it) trying to figure it out. Granted, a few of those hours were before realizing I'd screwed up on quotation marks. Anyhow, I've been trying to get this to work in Dev-C++ and it keeps telling me on line 34 "`BinList' undeclared (first use this function)". That being under displayBins. I honestly can't figure this out and I'm sure its something small and stupid, I was hoping someone here could help me out so I can do the rest.

CODE
#include <iostream>
#include <string>

using namespace std;

class Inv{
private:
    struct Bin{
      string name;
      int amt;
    
      Bin(string d, int a){
      name = d;
      amt = a;
      }
    };

public:
  void openWarehouse(){
  static Bin BinList[10] = {  Bin("Valve", 10),
                         Bin("Bearing", 5),
                         Bin("Bushing", 15),
                         Bin("Coupling", 21),
                         Bin("Flange", 7),
                         Bin("Gear", 5),
                         Bin("Gear housing", 5),
                         Bin("Vacuum gripper", 25),
                         Bin("Cable", 18),
                         Bin("Rod", 12)  };
  }
  
  void displayBins(){
    for(int i = 0; i < 10; i++)
      cout << BinList[i].name << "\t" << BinList[i].amt << endl;
  }

};


int main(){
    Inv Inventory;
    Inventory.openWarehouse();
    Inventory.displayBins();
    
    system("PAUSE");
    return 0;
}


This post has been edited by bennyboy371: 4 Dec, 2006 - 10:05 PM
User is offlineProfile CardPM
+Quote Post

UMTopSpinC7
RE: C++ Member Functions Help (needing More Help)
4 Dec, 2006 - 07:38 AM
Post #2

New D.I.C Head
*

Joined: 20 Oct, 2006
Posts: 47


My Contributions
QUOTE(bennyboy371 @ 4 Dec, 2006 - 08:23 AM) *

I'm having some problems with an assignment due tonight. I spent the better part of yesterday (though the code doesn't show it) trying to figure it out. Granted, a few of those hours were before realizing I'd screwed up on quotation marks. Anyhow, I've been trying to get this to work in Dev-C++ and it keeps telling me on line 34 "`BinList' undeclared (first use this function)". That being under displayBins. I honestly can't figure this out and I'm sure its something small and stupid, I was hoping someone here could help me out so I can do the rest.

CODE
#include <iostream>
#include <string>

using namespace std;

class Inv{
private:
    struct Bin{
      string name;
      int amt;
    
      Bin(string d, int a){
      name = d;
      amt = a;
      }
    };

public:
  void openWarehouse(){
  static Bin BinList[10] = {  Bin("Valve", 10),
                         Bin("Bearing", 5),
                         Bin("Bushing", 15),
                         Bin("Coupling", 21),
                         Bin("Flange", 7),
                         Bin("Gear", 5),
                         Bin("Gear housing", 5),
                         Bin("Vacuum gripper", 25),
                         Bin("Cable", 18),
                         Bin("Rod", 12)  };
  }
  
  void displayBins(){
    for(int i = 0; i < 10; i++)
      cout << BinList[i].name << "\t" << BinList[i].amt << endl;
  }

};


int main(){
    Inv Inventory;
    Inventory.openWarehouse();
    Inventory.displayBins();
    
    system("PAUSE");
    return 0;
}



It's giving you that error because the function displayBins() doesn't know about BinList because you declare binlist INSIDE the function openWarehouse(). If you did this it would work.

CODE
#include <iostream>
#include <string>

using namespace std;

class Inv{
private:
    struct Bin{
      string name;
      int amt;
    
      Bin(string d, int a){
      name = d;
      amt = a;
      }
    };

public:
  static Bin BinList[10];

  void openWarehouse(){
                              BinList = {  Bin("Valve", 10),
                         Bin("Bearing", 5),
                         Bin("Bushing", 15),
                         Bin("Coupling", 21),
                         Bin("Flange", 7),
                         Bin("Gear", 5),
                         Bin("Gear housing", 5),
                         Bin("Vacuum gripper", 25),
                         Bin("Cable", 18),
                         Bin("Rod", 12)  };
  }
  
  void displayBins(){
    for(int i = 0; i < 10; i++)
      cout << BinList[i].name << "\t" << BinList[i].amt << endl;
  }

};


int main(){
    Inv Inventory;
    Inventory.openWarehouse();
    Inventory.displayBins();
    
    system("PAUSE");
    return 0;
}

I did that really quickly though so it might not be exactly right.
User is offlineProfile CardPM
+Quote Post

bennyboy371
RE: C++ Member Functions Help (needing More Help)
4 Dec, 2006 - 07:45 AM
Post #3

New D.I.C Head
*

Joined: 4 Dec, 2006
Posts: 17


My Contributions
I've changed that to
CODE
static Bin BinList;

And that seems to work okay, except on line 35 in the displayBins member function it now says "no match for 'operator[]' in 'Inv::BinList[i]'", being the line
CODE
      cout << BinList[i].name << "\t" << BinList[i].amt << endl;

So I'm not sure that it worked correctly. That, or I'm getting the syntax wrong. I assume it to have something to do with how arrays are initialized, but under
CODE
static Bin BinList[10];

It gives me another error altogether, regarding an "undefined reference to Inv::BinList", so I'm not sure which error is the one to go with addressing.

This post has been edited by bennyboy371: 4 Dec, 2006 - 08:05 AM
User is offlineProfile CardPM
+Quote Post

horace
RE: C++ Member Functions Help (needing More Help)
4 Dec, 2006 - 08:15 AM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(bennyboy371 @ 4 Dec, 2006 - 03:45 PM) *

I've changed that to
CODE
static Bin BinList;

And that seems to work okay, except on line 35 in the displayBins member function it now says "no match for 'operator[]' in 'Inv::BinList[i]'", being the line
CODE
      cout << BinList[i].name << "\t" << BinList[i].amt << endl;

So I'm not sure that it worked correctly. That, or I'm getting the syntax wrong. I assume it to have something to do with how arrays are initialized, but under
CODE
static Bin BinList[10];

It gives me another error altogether, regarding an "undefined reference to Inv::BinList", so I'm not sure which error is the one to go with addressing.

if BinList is going to be a static data member you have to initialise it outside the class declaration, e.g.
CODE

#include <iostream>
#include <string>
using namespace std;

class Inv{
private:
    struct Bin{
      string name;
      int amt;
    
      Bin(string d, int a){
      name = d;
      amt = a;
      }
    };

public:
  static Bin BinList[10];   // declare static data member
  void openWarehouse(){
  }
  
  void displayBins(){
    for(int i = 0; i < 10; i++)
      cout << BinList[i].name << "\t" << BinList[i].amt << endl;
  }

};

// you have to initialise static data member outside the class declaration
Inv::Bin Inv::BinList[10] = {  Bin("Valve", 10),
                         Bin("Bearing", 5),
                         Bin("Bushing", 15),
                         Bin("Coupling", 21),
                         Bin("Flange", 7),
                         Bin("Gear", 5),
                         Bin("Gear housing", 5),
                         Bin("Vacuum gripper", 25),
                         Bin("Cable", 18),
                         Bin("Rod", 12)  };

int main(){
    Inv Inventory;
    Inventory.openWarehouse();
    Inventory.displayBins();
    
    system("PAUSE");
    return 0;
}

when run using DEV-C++ this gives
Valve 10
Bearing 5
Bushing 15
Coupling 21
Flange 7
Gear 5
Gear housing 5
Vacuum gripper 25
Cable 18
Rod 12
Press any key to continue . . .

This post has been edited by horace: 4 Dec, 2006 - 08:16 AM
User is offlineProfile CardPM
+Quote Post

bennyboy371
RE: C++ Member Functions Help (needing More Help)
4 Dec, 2006 - 08:19 AM
Post #5

New D.I.C Head
*

Joined: 4 Dec, 2006
Posts: 17


My Contributions
That works perfectly, thank you very much! What a wonderful learning experience. tongue.gif I can now finish the rest of the program after my classes, so thank you both.
User is offlineProfile CardPM
+Quote Post

y0da
RE: C++ Member Functions Help (needing More Help)
4 Dec, 2006 - 10:02 AM
Post #6

D.I.C Head
Group Icon

Joined: 8 Nov, 2006
Posts: 62


Dream Kudos: 75
My Contributions
Wasn't as hard as you thought huh? I just looked at it and figured that out... Sometimes you need to take a break and go do something esle besides programming so you can clear your mind....
User is offlineProfile CardPM
+Quote Post

bennyboy371
RE: C++ Member Functions Help (needing More Help)
4 Dec, 2006 - 07:23 PM
Post #7

New D.I.C Head
*

Joined: 4 Dec, 2006
Posts: 17


My Contributions
I'm baaaaaack. Well, I figured out that it would work fine as-is, but unfortunately I needed to put it inside of a member function, as per instructions. However, every which way I try, I get massive errors. I tried different ways for a good hour or so, and hammered out bugs in other areas of the code, so I'll just post the portions I'm having a problem with again.
CODE
class Inv{
private:
    struct Bin{
      string name;
      int amt;
    
      Bin(string d, int a){
      name = d;
      amt = a;
      }
    };

public:
  static void openWarehouse();
  static Bin BinList[10];
  void menu();
  void displayBins();
  void addParts(int, int);
  void removeParts(int, int);
};

void Inv::openWarehouse(){
static Bin BinList[10] = {  Bin("Valve", 10),
                         Bin("Bearing", 5),
                         Bin("Bushing", 15),
                         Bin("Coupling", 21),
                         Bin("Flange", 7),
                         Bin("Gear", 5),
                         Bin("Gear housing", 5),
                         Bin("Vacuum gripper", 25),
                         Bin("Cable", 18),
                         Bin("Rod", 12)  };
}
In MS Visual Studio, I get the error
QUOTE
error LNK2001: unresolved external symbol "public: static struct Inv::Bin * Inv::BinList" (?BinList@Inv@@2PAUBin@1@A)
and in Dev-C++, I get
QUOTE
Dev-C++: [Linker error] undefined reference to `Inv::BinList'
I figured I'd put errors from both to clarify a bit. I have to make BinList static inside of the openWarehouse function, and I really can't figure out how. The way horace posted worked perfectly fine, except instructions mean I must keep treading on! Any help guys? Don't worry, I'll practice more, I don't want to be dependent on asking for help.

This post has been edited by bennyboy371: 4 Dec, 2006 - 07:31 PM
User is offlineProfile CardPM
+Quote Post

bennyboy371
RE: C++ Member Functions Help (needing More Help)
4 Dec, 2006 - 10:28 PM
Post #8

New D.I.C Head
*

Joined: 4 Dec, 2006
Posts: 17


My Contributions
Okay, I have the entire program I'd needed to do completed except for the problem I'd stated above regarding making the array static within the openWarehouse member function. If no one can help me, I'll just submit it as-is and get docked whatever points for not having it in the openWarehouse function, even though it works perfectly.

Either way, I'd really love to know how to do this. I've been trying all night (though actually, since yesterday) staring at the class textbook, thumbing through C++ for Dummies, and I can't figure it out.
User is offlineProfile CardPM
+Quote Post

horace
RE: C++ Member Functions Help (needing More Help)
4 Dec, 2006 - 11:11 PM
Post #9

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
If BinList[] has to be static inside openWarehouse() the function could return a pointer to it that other functions could then use to access the array, e.g. your original code with changes indicated by // **
CODE

#include <iostream>
#include <string>

using namespace std;

class Inv{
private:
public:
struct Bin{                         // ** make public
      string name;
      int amt;
    
      Bin(string d, int a){
      name = d;
      amt = a;
      }
    };


  Bin* openWarehouse(){            // ** function returns a pointer to BIN
  static Bin BinList[10] = {  Bin("Valve", 10),
                         Bin("Bearing", 5),
                         Bin("Bushing", 15),
                         Bin("Coupling", 21),
                         Bin("Flange", 7),
                         Bin("Gear", 5),
                         Bin("Gear housing", 5),
                         Bin("Vacuum gripper", 25),
                         Bin("Cable", 18),
                         Bin("Rod", 12)  };
   return BinList;                 // ** retur the bin
  }
  
  void displayBins(Bin *BinList){  // ** pass Bin in as a parameter
    for(int i = 0; i < 10; i++)
      cout << BinList[i].name << "\t" << BinList[i].amt << endl;
  }

};

int main(){
    Inv Inventory;
    Inv::Bin *BinList;        // ** pointer to a Bin
    BinList=Inventory.openWarehouse(); // ** get pointer
    Inventory.displayBins(BinList);         // ** print BinList
    system("PAUSE");
    return 0;
}

this now prints
Valve 10
Bearing 5
Bushing 15
Coupling 21
Flange 7
Gear 5
Gear housing 5
Vacuum gripper 25
Cable 18
Rod 12
Press any key to continue . . .

User is offlineProfile CardPM
+Quote Post

bennyboy371
RE: C++ Member Functions Help (needing More Help)
4 Dec, 2006 - 11:22 PM
Post #10

New D.I.C Head
*

Joined: 4 Dec, 2006
Posts: 17


My Contributions
Hmm, well at this point in the book, pointers hadn't been addressed yet, I'm still very bad at them. Either way, it does seem to work so thank you incredibly much. If I can't get it in tonight, I'll learn pointers better from your code. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:16PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month