How to call a function from another class?

This is a discussion on How to call a function from another class? within the C++ Programming forums, part of the General Programming Boards category; Code: //Line.h const int MAX_CHARS = 6; struct Line { bool set (int n, const char* str); void display () ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    29

    How to call a function from another class?

    Code:
    //Line.h
    const int MAX_CHARS = 6;
    
    struct Line {
    
        bool set (int n, const char* str);
        void display () const;
    
    private:
    
        int line_no;
        char lineStr [MAX_CHARS+1];
    
    };
    Code:
    //********** List.h
    
    #include "Line.h"
    
    const int MAX_LINES = 3;
    
    struct List {
    
        void set (int);
        void display() const;
    
    private:
    
        Line listLines [MAX_LINES];
    
    };
    and there are Line.cpp and List.cpp. My List display supposed to call Line display.

    Code:
    void display () const {
    
        Line.display() const;
    
    }
    I dont know how to call this function properly.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Presumably, you would loop over your listLines member array and call the display() member function of each of the Line objects in that array.

    By the way, remember to use header inclusion guards. Also, I suggest that you declare MAX_CHARS and MAX_LINES as static const member variables in the respective classes. Actually, unless you have special reasons otherwise, lineStr probably should just be a std::string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    Thank you, yes it has to loop but how do I call Line display? This way -> Line.display() const; isnt working and lineStr should be std::lineStr?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Something like this:
    Code:
    void display() const {
        for (std::size_t i = 0; i < MAX_LINES; ++i) {
            listLines[i].display();
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    Cool, thank you, I see now how I shouldve called it. But now compiler says that listlines was not declared but it is part of List.h, I dont understand.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Functions in your cpp files probably need List:: or Line:: in front of them for their respective structs.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    Oh, right. Thank you very much laserlight and hk_mp5kpdw! I had no problem with C but struggling with C++ objects and classes/structs, cant still fully understand the concept.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class call to constructor
    By -EquinoX- in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2009, 06:31 AM
  2. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  3. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  4. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  5. Having a class function call a function outside the class
    By Aeroren in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2005, 06:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21