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.
#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.
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.