Thread: How to call a function from another class?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Something like this:
    Code:
    void display() const {
        for (std::size_t i = 0; i < MAX_LINES; ++i) {
            listLines[i].display();
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,817
    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, 07: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