Global variable error C++ - c++

I'm having an error I don't know how to fix in my large Operating Systems homework. The error I'm getting is "42 duplicate symbols for architecture x86_64". I presume this is to do with my global variables file "global.h". I have 3 global variables I use and "global.h" is included in an abstract class called "PageReplacementAlgorithm.cpp". I have around 6 classes that are derived from the PageReplacementAlgorithm class and they utilize these global variables. I think the problem comes in when I include all these derived classes in my "main.cpp" as I need to make new instances of them. How can I fix the implementation of the global variables?
Global.h
#include "PageTableEntry.h"
using namespace std;
#ifndef Global_H
#define Global_H
extern PageTableEntry pageTable[64];
extern int* frameTable;
extern int framesCount;
#endif
PageReplacementAlgorithm.h
#include "Global.h"
using namespace std;
#ifndef PageReplacementAlgorithm_H
#define PageReplacementAlgorithm_H
class PageReplacementAlgorithm {
public:
virtual int selectFrame(PageTableEntry &p) = 0;
};
#endif
Example Derived Class (FIFO)
include "PageReplacementAlgorithm.h"
using namespace std;
#ifndef FIFO_H
#define FIFO_H
class FIFO : public PageReplacementAlgorithm {
public:
FIFO();
int selectFrame(PageTableEntry &p);
private:
int entries;
};
#endif
Main.cpp
#include "Aging.cpp"
#include "Clock.cpp"
#include "FIFO.cpp"
#include "MMU.cpp"
#include "NRU.cpp"
#include "Random.cpp"
#include "SecondChance.cpp"

Why do you include all cpp files in main.cpp? I think they contain same includes, right? Even you have the guards there, you do additional includes before that guards and that is probably the source of problems. The main.cpp could contain just main() function and import headers of your classes, there is no need to include cpp.
Also, you can modify your header files to look like this (for sake of extreme safety):
#ifndef PageReplacementAlgorithm_H
#define PageReplacementAlgorithm_H
#include "Global.h"
using namespace std;
...
#endif
I recommend you to look at answer C++ #include guards

If you get rid of #include "(anything).cpp, things should work much better. When you build the project, or run the compiler e.g. g++ main.cpp foo.cpp, that's when those .cpp files get built and linked into your program.

Related

How do I solve this error: include files are nested too deeply

Should I put also my headers, and not only the classes inside the ifndef, define etc?
For example I have, this is just an example of code. Now, my question is, should I put the #include "myfile.h" in the ifndef, or should it stay outside?
#include <iostream>
#include "myfile.h"
#ifndef ANOTHERFILE_H
#define ANOTHERFILE_H
struct node
{
int val;
node*next;
}
#endif //ANOTHERFILE_H

Static Library "Undefined Reference" to object constructor

I'm having this issue where I cannot call the object constructor in main.cpp even after it has been included in main.h. The error message is:
C:\Users\Espresso\Projects\AZRA\Debug/../src/main.cpp:7: undefined reference to `g_editor::LevelEditor::LevelEditor()'
Where main.cpp contains
#include "main.h"
g_editor::LevelEditor g_levelEditor;
and main.h contains:
#include "g_editor/g_editor.h"
g_editor.h contains all of the header files of the objects in the library, which includes the levelEditor. g_editor.h:
#ifndef G_EDITOR_H_
#define G_EDITOR_H_
#pragma once
#include "g_editor/Objects/editor_module.h"
#include "g_editor/Objects/utility_window.h"
#include "g_editor/Objects/prompt_window.h"
#include "g_editor/LevelEditor/LevelEditor.h"
extern g_editor::LevelEditor g_levelEditor;
#endif
And finally, LevelEditor.h contains the constructor and member functions of LevelEditor:
#ifndef G_LEVEL_EDITOR_H_
#define G_LEVEL_EDITOR_H_
#pragma once
#include "../Objects/editor_module.h"
#include "Modules/collisionGrid_module.h"
#include "Modules/HUD_module.h"
#include "Modules/IO_module.h"
#include "Modules/ledge_module.h"
#include "Modules/segment_module.h"
#include "g_level/g_level.h"
using namespace g_level;
namespace g_editor
{
class LevelEditor
{
private:
std::vector<editor_module*> modules;
void loadModules();
public:
static LevelEditor& get()
{
static LevelEditor sSingleton;
return sSingleton;
}
LevelEditor();
~LevelEditor() {};
I apologize for the wall of text, I've been staring at this for a few days now and I have tried reordering the static libraries by precedence (which eliminated all issues save for this one.) Is there a design flaw in my current setup? I am using sSingletons, global externs, and static libraries.
There is no definition of LevelEditor::LevelEditor.
You are either missing a source file, or you forgot to add {}.
Edit: or, if your constructor does not do anything anyway, just remove the declaration.
Either
1) This function is missing:
LevelEditor(); // So now what does this do???? That's what is missing.
or
2) it isn't missing, but you didn't add the source module or library where this function is located to your linker settings.

field declared void / struct or variable not declared in this scope

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.

C++: Strange header error showing up

#ifndef _MY_OPENCLPLATFORM_
#define _MY_OPENCLPLATFORM_
#include "OpenCL.h"
namespace my
{
class OpenCLPlatform
{
cl_platform_id mplatformID;
cl_uint mnumDevices;
std::vector<OpenCLDevice> mdevices; // OpenCLDevice was not declared in this scope
public:
OpenCLPlatform(cl_platform_id platformID);
void getDevices();
void printInfo();
cl_platform_id& getPlatformID();
};
}
#endif
#ifndef _MY_OPENCLDEVICE_
#define _MY_OPENCLDEVICE_
#include "OpenCL.h"
namespace my
{
class OpenCLDevice
{
cl_device_id mdeviceID;
public:
OpenCLDevice(cl_device_id device);
void printInfo();
void printDeviceType(cl_device_type deviceType);
};
}
#endif
#ifndef _MY_OPENCL_
#define _MY_OPENCL_
#if defined(__APPLE__) || defined(MACOSX)
#include <OpenCL/opencl.h> // This works only for XCODE compiler
#else
#include <CL/cl.h>
#endif
#include <cassert>
#include <iostream>
#include <vector>
#include "Exception.h"
#include "OpenCLDevice.h"
#include "OpenCLPlatform.h"
namespace my {
class OpenCLDevice;
class OpenCLPlatform;
class OpenCL;
class OpenCL
{
cl_uint mnumPlatforms;
std::vector<OpenCLPlatform> mplatforms;
void getPlatforms();
public:
OpenCL();
~OpenCL();
void quickSetup();
void printPlatformVersions();
};
}
#endif
Does the the ordering "class OpenCLDevice; class OpenCLPlatform; class OpenCL;" matter? Sometimes, header files depend on each other which can lead to "hard to follow" or convoluted inclusions...Do you have a "one way" technique to deal with convoluted inclusions that you use all the time?
Edit:
I changed the code to match my real problem. If you look at the code above, the compiler is saying that 'OpenCLDevice was not declared in this scope'.
Edit:
I finally got the code to work, and this is what I did:
1. add #include "OpenCLDevice.h"in OpenCLPlatform.h
2. compile
3. remove #include "OpenCLDevice.h"in OpenCLPlatform.h
4. compile
It works now!
Edit:
I cleaned the project and removed all dependencies, and I'm getting the same errors again.
Edit:
I think compiler did something to the code. It may have chose to not include libraries that aren't used in the header and source file, but are used in other headers and source codes
Since you are including classa.h and classb.h where both classes are (presumably) defined, you shouldn't even need the forward declaration.
However, if you did not include them, then no, order of the declarations wouldn't matter. As long as as a class is forward declared before it is used you should be OK.
I see two potential issues:
Your #include "OpenCL.h" may not include the file you expect (yours), but instead some system file.
Forward declarations can't be used in your case. It works only when you have pointers or references to class instances. Your vector<OpenCLPlatform> requires the class declaration (i.e. inclusion of the corresponding header).

Problem In Separating The Interface And The Implementation

Its the first time I am trying to separate the class in a separate header file but I am getting an error.Please help me out.Thanks
CODE:
My main function:
#include <iostream>
#include <MyClass>
int MyClass::data;
int main()
{
cout<<"data="<<MyClass::data;
system("pause");
return 0;
}
MyClass.h
#ifndef MyClass
#define <MyClass>
class MyClass
{
static int data_;
};
#endif
Error: fatal error C1083: Cannot open include file: 'MyClass.h': No such file or directory
You should use
#include "MyClass.h"
angle brackets are for system headers.
Also it's data or data_?
Also it would be better something like
#if !defined(MYCLASS_H_INCLUDED)
#define MYCLASS_H_INCLUDED
...
#endif
#define-ing a name identical to the class name is going to be a source of problems
First good idea to separate definition and implementation in C++. Your #include directive shall use " and not < > as your header is not a system header. Or your header is not lying inside the same directory than the cpp file.
That is another topic but OO is more than just using some classes. Encapsulating static variables inside a class doesn't make them less global... At least they have another namespace...
use #include "Myclass.h" instead of #include