I have an IOS app that is a single view application. It has a standard C++ class that I made in it, however, it is not accepting the cpp class. Here is a simplified version of the .h file(without those lines #s):
1 #ifndef __Calculator__Numbers__
2 #define __Calculator__Numbers__
3 #include <iostream>
4 class NumDigits
5 {
6 };
7 #endif
I get the error: 'iostream' file not found
It appears as if the project doesn't have the C++ libraries? If this is it, how would I add them? If not, what should I do to fix this error?
It looks like the cpp libraries are not included in my project: http://i.imgur.com/ZkJHb7j.png
Implementation files have the extension .m in Obj-C . To use a C++ file in your Xcode project with Objective-C you must use .mm extension and you can include C++ header in the .mm file. You mustn't include the header in .m file, but if you want to include your C++ header in .h, you will need a macro like:
#ifdef _CP
#include <iostream>
#endif
To solve this, I just took out all the macros, #includes, and class declarations:
#ifndef __Calculator__Numbers__
#define __Calculator__Numbers__
#include <iostream>
class NumDigits
{
};
#endif
and left only the function declarations: void myFunction(int myVariable) ;
Then in the .cpp:
#include <iostream> //and other #includes
void myFunction(int myVariable) {
//stuff
}
This worked because the functions were still called and values were passed to and from them. The return values were the values that should've been spit out. when in the .mm if the .cpp was #included, the iostream file couldn't be found so 3include the .h,and in the .cpp, #include the .h
Related
I have the problem with precompiled header. It looks somewhat like that
ftpch.h
#pragma once
#include <vector>
#include <iostream>
#include <string>
#include <Windows.h>
ftpch.cpp
#include "ftpch.h"
Then I have a header file and cpp in my dll library.
test.h
#pragma once
// DLL_EXPORT is a macro that changes for dll and console app like that:
// __declspec(dllexport) and __declspec(dllimport)
class DLL_EXPORT Test
{
std::string foo() {return "ara ara"};
}
And this code compiles fine when I compile my dynamic library project, but fails to compile when I include "test.h" in my console app project and try to compile it. The error I get is:
C2039: 'string' is not a member of 'std'
Your header files should always be self-sufficient. Include your libraries (in this case <string>) where you need them, everywhere you need them, and only where you need them.
If your header requires a certain library to function, include it in that file; don't reply on a different header to have included that library already, because if that different file changes, you're out of luck.
You've already got #include guards through #pragma once, so adding #include <string.h> to the header files that need it won't cause collision, and will also make them more maintainable and easy to interpret.
Sorry for this question, which is probably a pretty nooby question, but i just don't know how to get my problem solved.
I have a function that returns true or false, rather the one number is a divider of an other number or not.
Now i need this function in two of my programs and instead of just copy-pasting it, i want to write this function in its own file and then include it in the programs.
What is the best way to do that?
I don't use VisualStudio, so I think I can't create a library.
You can create a header-only "library". That is, a single header file, say "my.h", which contains the definition of your function:
#ifndef MY_LIBRARY_UNIQUE_ID_OR_SOMETHING //these are called include guards,
#define MY_LIBRARY_UNIQUE_ID_OR_SOMETHING //look them up
inline bool myFunc()
{
return true;
}
#endif
Do note that the function must be inline. Otherwise you may get "function redefinition" linker errors.
In order to use this "library", all you have to do is #include "my.h" (with correct path, of course). Hope this helps
Create a file with an extension .h. For example: myheader.h
Then write to myheader.h the following:
#ifndef MYHEADER_H
#define MYHEADER_H
bool is_divider(int, int);
#endif
Write a source file named myheader.cpp
Inside the myheader.cpp write:
#include"myheader.h"
bool is_divider(int a, int b) {
/your code here
}
include the header myheader.h to the files you need to use is_divider()
Note that if you are using an IDE you should do the following:
Add the .cpp and .h files to your project
Include the path of the headers on the IDE options.
For example, if you are using codeblocks you can see the process here (keep in mind that same procedure applies in other IDE's as well)
If you are using a terminal and an editor you can create a makefile. A nice tutorial can be found here
Use a header file to share the function between both programs. The header file extension can take different forms. The most common extension is .h however there are others such as .hpp.
For example, a header file named foo.h might contain:
#pragma once // include this file just once
inline bool foo()
{
return false;
}
To use the header file you would include it from the .cpp file as follows:
#include "foo.h" // when foo.h is located in the same directory as the .cpp file
#include <foo.h> // when foo.h is located in a project additional include folder
#include "../foo.h" //when foo.h is located up one directory from the .cpp file
You need to create a header file with the function. Here are 3 options:
Option 1
Define function in header file
Here is an example:
Create a file header.h
Give it the following code:
#pragma once
inline int random()
{
return 4; //Strictly random
}
If your compiler doesn't support #pragma, use header guards, like this:
#ifndef HEADER_H_OR_ID
#define HEADER_ H_OR_ID
inline int random()
{
return 4; //Strictly random
}
#endif
For this option, every file that wants to use the function has to include "header.h"
Option 2
Forward declare the function in a header file, and implement in every source file
Example:
#ifndef HEADER_H_OR_ID
#define HEADER_H_OR_ID
int random();
#endif
Source.cpp
#include "header.h"
int random() {return 4;}
But unfortunately, this is not probably not what you want, although it allows uniqueness per file.
Option 3
Forward declare the function in a header file, and implement in corresponding source file
Example:
Header.h
#ifndef HEADER_H
#define HEADER_H
int random();
#endif
Header.cpp
#include "Header.h"
int random() {return 4;}
Here, you have to include the source file with the main file, such as by linking them on the command line like this:
g++ main.cpp header.cpp
Tools like Visual Studio already would link all files, so nothing manual, other than creating files, is necessary.
So I have 3 classes Vehicle, Linked_List, and TrafficSim
Each class has both a .cpp and .h file
TrafficSim.h has:
#include "Linked_List.cpp"
#include "Vehicle.cpp"
Linked_List.h has:
#include "Vehicle.cpp"
and all fo the cpp files have:
#include "File.h"
All of my H files have guard that goes:
#ifndef FILENAME_H
#define FILENAME_H
/* code for class function declarations */
#endif
For some reason, after compiling Im getting an error sayng I am redefining Vehicle, so Im guessing the guard I was taught to set up, does not work. Can anyone help me?
Generally, it is the .C or .CPP files that implement classes which #include the .H header files that declare the classes, and not the other way around, as you appear to be trying to do.
TestViewController.h/TestViewController.mm
HelloWorld.h/HelloWorld.cpp
If I include "HelloWorld.h" into TestViewController.mm it compiles well. When I include "HelloWorld.h" into TestViewController.h it prompts an error: 'iostream' file not found.
My HelloWorld.h code is a simple standard cpp file.
#ifndef __MixedCppTest__HelloWorld__
#define __MixedCppTest__HelloWorld__
#include <iostream>
#include <vector>
class HelloWorld {
public:
HelloWorld();
~HelloWorld();
};
#endif /* defined(__MixedCppTest__HelloWorld__) */
you can use macros
#ifdef __cplus
// TODO Code
#endif
Reference: Link
When I include "HelloWorld.h" into TestViewController.h it prompt
error: 'iostream' file not found….
Is there any Objective-C files which imports TestViewController.h?
At the stage of preprocessing header file becomes a part(along with the source file) of a translation unit, and if you're asking to include such header (with c++ libraries inclusion and c++ class definition) to Objective-C source file - then no, it's impossible. If you searching a way to compile and link .cpp and .m source files to one binary, you could make .mm wrapper unit to wire them together.
Thanks https://stackoverflow.com/users/635608/mat 's answer.
It has only one standard solution, that's make compiler knows about specified sources should be compiled as objective-c++.
There are only 3 ways:
change .m to .mm
change Type to Objective-c++ in Utilities panel in xcode editor.
change "Compile sources as" to objective-c++
If you import cpp file indirectly, you should change all files to objective-c++ compiled mode on the import chain.
I have a very basic project in VS2012 using precompiled headers. I know that I'm supposed to add all "common" includes to stdafx.h and that I need to include this in each .cpp file. Thus, the basic setup looks like this:
Foo.h:
#ifndef FOO_H
#define FOO_H
class Foo {
public:
Foo();
~Foo();
void do(string str);
};
#endif
Foo.c:
#include "stdafx.h"
#include "Foo.h"
void Foo::do(string str) {}
in stdafx.h:
#include <string>
using namespace std;
Without precompiled headers, I'd put #include <string> into foo.h, since it has to know about the declaration of string. But how this foo.h know about string in this setup? (Note that stdafx.h is only included in the .cpp files).
Note: I have a working example that uses precompiled headers; the question is about how this works.
This is because the compiler processes headers in the order in which they appear in the main compilation unit.
Because the .cpp file included <string> (indirectly via "stdafx.h"), the contents of <string> are known to the compiler, and can be used by code that follows, even code pulled in from header files.
It is fragile though, because including your header file without first including <string> will cause errors.
You can look on the pre-compiled headers as a kind of cache for header files. The compiler analyzes a set of headers when it first encounters it (usually when compiling stdafx.cpp), compiles them, and then has the results ready for any module that needs them.