Problem with a function....scope

This is a discussion on Problem with a function....scope within the C++ Programming forums, part of the General Programming Boards category; Ok, I should probably just wait and look at it again tomorrow before posting this because I know I should ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    15

    Problem with a function....scope

    Ok, I should probably just wait and look at it again tomorrow before posting this because I know I should be able to figure it out. But.......

    I get the error " 'fail' undeclared " It is declared in the extract_fails function and I realize it is out of scope in main() but I can't figure out to get it to work. Shouldn't I be able to reference it somehow. I have posted only the extract_fails() and the relevent portion of main(). If more is needed, I'll post the rest.

    Thanks. I hope my question is clear...I'm getting cross-eyed.

    Code:
    // include files........
    
    vector<Student_info> extract_fails(vector<Student_info>& students)
    
    {
     vector<Student_info> pass, fail;
     vector<Student_info> students;
    
     for (vector<Student_info>::size_type i = 0;
                       i != students.size(); ++i)
      if (fgrade(students[i]))
         fail.push_back(students[i]);
      else
          pass.push_back(students[i]);
    
     students = pass;
     return fail;
    }
    
    
    
    int main()
    {
     // more code here (students and grades being entered)
    
    
      extract_fails(students);
    
     vector<Student_info> fail;  //edit: oops.  not in the code
     for (int i = 0; i != fail.size(); ++i)        //list failing students
         cout << endl << fail[i].name;
    
    
    
    system("PAUSE");
    return 0;
    }
    Last edited by MedicKth; 09-22-2003 at 01:43 AM.

  2. #2
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Of course you cannot access a local variable from another function. But why don't you just use the return value of your function anyway?

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    Ok. Feeling stupid but I got it. I knew I should've just put away 'till morning. Thanks for your help though. Just needed a kick in the head.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 07:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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