declared name in.h but reports undeclare in .cpp in Xcode - c++

Below is my code:
MathCore.h
#ifndef __CC_MATHCORE_H__
#define __CC_MATHCORE_H__
#include "math.h"
class MathCore
{
public:
MathCore();
virtual ~MathCore( );
int x (int n );
};
#endif
MathCore.cpp
#ifndef __CC_MATHCORE_H__
#define __CC_MATHCORE_H__
#include "MathCore.h"
MathCore::MathCore()//a
{
}
MathCore::~ MathCore()//b
{
}
int MathCore::x (int n )//c
{
float v=0;
return v;
}
#endif
but it erports error at
a:C++ requires a type specifier for all declarations
Use of undeclared identifier 'MathCore'
b:Expected a class or namespace
c:Expected a class or namespace
Your comment welcome

You shouldn't have that #define in both the .cpp and .h files, as it will prevent the contents of the .h file from actually being included.
When you #include a file, for all practical purposes it behaves the same way as copying and pasting that file into wherever you have the #include. So, the first few lines of your MathCore.cpp are effectively this:
#ifndef __CC_MATHCORE_H__
#define __CC_MATHCORE_H__
#ifndef __CC_MATHCORE_H__
#define __CC_MATHCORE_H__
/* the rest of your .h file here */
When restructured that way, it becomes a little more obvious, that second #ifndef can never match since the symbol it's checking is defined immediately above.

Because, in your C++ file, you're using the same header guards as your header. The second line defines __CC_MATHCORE_H__. After this, you include the header, which does nothing if __CC_MATHCORE_H__ is defined. Remove the guard from the cpp file, and you'll be fine. Guards are rarely, if ever, needed in actual implementation files.

//////////////////////////////////MathCore.cpp
#include "MathCore.h"
MathCore::MathCore()//a
{
}
MathCore::~ MathCore()//b
{
}
int MathCore::x (int n )//c
{
float v=0;
return v;
}

Remove the include guard from MathCore.cpp

Related

Multiple definitions when using #ifdef

I am having a problem when compiling: Multiple definitions of "myFunction()"
I will greatly simplify the problem here. Basically, I have 3 files: "main", "header", and "myLibrary".
main.cpp
#include "header.hpp"
int main() { }
header.hpp
#ifndef HEADER_HPP
#define HEADER_HPP
#include "myLibrary.hpp"
// ...
#endif
header.cpp
#include "header.hpp"
// ...
myLibrary.hpp
#ifndef LIB_HPP
#define LIB_HPP
#if defined(__unix__)
#include <dlfcn.h>
std::string myFunction() { return std::string(); }
#endif
#endif
myLibrary.cpp
#include "myLibrary.hpp"
//...
So, why does the compiler say that I have Multiple definitions of "myFunction()"?
One clue I found: When I take header.cpp and erase the line that says #include "header.hpp", the program compiles without complaining. On the other hand, if I erase myFunction (from myLibrary.hpp) instead, the program also compiles without complains
You are defining the body of the function inside the header file. So every translation unit that you include that header in (in this case, main.cpp and header.cpp) will end up with its own copy of that function body. And when you try to link those multiple units together, you get the "duplicate definition" error.
The function needs to be declared in the hpp file, and defined in the cpp file:
myLibrary.hpp
#ifndef LIB_HPP
#define LIB_HPP
#if defined(__unix__)
#include <dlfcn.h>
#include <string>
std::string myFunction();
#endif
#endif
myLibrary.cpp
#include "myLibrary.hpp"
#if defined(__unix__)
std::string myFunction()
{
return std::string();
}
#endif
//...
Include guards only prevent the same header from being included twice within the same translation unit, which in practice is usually a single .cpp file. For example, it prevents errors when doing this:
#include "header.h"
#include "header.h"
int main()
{
}
But, more generally, it means that it doesn't matter if you've include a header that has already been included as a dependency of another header.
However, if you have two .cpp files include the same header, and that header contains the definition of a function (such as your myLibrary.hpp) then each .cpp file will have its own definition (the include guard won't help because the header is being included in two separate translation units / .cpp files).
The simplest thing to do is to declare the function in the header, which tells every file that includes your header that the function exists somewhere, and then define it in the .cpp file so that it is only defined once.
You should to define functions in the .cpp files, not in the header files. You declare them in the header files. What you're doing is defining it in the header file, so when it gets included into multiple files, the function is getting duplicated. Cross-file duplicate symbols will throw an error, unless they're static.
myLibrary.cpp:
#include "myLibrary.hpp"
#ifdef(__unix__)
std::string myFunction() { return std::string(); }
#endif
myLibrary.hpp:
#ifndef LIB_HPP
#define LIB_HPP
#if defined(__unix__)
#include <dlfcn.h>
#include <string>
std::string myFunction();
#endif
#endif

If you include something in the .h file Do you have to include the same thing again?

So I am just wondering if you #include something in a for example header.h file:
For example this is called header.h:
#include <vector>
#include <iostream>
#include <somethingElse>
So if for example I make a file called something.cpp Do I need to put all those include statements again?
#include "header.h"
// If I include #header.h in this file. Do the #include carry over to this file. Or do they not
I am wondering because whenever I include <vector> something in my .h file the #include statements that I used previously in the .h file always turn grey which means they are not used. Is it because I used it in the .h file? Its not a problem or anything I am just curious.
You don't need to include those headers again because your compiler can find those headers. You can also try to read and understand the makefile (or CMakeList) which will help.
Try always to avoid "Multiple file inclusion" via using inclusion guard or #pragma once in order to prevent the multiple file inclusion.
To include file means that the content of the file will be added to the very place you wrote include.
Here's an example:
// header.h
const int vlaue = 10;
const int value2 = 0;
// main.cpp
#include "header.h"
#include "header.h"
Above the content of "header.h" is added twice to main.cpp.
Do you know what is the result? It's a compile-time error complaining of redefinition of value and value2.
In the above example I think green programmers don't get trapped by it but it is just an explanation, So what I talk about is when a huge program where many header files and many source files and some files include others then it'll be so difficult to track the right file inclusion.
The workaround that is to use inclusion guards or pragma once eg:
Let's modify our header.h to look like:
// header.h
#ifndef MY_HEADER_H
#define MY_HEADER_H
const int vlaue = 10;
const int value2 = 0;
#endif
Now in main.cpp:
#include "header.h"
#include "header.h"
#include "header.h"
The code above works fine and no duplicate of header content is added to main.cpp. Do you know why? It's the magic of Macro there. So at first time the pre-processor checks whether a macro has been already defined with the name MY_HEADER_H or not and for sure for the first time it is not defined so the content is added. The second and so on the condition fails because the macro is already defined thus the content of header.h will not be added to where it is called.
The draw back of inclusion guard is if you have a macro with same name as the inclusion guard thus it is already defined so the content will never be added (empty content). Thus you get a compile-time error:
value, `value2` undeclared identifiers.
The second solution is using pragma eg:
Let's modify our header.h file:
// header.h
#pragma once
const int vlaue = 10;
const int value2 = 0;
// main.cpp
#include "header.h"
#include "header.h"
The code above works correctly so no multiple inclusion of header.h That is because of the magic of pragma once: which is a non-standard but widely supported pre-processor directive designed to cause the current source file to be included only once in a single compilation. Thus, #pragma once serves the same purpose as include guards, but with several advantages, including: less code, avoidance of name clashes, and sometimes improvement in compilation speed.
Finally you should include header wherever their content is used eg:
// Shape.h
class Shape{
// some code here
};
// Cube.h
#include "Shape.h"
class Cube : public Shape{
// some code here
};
// Cuboid.h
// #include "Shape.h"
#include "Cube.h" // So here the Shape.h is added to Cube.h and Cube.h is added here.
class Cuboid : public Cube{
// some code here
};
As you can see above the content of Shape.h is added to Cuboid.h indirectly because it is added to Cube.h and cuboid.h includes Cube.h so it is added to it. So without inclusion guards or pragma once if you include the two headers in one source file you get duplicate content there.

Multpile definition error of a header declared variable when there's just one definition/declaration [duplicate]

This question already has answers here:
How do I use extern to share variables between source files?
(19 answers)
Closed 5 years ago.
I have a header file where I've defined a structure and its variable 'editor' and am using it in multiple files. I have not declared it anywhere else, yet it gives me multiple declaration error. I've also tried using #ifndef but to no avail.
This is my header file:
editorlib.h
#ifndef ED_LIB
#define ED_LIB
//#include files
struct terminalProperties {
int rows,cols;
struct termios origTerm;
int mode;
}editor;
int TERMINATE=0;
//function definitions
#endif
My editorlib.cpp
#ifndef EDITORLIBRARY_H
#define EDITORLIBRARY_H
#include "editorLibrary.h"
getAttr() {
tcgetattr(STDIN_FILENO, TCSAFLUSH, &editor.origterm);
}
//further code
#endif
Driver file vi_editor.cpp:
#ifndef MAIN_H
#define MAIN_H
#include "editorLibrary.h"
int main(){
//some code
while(1){
//some code
if(TERMINATE)
break;
}
//some code
return 0;
}
#endif
My normal.cpp:
#ifndef NORMALMODE_H
#define NORMALMODE_H
#include "editorLibrary.h"
void normalMode() {
editor.mode = 1;
//further code
}
#endif
My command.cpp:
#ifndef COMMANDMODE_H
#define COMMANDMODE_H
#include "editorLibrary.h"
void commandMode() {
editor.mode = 2;
//further code
}
#endif
Similarly I have a few other files where I'm not declaring the editor variable but just using it in a similar manner as above. I can't seem to find why it tell me about multiple declaration.
Multpile definition of a header declared variable
This is already a violation of the One Definition Rule. The simple answer is "don't". You can only declare in a header, via extern, and define in a single .cpp file.

Conflicting declarations of enum

I have a header file called custom_types.h that was working fine until now. I have a few enums declared in it and there is no implementation file with the same name.
These are the two declarations in the file:
enum playback_type {
NOTE_PB,
SONG_PB
};
enum note_name {
C_REG = 1,
C_SHARP = 2,
D_REG = 3
};
Now for some reason I'm getting conflicting declaration errors (full size here):
You guys have any idea why this is happening? I don't understand how a single definition can be conflicting.
Use guards:
//custom_types.h
#ifndef custom_types_h //this is called guard!
#define custom_types_h
enum playback_type {
NOTE_PB,
SONG_PB
};
enum note_name {
C_REG = 1,
C_SHARP = 2,
D_REG = 3
};
#endif //dont forget this line!
Such guards ensure that the content of the header file will be included once in one translation unit (TU).
If your compiler supports (modern compilers support this), you could use #pragma once as:
//custom_types.h
#pragma once //it ensures that the content of this header will be
//included once in one translation unit
enum playback_type {
NOTE_PB,
SONG_PB
};
enum note_name {
C_REG = 1,
C_SHARP = 2,
D_REG = 3
};
Since headers are full of declarations, you must make sure the compiler doesn't read them twice. Once solution would be to make sure each header is included (directly or through another header) only once. This is not so easy.
The common solution is to add guards:
#ifndef SOME_CONSTANT_THAT_WONT_GET_MISTAKEN
#define SOME_CONSTANT_THAT_WONT_GET_MISTAKEN
... header contents ...
#endif
If this header gets included multiple times, the compiler will see both copies, but will discard the second one, since that SOME_CONSTANT... is already defined. That is, this:
#include "your_file.h"
...
#include "your_file.h"
will become:
#ifndef SOME_CONSTANT_THAT_WONT_GET_MISTAKEN // not defined
#define SOME_CONSTANT_THAT_WONT_GET_MISTAKEN // define it
... header contents ... // read these
#endif
...
#ifndef SOME_CONSTANT_THAT_WONT_GET_MISTAKEN // already defined
#define SOME_CONSTANT_THAT_WONT_GET_MISTAKEN // skip
... header contents ... // skip
#endif
most likely you are including the file more than once, that is the same #include in several other .h or .c/.cpp
The easiest way to solve this is to have at the beginning of every .h file a #ifndef that avoids redefinition when the file has already been included through another inclusion path.

C++ namespaces trobles

//portl.cpp
namespace FAWN {
namespace Sys{
class PortListner {
....
Connecter::ConPtr _cur_con; - the main problem is here
...
//con.cpp
namespace FAWN {
namespace Sys {
class Connecter {
.....
public:
typedef boost::shared_ptr<Connecter> ConPtr;
...
Moreover, portl.cpp file is included into some other "main" sourse file. And this "other-main" file includes con.cpp too. So if I include con.cpp to portl.cpp - I define Connecter twice (in portl and in main). If I do not include it, compilator doesn't know what Connecter::ConPtr (or FAWN::sys::Connecter::ConPtr) means and try to use it as defenition of method.
Put the class Connecter (which you should probably rename to Connector) into a header file (.h instead of .cpp) and add include guards into the file. That is, at the beginning of your con.h file, add lines
#ifndef CON_H_INCLUDED
#define CON_H_INCLUDED
and at the very end, add the line
#endif
This way, even if you #include con.h twice, the second time it will not get read because the symbol CON_H_INCLUDED has been defined on the first time so the #ifndef-#endif pair hides the content.
This is the common way in C++: put class declarations in .h files that get #included in .cpp files that then actually define the functions.
Here is how it should look:
#ifndef PORTAL_H
#define PORTAL_H
#include "con.h"
//portl.h
namespace FAWN {
namespace Sys{
class PortListner {
....
//might need to specify Connector's namespace fully here
FAWN::Sys::Connecter::ConPtr _cur_con;
...
};
}
#endif //PORTAL_H
//con.h
#ifndef CON_H
#define CON_H
namespace FAWN {
namespace Sys {
class Connecter {
.....
public:
typedef boost::shared_ptr<Connecter> ConPtr;
};
}
#endif //CON_H