I am working on a project in C++ which has many functions. I don't want to write them in the main program and would like to write a separate .cpp file for each of the functions. Most of these functions will act on some arrays, so I wish to make these arrays global. So I declared all the arrays in a separate .cpp file called globals.cpp and put them in a globals.h file prefixed with extern. The I wrote the functions and the main program as usual, however when I compile, I get an
Here is what I have:
//globals.cpp
#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"
BZ_USING_NAMESPACE(blitz)
Array<double,2> A(5,5);
In the globals.h file I have
#ifndef GLOBALS_H
#define GLOBALS_H
extern Array<double,2> A(5,5);
#endif
Then I have a function add.cpp, for example
#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"
BZ_USING_NAMESPACE(blitz)
void add.cpp(){
for(int i=0;i<5;i++){
A(i,i)=i*i;
}
}
I obviously include it in the prototype.h file
#ifndef GLOBALS_H
#define GLOBALS_H
void add();
#endif
Finally I have the main program mainprog.c
#include <iostream>
#include <blitz/blitz.h>
#include <blitz/array.h>
#include "prototype.h"
#include "globals.h"
BZ_USING_NAMESPACE(blitz)
int main(){
add();
cout<<A<<endl;
return 0;
}
However when I compile I get the error `globals.h:6:8: error: ‘Array’ does not name a type
and then an error in the add.cpp function saying the error A was not declared.
How do I declare the blitz arrays as global?
Thank you
`
The issue is that your macro to import the namespace(BZ_USING_NAMESPACE) is below your include of globals.h. Thus the Array class you are trying to reference in globals.h is actually blitz::Array or something at that point.
For a simple fix, simply use the BZ_USING_NAMESPACE in globals.h right above your declaration for A.
Always remember to include everything a header file needs in that header file.
#ifndef GLOBALS_H
#define GLOBALS_H
#include <blitz/blitz.h> //No idea if the Array class needs this header.
#include <blitz/array.h>
BZ_USING_NAMESPACE(blitz)
extern Array<double,2> A(5,5);
#endif
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.
Due to error, I needed to implement a header guard in my Header file and Cpp since I have never used it before I don't know what went wrong because on some classes it works and on one it just won't... Originally the problem was bigger, but I think I narrowed it down to the origin of the problem.
LedHandler.h
#ifdef LED_HANDLER_H
#define LED_HANDLER_H
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <FastLED.h>
/* #include "Led/LedFunction.h"
#include "Led/LedStates.h"
#include "Led/Fading.h" */
class LedHandler {
public:
LedHandler(int length, uint16_t pin);
void clear();
void show();
void setColor(int s, int r, int g, int b);
Adafruit_NeoPixel getStrip();
int getLength();
private:
/* LedStates ¤tState;
LedStates &targetState;
Fader<LedStates> &ledFader; */
int length;
Adafruit_NeoPixel strip;
CRGB* leds;
};
#endif
LedHandler.cpp
#ifdef LED_HANDLER_H
#define LED_HANDLER_H
#include <Adafruit_NeoPixel.h>
#include <FastLED.h
#include "Handlers/LedHandler.h"
LedHandler::LedHandler(int length, uint16_t pin) {
...
}
...
#endif
main.cpp
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
#include <Arduino.h>
#include <Scheduler.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "Tasks/WifiTask.h"
//#include "Tasks/Networking/UDPTask.h"
//#include "Handlers/GsonHandler.h"
#include "Handlers/LedHandler.h"
LedHandler ledHandler(60, D6);
Error
src\main.cpp:14:1: error: 'LedHandler' does not name a type
LedHandler ledHandler(60, D6);
^
*** [.pio\build\nodemcuv2\src\main.cpp.o] Error 1
As noted by walnut in comments, first issue is that #ifdef should be #ifndef. Now this directive can never be evaluated to true (because this macro is not defined anywhere).
Also, you shouldn't ever put include guards in your cpp file. As the name suggests, you use them to protect files that included in other files, and cpp files should never be included anywhere.
Now, with include guard in your cpp file, the following happens:
Code is read from top to bottom
ifndef is encountered, it is true (LED_HANDLER_H is not yet defined)
LED_HANDLER_H is defined
Other headers are included
"Handlers/LedHandler.h" is included
Now, it's important what #include directive does. It's a simple copy-and-paste of the file content into another file.
#ifdef LED_HANDLER_H from inside LedHandler.h is checked, and it's false (this macro is already defined in step 3)
The whole content of LedHandler.h is skipped due to include guard.
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
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.
I keep getting this error:
QuadraticProbing.h:54:22: error: ‘Human’ has not been declared
int hash(Human &human, int tableSize );
However, in QuadraticProbing.h, I #include at the top familytree.h, in which the class Human is declared. Does anyone know why I am still getting compilation errors? I think it has to do with multiple redefinition, because in familytree.h, I also #include QuadraticProbing.h because I use some of those functions in the corresponding.cpp file. Here is what I have at the top of each file. Any input would be greatly appreciated!! =]
#ifndef _QUADRATIC_PROBING_H_
#define _QUADRATIC_PROBING_H_
#include "vector.h"
#include "mystring.h"
#include "familytree.h"
----------------------
#ifndef FAMILYTREE_H
#define FAMILYTREE_H
#include "QuadraticProbing.h"
#include "familyRunner.h"