undeclared identifier

I keep getting this error and I know it is a simple problem with my syntax but I cant find a good answer to what I am missing. If anyone could help me out it would be much appreciated. The error is commented into the code. Many thanks.

What the code is doing isnt really that important, but if you want to know its just a simple program to decode hex inputs of binary code into machine code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include <iostream>
#include <string>

int main(int argc, char *argv[])
{
    // argc carries the number of command-line arguments passed in.
    // argv is std::string array containing all the arguments passed in.
    // The first of the arguments (i.e., argv[0]) is the name of the
    // executable (e.g. a.out), which we aren't printing out here.
    
    
    // Call appropriate class based on call number
    for ( int i = 1; i < argc; ++i )
    {
        std::string tempStr = argv[i];
        std::string biStr = toBinary(tempStr.at(1) + tempStr.at(2) + tempStr.at(3));
        char type = tempStr.at(0);
        
        
        //switch to call method based on type.
        switch (type)
        {
                
            case '0':
                BR(biStr); // I will get an error here saying BR() is
                break;     // an undeclared identifier, but it is declared below
                
            case '1':
                ADD(biStr);// Same here. (it is declared in my version of code)
                break;
                
            case '2':
                LD(biStr);
                break;
                
         // ... more code here that isn't important... 
                
            case 'f':
                TRAP(biStr);
                break;

            default:
                break;

        } // end switch
     
    } // end loop
    
    return 0;
} // end main


void BR(std::string bi)
{
    if(strcmp(bi.substr(0,3), "111"))
       {
           cout << "BR, #" << biToInt(bi.substr(3,9));
       }
       else
       {
           cout << "BR";
           
           if(bi.at(0) == '1')
           {
               cout << "n";
           }
           if(bi.at(1) == '1')
           {
               cout << "z";
           }
           if(bi.at(2) == '1')
           {
               cout << "p";
           }
           
           cout << ", #" << biToInt(bi.substr(3,9)) << "\n";
       }
}
     
You need a forward declaration. That means putting a declaration of the function somewhere before you use it. What you have is a definition of a function at the bottom, and it is just fine where it is as long as you forward declare it.

Put void BR(std::string bi); on line 3. This is you making a promise to the compiler that such a function exists and you have it implemented somewhere else that'll get linked in later. That way, the compiler won't complain when it comes across a call to BR inside of main().

If you have other functions down below main() that you use inside of main(), then you need to forward declare those, too.
Last edited on
Cool, thank you. Would this also work if I put the main method at the bottom?
Yes. A function by itself acts sort of like its own declaration, so if you move main below all the functions it uses, it should compile.
An unrelated comment: when using std::string, there is no need to use strcmp(), you can compare directly using the relational operators such as == and so on.
+1 great thats awesome, I was just starting to google why strcmp() wasn't working haha! I am coming from programing a lot in java and matlab so I'm working out the syntax. Only been on this project for about 20min.
Topic archived. No new replies allowed.