Thread: Calling a member function from a member function

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    16

    Calling a member function from a member function

    Hi all, hope you are well.

    There was another thread asking this question along with another, but this question was never answered. Here is my code - incomplete - hopefully i can explain my problem!

    <code>

    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    #define MAX 10
    char choice;
    char type;
    int i;
    // The base class.
    class books {
    public:
    struct database {
    char title[256];
    char author[256];
    int isbn[11];
    char publishername[256];
    char cdrom[4];
    int yearofpub[5];
    float price[5];
    }np[MAX]; // end of struct
    //..........................
    void find(void);
    void enter(int);
    void remove(void);
    void first(void);

    };//end of telephone class
    //------------------------------------

    // derived class goes here.

    class store: public books{
    public:
    void save(void);
    void load(void);
    };//end of derived store class.

    char name[12]="peter minns";
    //------------------------------------
    int main(void) {
    books bdata;
    store storage;
    storage.first();
    cout<<"sagfafs";
    return(0);
    }

    //------------------------------------

    void books::first(void) {
    cout<<"******************************************* ****************\n";
    cout<<"******************************************* ****************\n";
    cout<<"*****************Welcome to the Book***********************\n";
    cout<<"***********************Database************ ****************\n";
    cout<<"******************************************* ****************\n";
    cout<<"******************************************* ****************\n\n\n\n";

    cout<<" What would you like to do? \n";
    cout<<" Select:\n\n";
    cout<<" A To add a book to the database\n";
    cout<<" F To search for a book\n";
    cout<<" R To remove a book\n";
    choice=getchar();
    choice=toupper(choice);
    switch(choice) {

    case 'A': {
    cout<<"you selected 'Add'\n";

    cout<<" enter the title of the book";
    cin.gets(np[i].title);
    break;}

    case 'F': {
    cout<<"you selected 'Search'\n";
    break;}

    case 'R': {
    cout<<"you selected 'Remove'\n";
    break;}
    }

    }

    //---------------------------------------

    void books::enter(int i) {
    cout<<"hi";
    }

    </code>

    sorry if its not set out too well.

    Basically from within the switch menu i want to call member functions (i havent written them yet). The one i have been trying to get to work is the Add option. When they select that option i want to link to void books::enter(int).

    I am still finding my programming feet at the moment so any help would be appreciated.

    Cheers in advance;

    Andy

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    class MyClass
    {
    public:
        fooA();
        fooB();
    };
    
    MyClass::fooA()
    {
        // do something
    }
    
    MyClass::fooB()
    {
        // ...do something
    
        fooA();
    
        // ...do something
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    16
    cheers.

    I tried doing that but it came up with a parse error in books::first(), and i cant see how.

    Is that the only way to do it?

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Can I see the code where you call the function?
    Also pinpoint the line which gives error
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    16
    call the function from:


    void books::first(void) {
    cout<<"******************************************* ****************\n";
    cout<<"******************************************* ****************\n";
    cout<<"*****************Welcome to the Book***********************\n";
    cout<<"***********************Database************ ****************\n";
    cout<<"******************************************* ****************\n";
    cout<<"******************************************* ****************\n\n\n\n";

    cout<<" What would you like to do? \n";
    cout<<" Select:\n\n";
    cout<<" A To add a book to the database\n";
    cout<<" F To search for a book\n";
    cout<<" R To remove a book\n";
    choice=getchar();
    choice=toupper(choice);
    switch(choice) {

    case 'A': {
    cout<<"you selected 'Add'\n";

    enter(int); //-----WANT TO CALL IT HERE
    break;}

    case 'F': {
    cout<<"you selected 'Search'\n";
    break;}

    case 'R': {
    cout<<"you selected 'Remove'\n";
    break;}
    }

    };

    And it says there is a parse error before '(' token on the line containing enter(int);

    hope this is what u mean.

    Andy

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    16
    i dont know what the code tags are

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    int myInt;
    .....
    case 'A': {
        cout<<"you selected 'Add'\n";
      
        //enter(int);
        enter(myInt);  // you want to pass a variable of type int 
        break;}
    When you call the function you want to pass the variable of type int, without the keyword "int". So I suppose you want to set "myInt" (or any int type variable you want to pass) to certain value and pass it to function call.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i dont know what the code tags are
    Well click the link then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    16
    Your a gem mate - prob solved!

    Cheers!

    Andy

  11. #11
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by Andystudent
    Your a gem mate - prob solved!

    Cheers!

    Andy
    how 'bout the problem with code tag??

    way to go salem
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a member function from a different file
    By cdonlan in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2005, 12:50 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM