FAQs about C/C++ language and compiler/linker error messages.

Last Updated 2/17/98


I am getting an error message from the linker saying something like "unresolved external reference" or "undefined" blah-blah. What does this mean?

The linker runs after the compiler has finished. It is trying to assemble your whole program, looking at each point where a function is called, locating the code for that function in either the library object modules or other object modules, and then patching the address of the function code into the call.

This message results when the linker can not find a function that matches the call. Somewhere in the gobbledegook of the message you should be able to make out the name of the function it was looking for (possibly mangled, depending on your IDE), and a list of where the missing function was called from.

Perhaps you forgot to define the function (supply the actual code for it). Perhaps you spelled the name of the function differently in the definition than you did in either the calls or the function prototype. The information in the message should help you track it down.

In what order should I arrange my non-member functions and functions defined outside classes, functions called by main, etc?

You need to keep both the compiler and the human reader happy. See Arranging Declarations and Definitions.

I'm confused about how to overload operators.

Check out the Summary of Operator Overloading.


Can you have two input files open at once?

Yes. In fact you can have multiple input and output files open at the same time, in any mixture that you want.

Each file that needs to be open at the same time needs to have its own variable (object) that represents the file stream. In fact, cin, cout, and cerr are predefined global objects of essentially this sort. You can have as many ifstream and ofstream objects as you want.

Good practice is have a separate objects for each file that plays a different role in the program - there is no need to be stingy by trying to reuse a objects for different files that are used for different things, and doing so can make your program harder to understand or maintain.

But if your program is processing one file after another, and each file is playing the same role in the program (e.g. the data input file), then it certainly makes sense and makes your program more comprehensible to "recycle" the stream objects by associating it with a different file on the disk. When done processing the file, close the stream with the close function, and then open it with the open function using the next file name.

See the input stream handout and the output stream document in the coursepack, and Pohl for more detail.



How do I convert a string to lower case?

Check the string and character library function descriptions in the coursepack. There is a function, tolower, for converting a single character to lower case. Note that it checks the character, and does nothing if it is already lower case, so you don't need to use islower or isupper before calling tolower. But there is no function that converts a whole string - you have to write that yourself - part of the exercise!

Hey! Functions like tolower return ints, not chars! How can I use them?

In C and C++ chars are almost ints; after all, they are just 8-bit integers! C often doesn't know or care about the difference, but in C++ the char type has equal status to the int type. However, both C and C++ convert freely between ints and chars as needed in function calls, returns, and assignments. So:

int i = 'a';

char c = toupper(i);

results in 'A' being stored in the variable c.

How can I compare two strings in a case-insensitive way?

The standard library contains only the case-sensitive strcmp function. But look in the coursepack string demo code (StringStuff.c) for one way to write a comparison function that ignores differences in case - you can use this version, or invent your own.


The compiler says setw is an undeclared identifier, but I #included <iostream.h>. What's wrong? I'm using MSVC.

Check the output formatting document in the coursepack. To use the output manipulators like setw(), you have to #include <iomanip.h>.

We accidently left this out of the demo code. Our excuse is that for faster compilation, the CodeWarrior IDE includes a large number of the standard library headers in precompiled form by default; as a result, we often don't get an error message if we forget to #include a header explicitly. We know this doesn't help! Please accept our apologies.

I get a gazillion error messages from the compiler. What did I do to deserve this?


You probably made a mistake near the beginning of your program that turned the rest of your program into gibberish as far as the compiler was concerned. Leaving off a terminator punctuation, like a double-quote, a '}', or a semicolon is an easy way to trash the rest of your program. Also, messing up a declaration or a #include will often result in multiple error messages. To find the problem, focus on and correct the first few error messages. These probably identify the original problem.

The compiler is giving me an error message on a line that looks perfectly good. Is this a bug in the compiler?

Almost certainly not. This is a common result. Look for an error in the line or lines just before the error message. Often the compiler doesn't know there is a problem until it has gone a little further into your program, so it reports an error on the line where it realized that something is wrong, not where you made the error. Very few compilers can backtrack through your code to figure out what the *real* problem is.


I tried to use a bool variable, but the compiler doesn't recognize it. What do I do?

Best solution: Use a newer compiler - you've got an antique on your hands. Fortunately, this
particular problem has an easy workaround. At the beginning of your program, put the following code that fakes a bool type:

typedef int bool;

const int true = 1;

const int false = 0;

If the compiler complains about your redefining bool, true, and false, stop its whining by spelling them in all caps.

How do I pass an array to a function? How do I return it?

A complicated question that we hope gets introduced in EECS 100.
Read Array Parameters.