Thread: Undeclared first use of function

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Unhappy Undeclared first use of function

    It gave me the undeclared first use of function:

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <string>
    using namespace std;
    
    #define max_size = 100;
    
    int main ()
    {
    
        //float instrs[max_size];
     FILE * fin;
     fin = fopen("test.txt", "r");
    unsigned int oneinst;
    int i;
    
    while (!feof(fin))
          {
        fscanf(fin, "%x", &oneinst);
        int    instrs[] = {oneinst};
          }
    
    for(int i = 0; i < 5; i++)
    
            {
    
            int opcode = (instrs[i] >> 26) & 0x3F;
               }  
     
    
        return 0;
    }
    
    For reason when I run this I get the error: `instrs' undeclared (first use this function)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you indented the code properly, the reason might become apparent:
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <string>
    using namespace std;
    
    #define max_size = 100;
    
    int main()
    {
        //float instrs[max_size];
        FILE* fin;
        fin = fopen("test.txt", "r");
        unsigned int oneinst;
        int i;
    
        while (!feof(fin))
        {
            fscanf(fin, "%x", &oneinst);
            int instrs[] = {oneinst};
        }
    
        for (int i = 0; i < 5; i++)
        {
            int opcode = (instrs[i] >> 26) & 0x3F;
        }
    
        return 0;
    }
    As you can see, instrs is local to the while loop, so it does not exist in the for loop. You should have declared it before the loops by uncommenting:
    Code:
    float instrs[max_size];
    Though it looks like instrs should be an array of int, not float. Also, this would then be wrong:
    Code:
    #define max_size = 100;
    You could have written:
    Code:
    const int max_size = 100;
    You should not use feof to control a loop like that (why not just use C++-style I/O?), and then you need to make other changes to get your program to work.
    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
    Nov 2010
    Posts
    2
    Thank you very much laserlight, you have been more than helpful
    Last edited by TheXx11; 11-20-2010 at 01:15 AM. Reason: I already found out the answer to the question posted.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheXx11
    what is this C++ I/O style, what do I need to google search to find out more about it?
    You already #include <iostream>. Search for that.
    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

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. can't compile library
    By pjs111 in forum C Programming
    Replies: 16
    Last Post: 05-24-2010, 01:42 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Consumer program
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-27-2006, 05:21 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM