I have a problem with the inclusion of the vector module. It seems to there is a conflit with others modules. Here is the structure :
In the simulation.h :
#pragma once
#ifndef SIMULATION
#define SIMULATION
#include <ostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <cstdlib>
// #include <vector>
#include "File.h"
...
void afficherResultat(std::vector<Client> sortie);
...
#endif
And the File.h file :
#pragma once
#ifndef FILE
#define FILE
#include <vector>
class File {
...
std::vector<Client> l;
...
};
#endif
And I get 108 errors starting with : C4091 warning and C4430, C2065, C4229 errors...
Some people spotlight the order of the inclusions. Any Ideas ?
You are defining a macro for an identifier which is part of the standard library:
#define FILE
(see https://en.cppreference.com/w/cpp/io/c#Types for what FILE is).
Doing so is forbidden and will cause very weird errors.
Instead use names which are as unique as possible as include guards, e.g. INCLUDE_GUARD_FILE_H.
If you have an include guard there is also no need for #pragma once which is a non-standard way of solving the double inclusion problem that the include guard is also supposed to prevent.
Additionally you have not declared Client in File.h. Probably some #include for the header file defining Client is missing.
Related
I have a project consisting of 6 files; main.cpp, functions.h, tennisplayer.h, tennisplayer.cpp, tennisteam.h & tennisteam.cpp which are roughly defined as follows:
// main.cpp
#include "tennisteam.h"
#include <iostream>
#include <string>
#include <exception>
// some main() code that needs functions.h definitions
// functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include "tennisplayer.h"
#include <iostream>
#include <string>
#include <limits>
// some constants & function definitions needed by both main.cpp & tennisteam.cpp
#endif
// tennisplayer.h
#ifndef TENNISPLAYER_H
#define TENNISPLAYER_H
#include <string>
#include <vector>
// tennisplayer class declarations
#endif
// tennisplayer.cpp
#include "tennisplayer.h"
#include <iostream>
#include <fstream>
// tennisplayer class definitions
// tennisteam.h
#ifndef TENNISTEAM_H
#define TENNISTEAM_H
#include "tennisplayer.h"
#include <string>
#include <vector>
//
#endif
// tennisteam.cpp
#include "tennisteam.h"
#include <iostream>
#include <fstream>
// tennisteam class definitions
However, when I include functions.h into both main.cpp & tennisteam.cpp via tennisteam.h I get a linker error along the lines of:
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccv30cX0.o:tennisteam.cpp:(.text+0x0): multiple definition of `function(std::string const&)'; /tmp/ccRThgpp.o:main.cpp:(.text+0x0): first defined here
I'm aware this is a linker error. I've looked around for a fix but all I come across are posts instructing me to use include guards which I have done already. Is there something I'm missing here? Any help would be appreciated.
You have function function(std::string const&) that you not only declared but also defined in your header file. If you need to have it defined there instead of a .cpp file, mark it as inline.
This results in two cpp files (namely main.cpp and tennisteam.cpp) ending up with a definition of that function, because they both include that header file.
I am trying to understand pre-compiled headers. So I set up the following sample project:
pch.hpp
#include <vector>
pch.cpp
#pragma once
#include "pch.hpp"
Vector.hpp
#pragma once
#include "pch.h"
class Vector {
public:
Vector(const size_t N, const int init);
private:
std::vector<int> m_data;
};
Vector.cpp
#pragma once
#include "pch.hpp"
#include "Vector.hpp"
Vector::Vector(const size_t N, const int init)
: m_data(N, init) { }
Source.cpp
#include "pch.hpp"
#include "Vector.hpp"
int main() {
const Vector v1(3, 5);
}
I did this in VS 2022 by marking pch.hpp as the pre-compiled header file under Project Properties --> C++ --> Pre-Compiled Headers --> Header File with Use (/Yu). And setting pch.cpp as Create (/Yc).
Now, when I remove #include "pch.hpp" in Vector.cpp or Source.cpp, I get this error:
File Vector.cpp
Line 10
Severity Error
Code C1010
Description unexpected end of file while looking for precompiled header.
Did you forget to add '#include "pch.hpp"' to your source?
However, if I disable pre-compiled headers, everything compiles fine. As one would expect.
So, it seems that with pre-compiled headers enabled, I now need to put #include "pch.hpp" into every other source file? Even though #include "Vector.hpp" already includes pch.hpp? Is it possible to avoid having to put #include "pch.hpp" into all my other source files?
I ask, because I have a much larger code base (than the example above), and I thought I could essentially replace all instances of #include <vector> with #include "pch.hpp" and be done. But having to add #include "pch.hpp" to numerous cpp files, requires me to touch a lot of files (and seems counter-intuitive).
Apologies for the possibly naive question, as I am new to pre-compiled headers.
I'm trying to get my includes working, but everything I try leads to errors. Even using #pragma once doesn't work. Do you know what I made wrong?
main.cpp
#include "utility/headers/Window.h"
#include "engine/headers/Player.h"
#include "engine/headers/Chunk.h"
ChunkManager.h
#ifndef CHUNK_MANAGER_H
#define CHUNK_MANAGER_H
#include "../../utility/headers/Vector3i.h"
#include "Chunk.h"
#include <map>
class ChunkManager{...}
#endif // CHUNK_MANAGER_H
Chunk.h
#pragma once
#ifndef CHUNK_H
#define CHUNK_H
#include <glm/glm.hpp>
#include "CubeCreator.h"
#include "ChunkManager.h"
#include "../../utility/headers/Random.h"
#include "../../utility/noise/headers/Noise.h"
class Chunk{...}
#endif // CHUNK_H
Error message is 'ChunkManager' has not been declared.
Thanks in advance!
Replace #include "ChunkManager.h" with class ChunkManager;.
That's called forward declaration and solves problems like class A needs to know about class B and class B needs to know about class A.
Depending on how you use ChunkManager in class Chunk. A forward declaration might not work.
Since non of the mentioned techniques worked in my case I defined a new header file containing global variables like chunkSize. Maybe it's just impossible to do what I've tried.
However for those who might find this question here's how my imports look now:
ChunkManager.h
#include "Chunk.h"
Chunk.h
// no includes
main.cpp
#include "ChunkManager.h"
And access to chunkSize isn't done anymore by calling ChunkManager::chunkSize but instead by calling Settings::chunkSize
I'm having a bit of a problem on the following files:
i have the arcball struct on this file:
#ifndef ARCBALL_H_
#define ARCBALL_H_
#include "ex1.h"
...
extern struct arcball{
float radius;
int x,y;
}arcball;
...
#endif /* ARCBALL_H_ */
and I have the following ex1.h file which includes the arcball.h file:
#ifndef __EX1_H__
#define __EX1_H__
////////////////////////////
// Project Includes
////////////////////////////
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include "arcball.h"
////////////////////////////
// OpenMesh Includes
////////////////////////////
#include "OpenMesh/Core/IO/MeshIO.hh"
#include "OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh"
////////////////////////////
// GL Includes
////////////////////////////
#include "GLee.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
...
struct arcball arcball;
#endif
I have to include the ex1.h header since the ex1.h holds includes for glut functions I
use in the arcball.cpp file as well.
and I have to use the arcball.h header in order to use the functions and the struct
defines on that file.
The error I get is the following:
In file included from arcball.h:11:0,
from arcball.cpp:8:
ex1.h:120:16: error: aggregate ‘arcball arcball’ has incomplete type and cannot be defined
make: *** [ex1] Error 1
I don't understand why it is an incomplete type, since I included arcball.h file
Is this an mutual inclusion issue or a struct definition/usage issue?
And how can it be resolved?
Thanks!
The two .h files include each other, causing much of the confusion. That's a terribly bad idea, as the #ifdef around will have different effects depending on which file is included first: in this case, arcball.h -> ex1.h -> arcball.h-but-really-nothing-because-of-the-#ifdef.
Moreover, it is also a bad idea to declare a variable without extern in a header (here ex1.h). That's probably not having the effect that you want. Variables without extern should only be declared in .c files.
I've been teaching myself some OpenGL using SFML for creating windows/handling inputs, etc. My main.cpp started getting a bit unwieldy so I decided to start splitting my code up. I created a 4X_vertex.h and a 4X_vertex.cpp (4X is the name of the project) and moved the relevant functions and structs out of my main and into these files. However, when I compile, I get the error
variable or field "drawVertexArray" declared void
which from my research seems to be just an unhelpful message relating to the next error, which is
vertex was not declared in this scope
Here's my list of includes from my main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "4x_vertex.h"
#include "4x_constants.h"
My 4X_vertex.h:
#ifndef _4X_VERT_H
#define _4X_VERT_H
struct vertex{
GLfloat x,y,z;
GLfloat r,g,b;
};
void drawVertexArray(vertex v[]);
vertex* loadVertexData();
#include "4X_vertex.cpp"
#endif
The part of 4X_vertex.cpp that's giving me the trouble:
using namespace std;
void drawVertexArray(vertex v[]){
... openGL stuff...
}
All of this worked before I started moving it around so I'm assuming there's something weird going on with the includes, or something. All help is greatly appreciated!
Just some pointers. Best practice is to divide your project up into multiple source files. Typically, you would use the word "main" in the file name of the main source file (if applicable). So you might have something like...
main.cpp
feature1.cpp
feature2.cpp
tools.cpp
For your other files, you will typically name them after the class they implement. You will most often have both a .h and a .cpp. Put your declarations in the .h and your definitions in the .cpp had have the .cpp include the .h. That might give you...
main.cpp
feature1.cpp feature1.h
feature2.cpp feature2.h
tools.cpp tools.h
The modules that reference one of your classes includes it's .h as well. So, main.cpp might look like...
#include <iostream>
#include "feature1.h"
#include "feature2.h"
using namespace std;
void main(int argc, char **argv)
{ ...
cout << "Done!\n";
}
And feature1.cpp might be...
#include "feature1.h"
#include "tools.h"
feature1_class::feature1_class() { ... }
void feature1_class::AUsefulFeature(int val) { ... }
//etc.
...where feature1.h declares the class, defined constants, etc. f.g.,
#ifndef FEATURE1
#define FEATURE1
#include "tools.h"
class feature1_class
{
public:
feature1_class();
void AUsefulFeature(int val);
int APublicMember;
};
#endif
You may have noticed that tools.h is actually include twice in feature1.cpp. It is included from within the feature1.h and explicitly from the .cpp file. If you use the following pattern in your .h files ...
#ifndef TOOLS_H
#define TOOLS_H
//... do your thing
#endif
... then multiple includes shouldn't cause you any problems. And as you refactor code, it is one less thing to have to worry about cleaning up.
If you have been using a single file for all your source up till now, you may have been compiling like so...
cl main.cpp
Which gives you your .exe and .obj and maybe other files. But with multiple source files involved, it isnt much different. You can say...
cl main.cpp feature1.cpp feature2.cpp tools.cpp
There is much more to learn, but this is a start and helps you on the way to better organization of your coding thoughts.
You need to #include "4X_vertex.h" at the top of your 4X_vertex.cpp file. This will allow the .cpp file to see the declaration for the struct vertex.
In general, each file (both .h and .cpp files) needs to #include any header files which contain declarations for items used in that file. This includes the standard headers and OpenGL headers, as well as your custom ones.