Examples of C++ compile-time errors

Below we show a simple, correct C++ program, and then the compile-time errors and warnings generated by a variety of simple mistakes and typos. Each program includes a comment flagging the error, and below the program the compile-time error(s)/warning(s) are shown in comments.

Studying the compiler messages generated by the different kinds of mistakes will help you to become much more effective at debugging your code.

Note that the first thing the compiler tells you with each message is the name of the file being compiled and the function, line number, and column number where the compiler first realized there was an error.

With warnings, at the end of the message the compiler will also tell you which kind of warning flag generated that message, e.g. [-Wuninitialized] for potentially uninitialized variables.

It's worth noting how confused the compiler can become when you forget either a bracket or a double quote.

// missing #include 

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err0.C: In function 'int main()':
// err0.C:8:33: error: 'printf' was not declared in this scope
// err0.C:9:18: error: 'scanf' was not declared in this scope
#include <cstdio>

int sum(int a, int b)  // missing ;

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err1.C:5:1: error: expected initializer before 'int'
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
   // missing }

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err2.C: In function 'int main()':
// err2.C:17:1: error: a function-definition is not allowed 
//              here before '{' token
// err2.C:21:1: error: expected '}' at end of input
#include <cstdio>

int sum(int a, int b);

int main()
   // missing {
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err3.C:7:4: error: expected initializer before 'int'
// err3.C:8:10: error: expected constructor, destructor, 
//              or type conversion before '(' token
// err3.C:9:9: error: expected constructor, destructor, 
//              or type conversion before '(' token
// err3.C:10:9: error: expected constructor, destructor, 
//              or type conversion before '(' token
// err3.C:11:4: error: 'z' does not name a type
// err3.C:12:10: error: expected constructor, destructor, 
//              or type conversion before '(' token
// err3.C:13:4: error: expected unqualified-id before 'return'
// err3.C:14:1: error: expected declaration before '}' token
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b);   // extra ;
{
   int temp;
   temp = a + b;
   return temp;
}

// err4.C:17:1: error: expected unqualified-id before '{' token
#include <cstdio>

int sum(int a, int b);

int main()
{
   int X, y, z;   // X not x
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err5.C: In function 'int main()':
// err5.C:9:17: error: 'x' was not declared in this scope
// err5.C:7:8: warning: unused variable 'X' [-Wunused-variable]
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, z;  // missing y
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err6.C: In function 'int main()':
// err6.C:10:17: error: 'y' was not declared in this scope
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%f", &x);  // %f not %d
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err7.C: In function 'int main()':
// err7.C:9:18: warning: format '%f' expects argument of type 
//          'float*', but argument 2 has type 'int*' [-Wformat]
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", y);  // missing & before y
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err8.C: In function 'int main()':
// err8.C:10:17: warning: format '%d' expects argument of type 
//             'int*', but argument 2 has type 'int' [-Wformat]
// err8.C:10:18: warning: 'y' may be used uninitialized in this 
//             function [-Wuninitialized]
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n");  // missing parameter z (to go in the %d spot)
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err9.C: In function 'int main()':
// err9.C:12:17: warning: format '%d' expects a matching 'int' 
//              argument [-Wformat]
// err9.C:7:14: warning: variable 'z' set but not used 
//              [-Wunused-but-set-variable]
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   // missing return statement
}

// err10.C: In function 'int sum(int, int)':
// err10.C:18:8: warning: variable 'temp' set but not used 
//               [-Wunused-but-set-variable]
// err10.C:21:1: warning: no return statement in function 
//               returning non-void [-Wreturn-type]
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

void sum(int a, int b)  // void instead of int return type
{
   int temp;
   temp = a + b;
   return temp;
}

// err11.C: In function 'void sum(int, int)':
// err11.C:16:22: error: new declaration 'void sum(int, int)'
// err11.C:3:5: error: ambiguates old declaration 
//              'int sum(int, int)'
// err11.C:20:11: error: return-statement with a value, 
//              in function returning 'void' [-fpermissive]
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("d", &y);  // missing the % in format string
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err12.C: In function 'int main()':
// err12.C:10:17: warning: too many arguments for format 
//          [-Wformat-extra-args]
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(int x, int y); // shouldn't put data types in the call
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err13.C: In function 'int main()':
// err13.C:11:12: error: expected primary-expression before 'int'
// err13.C:11:19: error: expected primary-expression before 'int'
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x y, z;  // missing , between x and y
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err14.C: In function 'int main()':
// err14.C:7:10: error: expected initializer before 'y'
// err14.C:9:17: error: 'x' was not declared in this scope
// err14.C:10:17: error: 'y' was not declared in this scope
// err14.C:11:4: error: 'z' was not declared in this scope
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x)   // missing ; after statement
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err15.C: In function 'int main()':
// err15.C:10:4: error: expected ';' before 'scanf'
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n);  // missing " at end of text
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err16.C: In function 'int main()':
// err16.C:8:49: error: expected ')' before 'at'
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d, &y);   // missing " at end of format string
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err17.C: In function 'int main()':
// err17.C:10:35: error: expected ')' before 'at'
// err17.C:7:11: warning: unused variable 'y' [-Wunused-variable]
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   //  forgot to read in a value for x 
   scanf("%d", &y);
   z = sum(x, y);
   printf("%d\n", z);
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err18.C: In function 'int main()':
// err18.C:11:17: warning: 'x' may be used uninitialized in this
//                function [-Wuninitialized]
#include <cstdio>

int sum(int a, int b);

int main()
{
   int x, y, z;
   printf("Enter two integers\n");
   scanf("%d", &x);
   scanf("%d", &y);
   z = sum(x, y);
   // set a value for z, but then never did anything with it 
   return 0;
}

int sum(int a, int b)
{
   int temp;
   temp = a + b;
   return temp;
}

// err19.C: In function 'int main()':
// err19.C:7:14: warning: variable 'z' set but not used 
//            [-Wunused-but-set-variable]