CS 1124 — Object Oriented Programming

Strange Error Messages

This file is different from the file of "Common Errors" in that it is intended to highlight error messages that might be confusing. The messages are generated by Visual C++. Let me know if you come up with other examples.

syntax error : missing ';' before '&'

Suppose we have the following two files:

// file: Cat.h
class Cat {
public:
	friend ostream& operator<< (ostream& os, const Cat& c);
private:
	int x;
};
// End file Cat.h


// file: main.cpp
#include "Cat.h"
int main() {
	Cat c;
	return 0;
}

Compiling them will result in an error message "syntax error : missing ';' before '&'" on the line in the header file that declares operator<< as a friend. The real problem is that the symbol "ostream" is not being recognized. In this case the reason is because Cat.h needs to include the iostream library. Of course we then also need to qualify the symbol ostream to show that it comes from the std namespace, as in:

// file: Cat.h

#include <iostream>
class Cat {
public:
	friend std::ostream& operator<< (std::ostream& os, const Cat& c);
private:
	int x;
};
// End file Cat.h


unexpected Cat ('

  // file: Cat.h 
  #include <string>

  class Cat { 
  public:
     Cat (string aName) {name = aName;}
  private:
     string name;
  };
  // End file Cat.h 

The error is reported for the line holding the constructor. The error has nothing to do with Cat. This is similar to the previous example. What's going wrong is that the class name string is not being recognized. string is in the namespace std. Since this is a header file we should modify string with std::, resulting in

  // file: Cat.h 
  #include <string>

  class Cat { 
  public:
     Cat (std::string aName) {name = aName;}
  private:
     std::string name;
  };
  // End file Cat.h 

'Vector' : illegal copy constructor: first parameter must not be a 'Vector'

If we define a copy constructor, you might think the type of the argument should be the same as the class. But that's not quite right. The above error message was generated when I tried to declare a copy constructor for the class Vector, passing it a parameter of type Vector:

Vector(Vector);

. I should have used a constant reference to a Vector, as in:

 Vector(const Vector&);

Home


Maintained by John Sterling (jsterling@poly.edu). Last updated January 23, 2011