Compiling with Visual Studio 2005, on Windows XP. I add the following headers to my "stdafx.h" file like so:
#include <atlbase.h>
#include <atlcom.h>
#include <atlcore.h>
#include <atlstr.h>
(technically the same error appears with just atlbase.h included) which produces the following errors:
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
error C2062: type 'double' unexpected
in the following code:
struct CheckValue : public unary_function<pair<MetID,double>,void>{
CheckValue(double _expected) : m_Expected(_expected){}
inline void operator()(const pair<MetID,double> &_val){
m_CheckList.push_back( near( _val.second ) ? 0 : 1 );
}
inline bool near(double _val){ //here is location of both errors
return ( m_Expected - m_Epsilon < _val ) || ( _val < m_Expected + m_Epsilon );
}
const static double m_Epsilon;
const double m_Expected;
list<int> m_CheckList;
};
const double CheckValue::m_Epsilon = 0.00001;
Without those lines added, no problems. Anyone want to venture a guess as to why? I'm scratching my head here and can't continue writing unit tests without those include files.
Run it through the preprocessor and see what you get. Maybe near is defined to something, or somesuch problem. (Hard to say without line numbers)
(I believe /E or /EP is the correct switches, but you can find it in the VS GUI options for a single file too..)
near is a macro defined in WinDef.h. When you include ATL headers, they probably indirectly include WinDef.h. Hence the error.
If you really need those headers, either stop using identifier near, or #undef it right after all headers are included.
The order of includes can sometimes cause weird things to happen, and indeed, such "known bugs" have happened in the past with VC++. Try shuffling the includes around to see if it helps.
Related
I'm attempting to build source files of an open source C++ library written by someone else. This is being done on Windows with Cygwin's mingw-w64 compiler. The only compiler option I'm attaching is -std=gnu++11 since the library depends on some C++11 features.
Here are some examples of code in their library that appears to be causing issues:
CPScalar & Abs()
{
m_dValue = std::abs(m_dValue);
return *this;
}
//...
template<typename Unit>
bool SEScalarQuantity<Unit>::Set(const SEScalarQuantity<Unit>& s)
{
if (m_readOnly)
throw CommonDataModelException("Scalar is marked read-only");
if (!s.IsValid())
return false;
m_value = s.m_value;
m_isnan = (std::isnan(m_value)) ? true : false;
m_isinf = (std::isinf(m_value)) ? true : false;
m_unit = s.m_unit;
return true;
}
I get compiler errors on the std:: qualified functions above. The compiler error on the m_dValue = std::abs(m_dValue); line is
error: call of overloaded 'abs(double&)' is ambiguous
Which made me think it could be related to the question of whether std::abs(0u) is ill-formed as well as this answer to a similar SO question.
m_isnan = (std::isnan(m_value)) ? true : false; and the following line gives me
error: expected unqualified-id before '(' token
There are countless other uses of std:: that the compiler doesn't complain about. If I remove all of the std:: qualifiers in the statements that are giving me errors, the code compiles beautifully.
Thing is, this open source project is (presumably) being built by others without modification, so what am I missing here?
Add #include <cmath> to the file being compiled. The problem is that there are a couple of overloads of std::abs for integer types that are declared in the header <cstdlib> and the compiler is complaining that it doesn't know which of those to use. What's needed, though, is std::abs(double), and that's declared in <cmath>.
The reason that this code works with some compilers and not others is probably that there is a declaration of std::abs(double) coming in from some header other than <cmath>. That's allowed, but not required.
This code compiles fine:
#include <curand_kernel.h>
#include <thrust/sort.h>
int main(void) {
return 0;
}
while this gives compilation errors:
#include <thrust/sort.h>
#include <curand_kernel.h>
int main(void) {
return 0;
}
CUDA 6 gives me three errors:
... curand_mtgp32_kernel.h(315): error: calling a __device__ function("__syncthreads") from a __host__ __device__ function("curand_mtgp32_single") is not allowed
... include/curand_mtgp32_kernel.h(373): error: calling a __device__ function("__syncthreads") from a __host__ __device__ function("curand_mtgp32_single_specific") is not allowed
... curand_kernel.h(392): warning: missing return statement at end of non-void function "__curand_uint32_as_float"
on CUDA 5 I get only:
... curand_kernel.h(405): warning: missing return statement at end of non-void function "__curand_uint32_as_float"
where "..." is my CUDA install directory. It seems all these areas are wrapped in macros testing whether __CUDA_ARCH__ is defined, or what its value is. I am seeing this problem only with sort.h, not with the various other thrust headers I am including (host/device vectors, scan, scatter+gather and some fancy iterators).
I cannot (easily and sensibly) make all headers in my current project self-sufficient, given this behaviour. Can anyone explain to me why the order matters here, or if it is a bug or 'feature' of one of these headers?
It's a known issue (it's actually 2 separate issues). It was identified too close to the CUDA 6 release to fix in CUDA 6.
It should be fixed in a future release.
In the meantime, you should be able to work around it by reversing the order of inclusion of those header files, or else you can try updating to the current Thrust master branch.
I have a C++ project in VS with more or less 100 files (each file is a class). I modified one a couple of days ago, adding some declarations, and now I can't compile and it gives a lot of errors and this one lastly:
error count exceeds 100; stopping compilation
Posting the errors seems useless, but here are some (the are all pretty much the same):
error C2275: 'btTransform' : illegal use of this type as an expression
error C2275: 'btVector3' : illegal use of this type as an expression
error C2275: 'btVector3' : illegal use of this type as an expression
error C2275: 'btVector3' : illegal use of this type as an expression
error C2504: 'Platform' : base class undefined
error C2535: 'btAlignedObjectArray<T>
Note than most of the mentioned errors shouldn't be errors, and IntelliSense shows no error in the error list output. And I am completely sure I forgot a ; or something similar.
What should I do? Also I am working with a lot of stuff, and I forgot which file I modified. I browsed through most of them and couldn't find anything.
Here is the complete list: http://pastebin.com/1CD9fGgn (it is so long that it doesn't fit here)
As requested:
Player.h
#pragma once
#include <Ogre.h>
#include "BtOgreGP.h"
#include "BtOgrePG.h"
#include <OISKeyboard.h>
#include <OISJoyStick.h>
#include "BulletCollision\CollisionDispatch\btGhostObject.h"
#include "balance.h"
#include "WorldGenerator.h"
#include "Keys.h"
#include "PlayerController.h"
using namespace Ogre;
class Player
{
public:
Player(Root*, SceneManager*, RenderWindow*);
~Player(void);
Camera* mCamera;
void update(const FrameEvent&);
bool isTouchingBelow();
// bool isJumping();
btPairCachingGhostObject* getGhostObject()
{
return mGhostObject;
}
void clearObjectTouchingNormal()
{
mNormals->clear();
}
void addObjectTouchingNormal(btVector3* vector)
{
mNormals->push_back(*vector);
}
btAlignedObjectArray<btVector3> getObjectTouchingNormal()
{
return *mNormals;
}
private:
btAlignedObjectArray<btVector3>* mNormals;
double mTimeLastJump;
WorldGenerator* mGenerator;
bool mPressJumpLastUpdate;
// btAlignedObjectArray<btVector3> getObjectTouchingNormal();
Vector3 mLastVectorVelocity;
//SceneNode* mCameraHelper;
SceneNode* mMainNode;
SceneNode* mBodyNode;
SceneNode* mCameraPivot;
SceneNode* mCameraYaw;
SceneNode* mCameraPitch;
SceneNode* mCameraHolder;
SceneManager* mSceneManager;
BtOgre::DebugDrawer* mDebugDrawer;
//btRigidBody* mPlayerBody;
btQuaternion* mDefaultQuaternion;
Vector3 mStartPosition;
PlayerController* mKinematicController;
btPairCachingGhostObject* mGhostObject;
//bool mIsJumping;
Radian mLastRotation;
btVector3 mBodyDimensions;
/*bool mCameraCenterMovementFlag;
Radian mCameraCenterYaw;*/
};
class ClosestNotMe : public btCollisionWorld::ClosestRayResultCallback
{
protected:
btRigidBody* mMe;
public:
ClosestNotMe (btRigidBody* me) : btCollisionWorld::ClosestRayResultCallback(btVector3(0.0, 0.0, 0.0), btVector3(0.0, 0.0, 0.0))
{
mMe = me;
}
virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult& rayResult,bool normalInWorldSpace)
{
if (rayResult.m_collisionObject == mMe)
return 1.0;
return btCollisionWorld::ClosestRayResultCallback::addSingleResult (rayResult, normalInWorldSpace);
}
};
Globals.h
#pragma once
#include <Ogre.h>
#include <vector>
#include "Player.h"
enum GameDifficulty
{
GD_EASY,
GD_NORMAL,
GD_HARD
};
class GlobalVariables
{
public:
static std::vector<Player*> players;
};
Scroll to the top of your error list, and deal with that one. If you're in Visual Studio, you can compile it and hit Ctrl-Shift-F12.
If you make some error in syntax (my usual one is an unmatched quote or brace), the compiler can lose its context, so everything from that point forward becomes unintelligible to it. The only way you'll get through it is to correct the first error found; and then--if there are still errors--the next one. At some point, you'll find that thing you did, and the rest of the errors will magically disappear.
Welcome to the wonderful world of c++ errors.
When faced with something like this start at the first error reported (Look at the output from the compiler). Then recompile. Repeat until there are no more errors.
There are a lot of different syntax errors that completely muck up the rest of the file. These include bad type in a declaration, missing semi-colon, and missing braces.
I've seen over 200 errors disappear with a one character fix.
Also, if you forget a semi-colon or brace in one header file, it might cause errors in the next included header. I've had really bad errors in windows.h, because I've included my header before it and forgotten something.
Solving tricky compiler error problems like the one you describe become much easier when you dump the (annotated) preprocessed output and look at the information that the compiler is getting. cl.exe takes the /E argument in order to dump the preprocessed output to stdout (see also this SO answer). If you use that, and look at the error in that context, it should be clear.
An error like 'Player': undeclared identifier showing up in the file where you think you're defining that very identifier is likely caused by a circular dependency. For example, does PlayerController.h contain references to the Player class without a forward declaration?
Source control is key. Based on your comment and extra code, please also post player.h contents.
In general in such a horrible case I do binary search with commenting out large portions of the project.
An error in an h file included in several cpps is usually more likely to crate lots of errors from one little mistake. A cpp parsing issue only affects the cpp. Look for an error in an h file.
Also general word of advice, try to keep all the h file references in the cpp files and limit the references made to h files from h files to an absolute minimum, this will reduce include nightmares. As you can see player.h includes a large amount of h files. Each one of them could be the culprit, I suggest commenting them one by one and wait for an error about an undefined symbol instead of the errors you see now. This will mean that you no longer have a parsing error and now have a missing definition (thus the error is in the file you commented out or in one of the files it includes...).
I'm facing an interesting problem: I had an MFC application project in Visual C++ 6.0. Since there are many changes either in MFC or in C++ standard, I wanted to port my application to Visual Studio 2010. It was fine, but I am facing a warning now, which I can't handle.
The header file has the following class definition:
template <class T>
class foo : public CObject
{
// ...
// other stuff
// ...
private:
CTypedPtrMap<CMapWordToPtr, const long, T*> oElementMap;
void some_stuff();
}
In the source file there is:
template <class T>
void foo::some_stuff()
{
// ...
// other stuff
// ...
int nIndex = 0;
// ...
// other stuff
// ...
oElementMap.RemoveKey(nIndex);
}
When I try to compile this, I get the following warnings:
Warning 1 warning C4244: 'argument' : conversion from 'const long' to
'WORD', possible loss of data c:\programme\microsoft visual studio
10.0\vc\atlmfc\include\afxtempl.h 2066
It comes definetly from the above mentioned "RemoveKey" line: if I just simply comment out that line, I won't get this warning.
I know, the main problem is, that CTypedPtrMap object uses const long as key type, but CMapWordToPtr would have WORD (unsigned short) instead of it. But the fact is: I need const long as key type, since I am processing regulary about 1 million data entries in this map, so with unsigned short the class would not be capable to do it's job furthermore.
I tried to nest either the "RemoveKey" line or the include of stdafx.h into the following expressions, but neither worked:
#pragma warning (disable: 4244)
// expression
#pragma warning (default: 4244)
Please share me any ideas about this issue, how could I resolve this warning WITHOUT changing the container's oElementMap definition and behaviour, and WITHOUT supress/disable this warning globally in the project settings as well as WITHOUT changing the afxtempl.h file supplied by VS2010.
Thanks for help:
Andrew
I've replaced it's definition to: CMap<long, long&, T*, T*&> oElementMap;. I was not sure it is the "long-counterpart" of the old map definition, therefore I did several test to compare them.
The solution was finally this.
I've written a small program that utilizes the Fast Light Toolkit and for some reason a compiler error is generated when trying to access the functions in the cmath header.
Such as error ::acos has not been declared.
This goes on for pretty much every function it tries to use in the header. What could I be missing?
The header files I have included are
Simple_window.h
Graph.h
both of which are part of the FLTK.
The code is this:
#include "Simple_window.h" // get access to our windows library
#include "Graph.h" // get access to graphics library facilities
int main()
{
using namespace Graph_lib; // our graphics facilities are in Graph_lib
Point tl(100,100); // to become top left corner of window
Simple_window win(tl,600,400,"Canvas"); // make a simple window
Polygon poly; // make a shape (a polygon)
poly.add(Point(300,200)); // add a point
poly.add(Point(350,100)); // add another point
poly.add(Point(400,200)); // add a third point
poly.set_color(Color::red); // adjust properties of poly
win.attach(poly); // connect poly to the window
win.wait_for_button(); // give control to display engine
}
Edit: Here is example code of when the compiler error is generated. This is inside the cmath header.
namespace std
{
// Forward declaration of a helper function. This really should be
// an `exported' forward declaration.
template<typename _Tp> _Tp __cmath_power(_Tp, unsigned int);
inline double
abs(double __x)
{ return __builtin_fabs(__x); }
inline float
abs(float __x)
{ return __builtin_fabsf(__x); }
inline long double
abs(long double __x)
{ return __builtin_fabsl(__x); }
using ::acos; //ERROR HERE
inline float
acos(float __x)
{ return __builtin_acosf(__x); }
inline long double
acos(long double __x)
{ return __builtin_acosl(__x); }
template<typename _Tp>
inline typename __enable_if<double, __is_integer<_Tp>::_M_type>::_M_type
acos(_Tp __x)
{
return __builtin_acos(__x);
}
Edit: Code::blocks is saving files as C files....
When you include the C++ version (<cXXXX>) of standard C libraries all the symbols are defined within the std namespace. In C++ you do not need to link against the math library (-lm is not required)
#include <cmath>
#include <iostream>
int main()
{
std::cout << std::fabs( -10.5 ) << std::endl;
}
I had this problem - it was driving me crazy but I tracked down the cause, and it was a little different than what I've seen reported on this issue.
In this case, the general cmath header (or math.h - the error and solution occur in C++ or C) had architectural environment switches to include architecture specific math subheaders. The architecture switch (environment variable) hadn't been defined, so it was punting and not actually including the headers that truly defined the math functions.
So there was indeed a single math.h or cmath.h, and it was included, but that wasn't enough to get the math functions. In my case, rather than define the architectural variable, I instead found the location of the correct sub math headers and added them to my compile path. Then the project worked!
This seems to be an issue that comes up a lot when porting Linux projects to OS-X. I'd imagine it might occur anytime a project was moved betwee platforms such that the standard library headers are arranged differently.
Jeff
Since your code as shown above does not directly call acos(), there is arguably a bug in one of the headers that you do use. It appears there is some (inline) code in one of the headers that invokes the acos() function without ensuring that the function is properly declared. This might be a macro or an inline function.
The best fix is to ensure that the headers are self-contained - change the headers.
If that is not possible, the hackaround is to include the appropriate header (#include <cmath>, probably) in the source code.
The program is able to access the cmath header, the error is in the cmath header itself.
In that case, you will probably need to provide a global acos() function (declaration at least, possibly definition too) that calls onto std::acos():
double acos(double x) { return std::acos(x); }
Just make sure this is not inside any namespace - not even the anonymous one. (Check compiled with G++ 4.0.1 on MacOS X, with '#include <cmath>' preceding it. Given that you have a problematic <cmath> header, you might need to get fancy:
extern double std::acos(double);
double acos(double x) { return std::acos(x); }
#include <cmath>
This is pretty nasty - are you sure there isn't a bug-fixed version of your compiler?
Is there any chance that you've got '#include <cmath>' inside a namespace?
It also happens in Visual C++, in programs that do not sapuse to use cmath.
I found that the problem is that I used main.c file instead of main.cpp file.
The error is most likely to be in your code and not in cmath... unless you changed something in cmath. Could you copy the errors and tell us what is the application you are using to program?