I have an extern code written in C in my C++ project in MS VS 2010. It comes with .h and .c files and I use include like this:
extern "C"{
#include "Extern\libname.h"
}
It worked nice. Then I read Google C++ style guide and removed all entries of
using namespace std;
replaced them with just
std::whatINeed
everywhere.
And the error showed up -
error C2065: 'FILE' : undeclared identifier
in the header of extern library. And then I move cursor to the word "FILE" in that code, there is
tydef _iobuf FILE
What should I add into my code to fix it? I don't want to change extern code because it can be updated and I will be forced to add my fixes on every update. Also, I am sure that there is a simple bug caused by me.
add
#include <cstdio>
above you include.
Related
I have recently attempted to switch my C++ project's target from an executable to a dynamic library (.dylib), and as soon as I rebuilt with the new target, I get a few errors saying that some defines and declarations in math.h are undeclared, such as M_PI and the sqrt() function.
The error message is as follows: error: use of undeclared identifier 'M_PI', and error: use of undeclared identifier 'sqrt'
This only occurs when I am building my project as a library, and I cannot figure out why it is doing this.
If anyone can help me out on this, it would be much appreciated!
Edit:
Also, if I try to change my include to #include <cmath>, I get more errors, such as: No member named 'signbit' in the global namespace.
Make sure #include <cmath> is at the top of the code, above int main(). I had the same problem. moving the #include <cmath> below <iostream> and above int main() solved the problem for me.
I am new to working in Visual Studio (am using version 2005). I am running into a problem with namespaces that I am not able to figure out.
I am trying to create a static library which I will link to an Application later.
So, I have a XXX.h file with the following code
#ifndef _XXX_X_H
#define _XXX_X_H
namespace LLL_NWK
{
void lllInit();
}
#endif
I include XXX.h in XXX.c and the code looks like
#include "XXX.h"
using namespace LLL_NWK;
void lllInit()
{
}
However, when I build the Library I encounter the following errors
error C2061: syntax error : identifier 'LLL_NWK'
error C2059: syntax error : ';'
error C2449: found '{' at file scope (missing function header?)
error C2059: syntax error : '}'
I am unable to figure out the cause of this error. Would appreciate some help and pointers.
First, using namespace LLL_NWK is not appropriate here. You are declaring and defining a function void lllInit() outside of namespace LLL_NWK. You need to place the definition inside of the namespace, which can be done like this:
void LLL_NWK::lllInit()
{
}
or like this:
namespace LLL_NWK
{
void lllInit()
{
}
}
Second, make sure you compile the code as C++.
That code is not supported by the C compiler - makes sure to rename the filename to .cpp instead .c. In this case, namespace is not supported. See this post: Namespaces in C
So, I have this question. Why does cout throws
error C2065: 'cout' : undeclared identifier
I am using Visual Studio 2012 as an IDE and I am writing a school project. I have everything done except an example file. So I am trying to write something on the screen like this:
#include "iostream"
#include "stdafx.h"
using namespace std;
int main()
{
cout<<"example";
return 0;
}
So the problem is with cout... printf works fine, but I want to use cout.
EDIT:
I've changed "" to <> but it is not helping. Also I am using this code only for example... This is not the whole project.
stdafx.h shall be the first include directive in your source file.
Switch files and convert the second include to <>, as other suggested.
#include "stdafx.h"
#include <iostream>
See this post for more information.
First of all:
#include <iostream>
instead of #include "iostream"
Secondly, it is generally considered bad practice to write using namespace std;, even though most courses start with that. It is better to only use what you actually need, in your case:
using std::cout;
#include "iostream"
should be
#include <iostream>
Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include
By courtesy of #Jerry Coffin's answer:
When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.
When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).
EDIT:
However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.
If you use #include <iostream> with the <> instead of "" then it should work. Right now, the compiler doesn't know where to find the iostream library.
Also, you might want to change cout<<"example"; to cout<<"example"<<endl; for a new line so that it formats correctly.
Came across this issue while trying to build a Dynamic Linked Library. Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:
#include "pch.h"
This should also be the case for VS2017 or earlier.
This error also occurred in the Visual Studio 2017 IDE. Moving stdafx.h to the top solved the error.
For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?
C++ newbie here. I'm trying to put some WIA functions in a DLL. I keep getting and undeclared identifier on the IWiaDevMgr variable. When creating the project I chose the Win32 Console Application and DLL application type. Not sure if it matters but I put the wiaguid.lib in the project
properties -> Linker -> input -> additional dependencies.
What is wrong with this code?
MyDLL.h
#include <wia.h>
namespace MyDLL
{
class MyFirstFuncs
{
public:
static __declspec(dllexport) int doWork();
};
}
MyDLL.cpp
#include "MyDLL.h"
namespace MyDLL
{
int MyFirstFuncs::doWork()
{
IWiaDevMgr *pIWiaDevMgr;
}
}
I had the exact same problem. Through trial and error I found that
#include <windows.h>
#include <wia.h>
fixed the problem.
I'm a C++ newbie also so couldn't tell you the exact reason why this works. Probably WIA is dependant on some definitions/macros/whatever in WINDOWS.H
Check the order in which you have included your header files. It may be the same problem like the one I had in programming a Directshow application. I had included vmr9.h before d3d9.h. During the build process, the compiler fired errors concerning d3d9 objects included in the vmr9.h. I had to reorder the inclusions to solve the problem
This is pasted from a website, which presumably was working. I did some googling and found that the issue I have now is a result of Visual C++ 2010 SP1, which I downloaded today, and is now giving me this error:
PCH Warning: header stop cannot be in a macro or #if block.
Hopefully someone will be able to help me with this!
#ifndef APP_STATE_H
#define APP_STATE_H
#include "Framework.h"
class AppState; //this line is giving me the error
//define two classes
#endif
Framework.h:
#ifndef OGRE_FRAMEWORK_H
#define OGRE_FRAMEWORK_H
#include <OgreCamera.h>
#include <OgreEntity.h>
#include <OgreLogManager.h>
#include <OgreOverlay.h>
#include <OgreOverlayElement.h>
#include <OgreOverlayManager.h>
#include <OgreRoot.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreConfigFile.h>
#include <OISEvents.h>
#include <OISInputManager.h>
#include <OISKeyboard.h>
#include <OISMouse.h>
class OgreFramework : public Ogre::Singleton<OgreFramework>,OIS::KeyListener,OIS::MouseListener{
public:
OgreFramework();
~OgreFramework();
bool initOgre(Ogre::String wndTitle, OIS::KeyListener *pKeyListener = 0, OIS::MouseListener *pMouseListener = 0);
void updateOgre(double timeSinceLastFrame);
//OIS
bool keyPressed(const OIS::KeyEvent &keyEventRef);
bool keyReleased(const OIS::KeyEvent &keyEventRef);
bool mouseMoved(const OIS::MouseEvent &evt);
bool mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id);
bool mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id);
Ogre::Root* mRoot;
Ogre::RenderWindow* mRenderWnd;
Ogre::Viewport* mViewport;
Ogre::Log* mLog;
Ogre::Timer* mTimer;
//OIS
OIS::InputManager* mInputMgr;
OIS::Keyboard* mKeyboard;
OIS::Mouse* mMouse;
private:
OgreFramework(const OgreFramework&);
OgreFramework& operator= (const OgreFramework&);
};
#endif
I had the same issue and was looking for a solution. Following worked for me:
Add #pragma once at the start of the file (even before the #ifndef APP_STATE_H header guard)
You probably used a project template to get started and threw away the pre-generated source code files. Those project templates like to turn on precompiled headers because it is such a time-saver. Right-click your project in the Solution Explorer window, Properties, C/C++, Precompiled Headers. Change the "Precompiled Header" setting to "Not Using".
1.Close the Project.
2.Reopen the project,and all ok.
this is my expeirence.
move the #include statements outside the #if #end block
Rebuilding IntelliSense database solves the problem.
Close Visual Studio
Delete [SolutionName].sdf
Delete DllWrappers.opensdf
Delete ipch folder
Open Visual Studio
I had the same problem. My solution was to add a missing ';' at the end of a class definition. Although this does not seem to apply to your problem, others who come here with the same error might find this helpful.
This is probable a day late and a dollar short but I had the same error when I accidentally put my header file in a .cpp file instead of a .h file. I will post it though in case it can help someone.
I just added a referenct to the header file (#include "header.h") and it helped.
I found that my .h file was actually being treated as a .cpp file! Right click on the file in the Solution Explorer > All Configurations > Item Type: C/C++ header
Make sure the item type is not C/C++ compiler or other.
Once you add a .cpp file and have it include the header this error should go away. I've read some where else this is a bug.
I use Visual Studio to edit Linux projects. For me, the issue was present when I include string.h in my precompiled header file. It was caused by lines that have an __asm statement, for example:
__THROW __asm ("memchr") __attribute_pure__ __nonnull ((1));
The solution was to define the following macro under Project Properties, Configuration Properties, C/C++, Preprocessor, Preprocessor Defines:
__asm(x)=
In my case I was wrapping the whole .cpp file inside of #ifdef directive and getting this error.
The issue is, when using precompiled header, your standard precompiled header include needs to be outside of that!
For example:
#include "pch.h" // <- outside of any #if pragma directive
#ifdef EXAMPLE_DIRECTIVE
#include "another_header.h" // <- any other headers can be included inside
class AppState {}; // etc, your code here
#endif
Note that this issue can likely be also caused by some header you're including, which includes PCH header itself. If that's the case, just copy the import of PCH header from that file to the top of the file you're getting the error in, above the #if block.