Thread: Can a function return an interator to main()

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What you need to understand is the scope of variables and how to pass through reference or value depending on what you need.
    In your example, the function should probably take the vector as argument instead of creating it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Code:
    class SomeClass
    {
        public:
        SomeClass() { }
        ~SomeClass() { }
        void Add(int a) { SomeList.push_back(a); }
        std::list<int>::iterator GetFirstElement() { return SomeList.begin(); }
    
        private:
        std::list<int> SomeList;
    }
    
    int main()
    {
        SomeClass Foo;
        Foo.Add(5);
    
        std::list<int>::iterator it = Foo.GetFirstElement();
    
        return 0;
    }
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Concerning Dark_Phoenix's example - I think that if you want to provide:
    Code:
    std::list<int>::iterator GetFirstElement() { return SomeList.begin(); }
    You should also provide:
    Code:
    std::list<int>::const_iterator GetFirstElement() const { return SomeList.begin(); }
    Incidentally, I note that the terminating semi-colon is missing from the class definition.
    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

  4. #19
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    Incidentally, I note that the terminating semi-colon is missing from the class definition.
    Hmm, now how did I miss that..? :P
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM