Computer Science 252: Compiler Errors

Here's a list of common compiler errors and what they mean. See also the link errors later in this page (link errors show up as ``undefined references'').

ANSI C++ forbids declaration `vector' with no type

This means the programmer forgot to #include <vector>.

passing `const XXX' as `this' argument of `YYY' discards qualifiers

Const member functions have the restriction that they cannot modify their object. Therefore, any member functions of the object that are called from within the function, either directly or indirectly, must also be a const. Otherwise, there is no guarantee that the non-const function will not modify the object and violate the const function's restriction. This also applies to objects declared as const. You cannot call non-const member functions for these objects. In this error message, XXX is the const object you are calling with and YYY is the const member function being called.

Warning, but they went away by themselves

Warnings during compilation don't hinder compilation from completing, so a second compilation will note that the target is `up to date.`

Nevertheless, warnings always mean something, and they are usually dangerous to ignore. Particularly, warnings about something being `implicitly declared` means the compiler never saw a declaration for the function or variable in question, so you either forgot to declare it, or declared it wrong.

Cannot pass objects of type

adventure.cc: In function `void adventure(class Room *)':
adventure.cc:52: warning: implicit declaration of function `int
parse_direction(...)'
adventure.cc:52: warning: cannot pass objects of type
`basic_string,__default_alloc_template
>' through `...'
The compiler is looking for a non-member function parse_direction(), doesn't find it (probably because it was declared wrong), and implicitly declares a function that returns a type int and doesn't accept a string. Add the correct declaration, and both messages go away.

Appointment.cc:9: no matching function for call to `Duration::Duration ()'

		
Appointment.cc: In method `Appointment::Appointment()':		
Appointment.cc:9: no matching function for call to `Duration::Duration()'
Time.h:17: candidates are: Duration::Duration(const Duration &)
Time.h:8: Duration::Duration(int)

When you define the constructor for Appointment:

Appointment::Appointment() { /* stub */ };

this default constructor will, as part of what it does, initialize all the private variables of Appointment by calling default constructors, so it will call Time(), Date(), Duration() and char*(). The first two and the last one work fine. However, for the Duration class (at least at the time when you copied it) there *is* no constructor Duration(). The compiler is suggesting two alternatives for you:

Duration(const Duration &)

which is the copy constructor, which needs another Duration, and

Duration(int)

which needs an integer. The solution is to explicitly tell in the Appointment constructor that you want it to call the second one:

Appointment::Appointment() : dur(0) { /* stub */ };

where dur is the name of the private variable of type Duration.

conversion from ... to non-scalar type ... requested

AppointmentBook.cc:134: conversion from `Duration (Appointment::*)() const'
to non-scalar type `Duration' requested.

The Duration (Appointment::*)() const is a reference to a member function of Appointment. Some Appointment::function() that returns a Duration has been called by saying something like

x = a.function;

This is an attempt to set x to be the function a.function, rather than the result of the function, which is what you most likely wanted:

x = a.function();

Parentheses matter!

Appointment.h:12: `Date' was not declared in this scope

This means the type `Date' (or ostream or whatever) was not declared before this line was seen by the compiler. This may happen while compiling (for example) driver.cc; you need to #include "Date.h" before including any file that needs Date (for instance Appointment.h).

Similarly if it says `ostream' was not declared in this scope, it means that #include didn't occur or didn't occur early enough.

Another possibility is that you neglected to import the name from another namespace (such as std) with a using declaration.

The order in which files are declared does make a difference.

type specifier omitted for parameter

ApptBook.h:46: type specifier omitted for parameter
ApptBook.h:46: parse error before `&'
ApptBook.h:62: syntax error before `;'
ApptBook.h:63: syntax error before `;'
void ApptBook::PrintBook(Date d, ostream &out)

This problem typically has to do with the order in which the header files are included. If you include Date.h, Time.h, Appt.h, ApptBook.h and then iostream.h, when the compiler pulls in ApptBook.h it has yet to see a declaration of what an ostream is. So when ApptBook.h asks for an ostream parameter, this is as good as a parameter with no type.

jump to case label crosses initialization

menu-driver.cc:166: jump to case label
menu-driver.cc:143:   crosses initialization of `class Duration dur'

