[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppCompileError

(C++) Compile error

An error given to you by your compiler. It prevents your program from running. Prefer compile-time errors over run-time errors (see reference 1). The naming of these depends on the compiler.

For compile errors when using the Boost C++ Library see Boost compile errors.

Other errors

List of compile-time errors

Overview

syntax error

A common newbie compile error. It means that the compiler could not parse a line of code. This means that you probably made a typo somewhere.

//Error program
const double x = (1.0/(2.0/(3.0/(4.0/5.0))); //Forgot a bracket
const double y = 5.0 + ; //Forgot to write somehing after the plus
for (unsigned int i=0; i<10; ++i)
{
  std::cout << "Hello world" << std::endl;
  //Forgot a closing accolade
  const double z = 8.0 //Forgot semicolon
  return 0;
}


Solution: read the code carefully.

Error: stray '\342' in program

I got this error in:
  1. include <fstream>
int main() { std::ofstream myOut("test.txt"); //ERROR return 0; }


This code is perfectly fine... But I copied it from a Microsoft Word document. Therefore, it has RichText additions. This can be revealed using a plain-text editor (e.g. under LINUX use: 'more main.cpp'). In a plain-text editor this line looked like:
  1. include <fstream>
int main() { std::ofstream myOut(<E2><80><gC>test.txt<E2><80><gD>); //ERROR return 0; }
So, the RichText-ed apostroph " should be replaced with a 'plain' apostroph.

[C++ Error] E2451 Undefined symbol 'cout'

This error occurs when the program does not know the command 'cout' (which is the reason why it thinks it is a 'symbol'(e.g. an integer). #include 'iostream.h' to solve this. See also the Hello World program.

[C++ Error] UnitMain.cpp(34): E2268 Call to undefined function 'Assert'

You must be cautious when defining your own assert statement. Especially when als #defineing your own #include guards. Therefore, do the following: - Check if your own assert statement header file can be found in the right folder - Give your #include guard an original name. Do not:

//NO!!!
  1. ifndef assertH
  2. define assertH
//Your assert
  1. endif


But
  1. ifndef BilderbikkelAssertH
  2. define BilderbikkelAssertH
//Your assert
  1. endif


For example, the assert statement used by the Boost C++ Library use the SAME #include guard! This can cause your own assert statement to not be included.

[C++ Error] _algo.c(432): E2268 Call to undefined function 'rand'

The compiler takes you to the header _algo.h, to the blue line below:
template <class _Distance>
inline _Distance __random_number(_Distance __n) {
  1. ifdef _STLP_NO_DRAND48
return rand() % __n;
  1. else
return lrand48() % __n;
  1. endif
}
It cannot find rand when you are not using namespace std. I solved it by changing the blue line to
  return std::rand() % __n;


Nasty compile error with boost::shared_ptr

[C++ Error] shared_ptr.hpp(124): E2034 Cannot convert 'Y *' to 'BirdBase *'
[C++ Error] shared_count.hpp(82): E2450 Undefined structure 'sp_counted_impl_p<Y>'
[C++ Error] shared_count.hpp(82): E2315 'sp_counted_impl_p<Y>()' is not a member of 'sp_counted_impl_p<Y>', because the type is not yet defined
[C++ Error] shared_count.hpp(82): E2034 Cannot convert 'sp_counted_impl_p<Y> *' to 'sp_counted_base *'
[C++ Error] checked_delete.hpp(32): E2034 Cannot convert 'Y' to 'bool'


caused by:

//Removes the boost::shared_ptr<BirdBase> mMale from the class Couple
boost::shared_ptr<BirdBase> Couple::getMale()
{
  boost::shared_ptr<BirdBase> male(mMale);
  mMale.reset(0); //Set pointer to NULL
  return male;
}


The compile error is solved by changing this to:

//Removes the boost::shared_ptr<BirdBase> mMale from the class Couple
boost::shared_ptr<BirdBase> Couple::getMale()
{
  boost::shared_ptr<BirdBase> male(mMale);
  mMale.reset(); //Remove the 0
  return male;
}


[C++ Error] E2092 Storage class 'static' is not allowed here

When you make a class member variable static, then use the keyword static ONLY in the class declaration.

//Declaration
class MyClass
{
  static double myDouble;
  //Stuff
};
//Initialization
double MyClass::myDouble = 0.0;
//THIS ERROR: static double MyClass::myDouble = 0.0;


[C++ Error] E2138 Conflicting type modifiers

To quote the C++ Builder Help: 'This occurs when a declaration is given that includes more than one addressing modifier on a pointer or more than one language modifier for a function'. This can be due to, e.g.

[C++ Error] exception(78): E2141 Declaration syntax error

Takes you to the header exception to the line
_STLP_BEGIN_NAMESPACE
I do not know exactly what causes this, but I have observed the following: in my automatically created Unit, I had the following code:
//---------------------------------------------------------------------------
  1. ifndef UnitParametersH
  2. define UnitParametersH
//--------------------------------------------------------------------------- class Parameters { //Stuff }; void Dot(const Parameters& parameters) //Forgot semicolon!!!
  1. endif
After adding the semicolon, it worked again.

[C++ Error] checked_delete.hpp(32): E2450 Undefined structure 'MyStructure'

Your compiler will take you to this line:
template<class T> inline void checked_delete(T * x)
{
    // intentionally complex - simplification causes regressions
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ]; //This line
    (void) sizeof(type_must_be_complete);
    delete x;
}


Congratulations, as you have just let boost find out a nasty run-time error for you [1], thanks to boost::checked_delete. It is caused by you forward declaring a class in a header file, without #including the class's header file in the implementation (.cpp) file. After #including it the error is gone.

See boost::checked_delete why you otherwise would have had a nasty run-time error.

[C++ Error] _config.h(488): E2141 Declaration syntax error

You are taken to the line below, in the header file exception
_STLP_BEGIN_NAMESPACE


This is caused by a combination of a #include guard and a function declaration without a terminating semicolon ;, e.g. in the code below:

  1. ifndef UnitHelperH
  2. define UnitHelperH
void sayHello() //FORGOT SEMICOLON[/b]
  1. endif


Add the semicolon and everything will be fine.

'operator<<' not implemented in type 'ostream' for arguments of type 'std::string'

Caused by the following simple code:

  1. include <iostream>
  2. include <vector>
int main() { std::string s = "Hello"; std::cout << s; }


The answer is to #include <string>, which contains the definition of the function lacking.

  1. include <iostream>
  2. include <vector>
#include <string> int main() { std::string s = "Hello"; std::cout << s; }


References



last edited (July 3, 2007) by bilderbikkel, Number of views: 16122, Current Rev: 23 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.