Error in creating a class header file - c++

I am attempting to create a simple class header file. I have done the following so far:
#ifndef RECORD_H
#define RECORD_H
class Records{
int idNumber;
int serialNumber;
public:
Records();
};
#endif
However, I am getting the following error:
[Error] unknown type name 'class'
What am I doing wrong?

You're probably compiling it as C rather than C++. That is why you are getting that error..
Make sure your source file has a .cpp extension.

As lakesh said, you may to compiling as C rather than C++. Most compiler use the file extension to determine how to compile the file (use .cpp instead of .c)
If that's not it, then the problem would probably be in the a different file loaaded before this.
// Record.cpp
#include "badfile.h" // error in here
#include "record.h" // error showing up here.

Related

Why Xcode gives error 'string' file not found?

I am trying to interop with basic C++ method in Swift. But Xcode gives me an error when I build the project;
'string' file not found
I have researched this issue on StackOverflow but every solution and answers didn't solve my problem. And I started to think that what if I use libstdc++ rather than libc++. Then I found that libstdc++ was removed with Xcode 10. I found the files on Github and apply it to appropriate file directories. And that did not do much either. Did you guys encounter with this kind of problem before?
This is my header file.
#ifndef CPPTest_hpp
#define CPPTest_hpp
#include <string>
std::string saySomething();
#ifdef __cplusplus
extern "C" {
#endif
int returnNumber();
#ifdef __cplusplus
}
#endif
#endif CPPTest_hpp
This is the cpp file.
#include "CPPTest.hpp"
std::string saySomething()
{
return "Welcome to C++";
}
int returnNumber()
{
return 10;
}
This is where I expose header to Swift
#include "CPPTest.hpp"
After all, When I build the project. It gives me an error like;
'string' file not found
Why does this error occur?
By the way, it works when I use integer as return type and its appropriate implementation in cpp file.

error: no type named 'InterfaceProvider' in namespace 'content::shell'; did you mean '::shell::InterfaceProvider'?

I am getting the following compile time error when i am including the render_tree_node.h
in my example.cc
included from ../../content/shell/browser/my_example.cc:17:
In file included from ../../content/browser/frame_host/frame_tree_node.h:17:
In file included from
../../content/browser/renderer_host/render_process_host_impl.h:165:3: error: no type named 'InterfaceProvider' in namespace 'content::shell'; did you mean '::shell::InterfaceProvider' ?
when I compile this withuot adding my_example, it does compile the code.
I have tried adding the Dependency in my BUIDL.gn of my_example.
can anyone help ?
render_process_host_impl.h looks like this :->
namespace shell {
class InterfaceRegistry;
}
namespace content {
virtual shell::InterfaceRegistry* GetInterfaceRegistry() = 0;
}
mmExample.cc
#include "/render_tree_node.h"
my Question is when i compile myExample.cc the error mentioned above appears else it does not, there are other files are also there which are using this file and this file is compiling i have rechecked even without adding my files also.
what could be the problem ??

header file include cpp file in visual studio gives compile error

I have my header like:
#include iostream & map
namespace dummy {
const char* some_xxx_name(const char* name);
}
#include myfile.cpp (the following cpp file stated below)
I have my cpp file like:
#include "myfile.h" (the file stated above)
#include <sstream>
#include <cassert>
inline const char* dummy::some_xxx_name(const char* name) {
........
}
They are working fine under my linux environment with makefile, now I am adopting in visual studio environment and get following error:
error C2084: function 'const char *dummy::some_xxx_name(const char *)' already has a body ....(point to the line in the header)
I know the file structure is not good at all for including cpp at header and including header at cpp(or maybe).
want to ask if there is any chance to make it work without updating the code? Not familiary how shall I go forward under windows visual studio environment.

File Handling C++ with xcode. simple ofstream out not working

I wanted to do simple file handling test program in Xcode. But it gives me an error.
#include <iostream>
#include <fstream.h>
using namespace std;
int main() {
ofstream out("test");
out<<"pen"<<"\t";
out<<"25";
}
Error:
Implicit instantiation of undefined template std::basic_ofstream<char>
The first error means that you have to write fstream instead of fstream.h . The second error would be caused by the first one. Look at my g++'s output when I try to compile your example:
example.cpp:2:21: fatal error: fstream.h: No such file or directory
compilation terminated.
In this link, you will find ofstream's class specification. I think it can be helpful for you:
http://www.cplusplus.com/reference/fstream/ofstream/

PCH Warning: header stop cannot be in a macro or #if block - Visual C++ 2010 Express SP1

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.