Any local variables inside of switch statements should be declared inside local blocks:

       case 'A':
         { Duration dur;
           ...
         }
       case 'D':
         { ...
Otherwise, the compiler considers them in scope also after the close of the case label, but notes that you could jump to case 'D': without ever seeing the initialization.

xxx.cc:nn: default argument given for parameter m of ...

This means that in the .cc file, you gave the default parameter values again (a no-no). Only give default parameter values in prototypes (in the .h file).

control reaches end of non-void function

A void function needs no return value; a non-void function (i.e., a function that returns something like a bool, and Appointment, or any other type) needs a return value. If there is a way to go through the entire code in the function without executing a return statement, that is a problem; the compiler signifies that it's found a way to do this by saying that control reaches end of non-void function without returning a value.

warning: statement with no effect

Do not ignore this warning: it probably means you used == instead of = in an assignment:

	date == new_date;	//!! WRONG!

warning: ... `const char *' ... discards const

This is not a serious warning; it means that you have a constant character array that is being returned or passed as char * which allows it to be mutated. It would be better to figure out whether the return or parameter type should be changed to const char *, or whether the character array really is modified (probably a mistake).

no match for `const Time & + Time'

The compiler gives the possibilities (overloadings) of this operator:

 
[somewhere]: candidates are: operator +(Time, Duration)
[somewhere]:                 operator +(Duration, Time)
[somewhere]:                 operator +(Duration, Duration)
This means you are trying to add two things for which no operator has been defined. G++ suggests things that you can add.

In general, if you get a no match error, look at all the possibilities. Perhaps one is the one you meant, in which case, you need to figure out why it didn't match. Usually it is because a parameter is missing, or parameters are in the wrong order. Or perhaps you were calling the wrong function, for example:

	cout >> "Please enter a number: ";    // !! wrong operator!

warning: value computed is not used

This error message when attached to a catch line usually means the type of the caught exception was not a reference. it should be a reference, for complicated reasons. If you have a regular value, it means the actual exception will be stripped down to fit in the value you specify, often losing the error message that is available. If you don't care what the result is, don't name the exception and then the warning message will go away. But if you do wish to call, for example what(), then it should be a reference, not a value.


Link errors

Virtual Table error

Sometimes when you compile a collection of classes like the classes for the project, you get a message like this:

 Undefined                       first referenced
  symbol                             in file
 Bag virtual table                   Bag.o
 ld: fatal: Symbol referencing errors. No output written to mud
 collect2: ld returned 1 exit status
 make: *** [mud] Error 1
(Sometimes the error says "vtable" instead of "virtual table".) Such errors occur when you define a virtual function in the header file but fail to implement the function in the implementation file. It will always be the first non-inline, non-pure virtual function of the class mentioned (here Bag).

elf errors when linking

If you get messages about elf errors because the format of the object (.o) file is "unrecognized" this is likely because you are over quota. When the compiler tries to create the object file, it creates an empty file because you don't have available space. When the linker tries to load this file, it realizes that this is an empty file and reports this as a missing symbol table and an unrecognized format.

Symbol referencing errors when linking an executable

This means you neglected to define the named symbols. They are declared in the class definition, but never defined. Sometimes this happens if you forgot to define the member function as a member. If you have a non-member function declared, it doesn't take the place of an undefined member function.

Another possibility is that your makefile is neglecting to link in all the object files it should. Fix the Makefile.

Symbol referencing errors when compiling a .cpp file

If, when trying to compile a file.cc into a file.o, you get symbol referencing errors, there is something wrong in your Makefile. Symbol referencing errors only come during the linking phase, so for some reason the Makefile thinks you want to link.

One, subtle way this can happen is if you say make program and there is no target for program in the Makefile. Make will, before giving up, try to see if it has a default rule to make program. One of the default rules say you can make it if you have a file program.cc by doing:

cc program.cc -o program

which will use the C-compiler to create a program directly from the .cc file, which is not what you wanted to do. Create a target for program that uses g++ to link all the necessary object files, and the symbol errors will go away.

symbol __ls__FR7ostreamR11Appointment undefined

An operator (probably <<) was declared and used but a body was never given. Operators functions need bodies just like any other functions.

You get better error messages if you compile with -g, and/or pass the error messages through c++filt. For example:

miller.cs 23 % g++ tmp.cc
Undefined                       first referenced
 symbol                             in file
__3Foo                              /var/tmp/cc7waaxC1.o
__3Fooi                             /var/tmp/cc7waaxC1.o
__ls__FR7ostreamRC3Foo              /var/tmp/cc7waaxC1.o
_._3Foo                             /var/tmp/cc7waaxC1.o
main                                /usr/local/lib/gcc-lib/i386-pc-solaris2.7/2.8.1/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
Pretty ugly right? Try by piping errors through c++filt using |&
miller.cs 24 % g++ tmp.cc |& c++filt
Undefined                       first referenced
 symbol                             in file
Foo::Foo(void)                              /var/tmp/ccKbaqzC1.o
Foo::Foo(int)                             /var/tmp/ccKbaqzC1.o
operator<<(ostream &, Foo const &)              /var/tmp/ccKbaqzC1.o
Foo::~Foo(void)                             /var/tmp/ccKbaqzC1.o
main                                /usr/local/lib/gcc-lib/i386-pc-solaris2.7/2.8.1/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
Much better now?

undefined reference to `Duration::Duration(int)'

This means that you used a constructor that was properly declared, but you didn't include a definition in a .o file. Most likely the link line doesn't include Time.o.

undefined symbol 'main'

[classfile].cpp is not a standalone program: it cannot be linked by itself. It must be linked with (at least) a main program. When compiling files that do not stand on their own, make sure to always use the "-c" flag so that the compiler just creates a .o file that can be linked with other files. See the first lab for examples and more details.

Confusing Error Messages

You may occasionally be confronted with error messages such as the following:

ld.so.1: a.out: fatal: libstdc++.so.2.8.1.1: open failed: No such file or directory

It is possible that some damage has been done to your system login files. (this is not necessarily your fault). To fix, try typing "fix-dot" on the miller command line, exit the system, and then log in again.