Undefined reference to global array - c++

I have an array called vel declared inside global.h and defined inside global.cpp. When I try to use it inside a function, get_velocities(), of another class called Robot (inside Robot.cpp), it says:
undefined reference to `vel'
Here are the three files:
1) global.h
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
#include <array>
using std::cout;
using std::cin;
using std::endl;
using std::array;
static constexpr const int marker_num = 10;
static constexpr const int dim = (2 * marker_num) + 3;
extern array <float, 3> vel;
#endif // GLOBAL_H_INCLUDED
2) global.cpp
#include <iostream>
#include <cstdio>
#include <cmath>
#include "global.h"
#include "WorldState.h"
#include "Robot.h"
#include "Sensor.h"
#include "Marker.h"
array <float, 3> vel = {0.0, 0.0, 0.0};
3) Robot.cpp
#include <iostream>
#include <cstdio>
#include <cmath>
#include "global.h"
#include "WorldState.h"
#include "Robot.h"
#include "Sensor.h"
#include "Marker.h"
Robot::Robot(float a, float b, float c){
//ctor
x = a;
y = b;
theta = c;
}
void Robot::get_velocities(){
v_tx = 1.0;
v_ty = 0.0;
omega_t = 0.0;
vel = {v_tx, v_ty, omega_t};
}
Edit:
I did read this question . What I realized was that the global variable requires not just a declaration but also a definition. I have provided this definition inside global.cpp. Also when I include #include "global.cpp" in Robot.cpp, it works (But this is not an acceptable practice). So, I believe this error is due to global.cpp not linking properly.
1) Isn't it a common practice to declare global variables in global.h and keep the definitions in global.cpp? How do I link them properly? I believe that one way is to create a proper make file. However, I am using codeblocks IDE. How do I do it in this IDE?
2) Is it better to eliminate global.cpp and do all definitions for global variables and functions inside the main or the file that uses them?

Declare and define the array vel within global.h. Maybe storing them within the same class would help.

My problem was that the Codeblocks IDE was not linking global.cpp. I found the answer here (first answer). I apologize if this question is a duplicate or combination of duplicates.

Related

C++ undefined reference to class (1 header 2 cpp's)

I am reading a book (C++ for dummies) as well as watching youtube videos to learn how to code. I am currently struggling with very simple class functions.
Main.cpp
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include "Test.h"
using namespace std;
int x;
int main(int nNumberofArgs, char* pszArgs[])
{
combat fight;
cout << x;
fight.dodmg();
cout << x;
return 0;
}
Test.h my header file with the class
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
class combat
{
public:
int dodmg();
void zero_out();
private:
int x;
};
#endif // TEST_H_INCLUDED
Test.cpp class functions
#include "Test.h"
int combat::dodmg()
{
x = x - 5;
return x;
}
void combat::zero_out()
{
x = 20
}
I tried to make this very simplistic just to figure out how to work a class.
I included a lot of #includes just to try and make sure it wasn't something stupid like I needed strings.
I am not sure why but the videos I watched simply had the header say
ifndef TEST_H (of their respective code, mine has an _INCLUDE as well, I tried deleting it and it still didn't work.
My unfortunate errors
on line 14 of main.cpp fight.dodmg(); it says
\Beginning_Programming-CPP\Playing_with_class\main.cpp|14|undefined reference to `combat::dodmg()'|
then below that
||error: ld returned 1 exit status|
How are you compiling this? I think this is an issue because you arent compiling your Test.cpp file. If you arent already, try compiling with the command:
g++ main.cpp Test.cpp -o MyProgram
UPDATE:
Few things, you dont have a closing statement to your #ifndef directive in Text.h, you will need a constructor to set the value of x so i added one to the combat class also you were missing a semicolon in the zero_out function. I added comments to all the lines I changed.
Okay try this:
Test.h
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
class combat
{
public:
combat(); // added constructor
int dodmg();
void zero_out();
private:
int x;
};
#endif // closed #ifndef
Text.cpp
#include "Test.h"
combat::combat() // implemented constructor
{
x = 20;
}
int combat::dodmg()
{
x = x - 5;
return x;
}
void combat::zero_out()
{
x = 20; // added ';'
}
Hope this helps,
Final edit: I dont think you really need your header guards in this scenario, you could remove the "#ifndef, #define, and the #endif" lines and not see a difference really
It sounds like you provide the wrong arguments for the compiler. Your header file (Test.h) simply provides signatures for the methods, but the implementations are given in the source file (Test.cpp).
This is an important part of writing C++ (or C) code. Your compiler does not automatically search for source files, so you need to tell it where to look, e.g.:
g++ -std=c++11 main.cpp Test.cpp -o main

C++: Accessing data of dynamic structures within extern function

I have a problem with passing dynamically allocated structures to a function and accessing it's content.
The program uses mex to pass data from Matlab to C++. I use Visual Studio.
The structure I define in a header in 'InOut.h'
#include <string>
#include <cstdint>
#include <cstdlib>
struct sObjects {
std::string Type;
float *Position;
};
typedef struct sObject sObject;
In the main function I than allocate the structure is in 'MainFcn_Mex.cpp'
#include "MainFcn_Mex.h"
// ...
// get number of Objects from Matlab
int N_Obj = mxGetNumberOfElements(prhs[1]);
sObjects *Objects = new sObjects[N_Obj];
for (int k=0; k<N_Obj; k++) {
// get the pointer pointer map
pMap = mxGetField(prhs[1],k,"Type");
Objects[k].Type = mxArrayToString(pMap);
// get the pointer pointer map
pMap = mxGetField(prhs[1],k,"Position");
// setting pointer to the first Element
Objects[k].Position = (float*)mxGetPr(pMap);
mexPrintf("Objects(%d,1).Type: %s \n", k+1, Objects[k].Type);
}
create_Objects(Objects, N_Obj);
The function create_Objects is in a differente file 'create_Objects.cpp' and included via 'MainFcn_Mex.h':
#include <stdio.h>
#include <direct.h>
#define _USE_MATH_DEFINES
#include "math.h"
#include <cmath>
#include "mex.h"
#include "matrix.h"
#include <cuda.h>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
#include "InOut.h"
void create_Objects(sObjects *Objects, int N_Obj);
The content of 'create_Objects.cpp' so far is:
#define _USE_MATH_DEFINES
#include "math.h"
#include <cmath>
#include "InOut.h"
#include "mex.h"
void create_Objects(sObjects *Objects, int N_Obj)
{
for (int k=0; k<N_Obj; k++) {
mexPrintf("Objects(%d,1).Type: %s \n", k+1, Objects[k].Type);
}
}
Visual Studio tells me:
"error C2676: binary '[' : 'sObjects' does not define this operator or
a conversion to a type acceptable to the predefined operator"
Why can I access the data in the main function and not in seccondary functions?
How can I access a dynamically allocated structure in other functions, when its size isn't known at compile time?
Thanks a lot for your time!
It looks like you are trying to use struct directly as a typedef. Simply add typedef to your struct definition, will turn it into a type.
Like this:
... #include "mex.h"
typedef struct sObjects {
std::string Type;
float *Position;
};
(Otherwise you should use the full struct keyword as in void create_OpticsObjects(struct sObjects &Objects, int N_Obj).)
Your function prototypes don't need the extern qualifier.
You don't need extern unless you want globals variables. You seem to want simply global structs or types in your example, so extern is not required.
Using extern for globals
What you could be referring to is an instance of your object (or a pointer to your object), and that can be made global by using extern. as in this excerpt from the header file:
... #include "mex.h"
typedef struct sObjects {
std::string Type;
float *Position;
};
extern sObjects *pointerToOnesObjects;
Then in ONE source file, you need to declare the 'real' variable as in (this is good to initialise it here):
sObjects *pointerToOnesObjects = NULL;
With this method your variable pointerToOnesObjects is now available globally (in all your source files that use the same header file).

c++ redefinition error even with ifndef

I have several files, and my error is quite odd. I don't know if it might be part of the problem, I work with OpenGL/SDL, with XCode, and I created the files using CMake.
I have files Wall.hpp, Wall.cpp, Player.hpp, Player.cpp, controls.hpp, controls.cpp, main.cpp.
Here is a summarization of the files inclusions:
// Player.hpp
#include <iostream>
class Player{
public :
Player();
int getLifePoints();
int getStaminaPoints();
float getPosX();
float getPosY();
float getPosZ();
void setPosX(float x);
void setPosY(float y);
void setPosZ(float z);
void reducePoints(int damage);
bool isHeroRunning();
void changeHeroRun();
bool isHeroDucking();
void changeHeroDuck();
private :
int lifePoints;
int staminaPoints;
float posX;
float posY;
float posZ;
bool isRunning;
bool isDucking;
};
// Player.cpp
#include "Player.hpp"
using namespace std;
Player::Player(){
this->lifePoints = 100;
this->posX = 0;
this->posY = 0;
this->posZ = 0;
}
int Player::getLifePoints(){
return this->lifePoints;
}
int Player::getStaminaPoints(){
return this->staminaPoints;
}
float Player::getPosX(){
return this->posX;
};
float Player::getPosY(){
return this->posY;
};
float Player::getPosZ(){
return this->posZ;
};
void Player::setPosX(float x){
this->posX=x;
};
void Player::setPosZ(float z){
this->posZ=z;
};
void Player::setPosY(float y){
this->posY=y;
};
void Player::reducePoints(int damage){
this->lifePoints= lifePoints - damage;
}
int lifePoints;
float posX;
float posY;
float posZ;
bool Player::isHeroRunning(){
return isRunning;
}
void Player::changeHeroRun(){
this->isRunning=!this->isRunning;
}
bool Player::isHeroDucking(){
return this->isDucking;
}
void Player::changeHeroDuck(){
this->isDucking=!this->isDucking;
if (isDucking){
this->posZ=this->posZ/2;
} else {
this->posZ=this->posZ*2;
}
}
// Wall.hpp
#ifndef _WALL_H
#define _WALL_H
#include <iostream>
#include <vector>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GL/glfw.h>
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
class Wall{
public :
Wall(GLfloat x1, GLfloat x2, GLfloat z1, GLfloat z2);
//~Wall();
GLfloat getX1();
GLfloat getX2();
GLfloat getY1();
GLfloat getY2();
GLfloat getZ1();
GLfloat getZ2();
int isInZone(GLfloat x, GLfloat y, GLfloat z);
std::vector<GLfloat> add_wall (std::vector<GLfloat> walls);
private:
GLfloat x1, x2, y1, y2, z1, z2;
};
#endif
// Wall.cpp
#ifndef _WALL_C
#define _WALL_C
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GL/glfw.h>
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "Wall.hpp"
#include <vector>
Wall::Wall(GLfloat x1, GLfloat x2, GLfloat z1, GLfloat z2){
this->x1=x1;
this->x2=x2;
this->y1=y_floor;
this->y2=y_ceiling;
this->z1=z1;
this->z2=z2;
}
#endif
// controls.hpp
#ifndef _MACRO_CONTROLS_C
#define _MACRO_CONTROLS_C
#include <vector>
#include "Wall.hpp"
...
#endif
//controls.cpp
#ifndef _MACRO_CONTROLS_C
#define _MACRO_CONTROLS_C
// Include GLFW
#include <GL/glfw.h>
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
#include <iostream>
#include "controls.hpp"
#include <unistd.h>
#define GetCurrentDir getcwd
#include "Wall.hpp"
...
#endif
//main.cpp
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <list>
#include <vector>
#include <time.h>
#include <typeinfo>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GL/glfw.h>
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
#include <unistd.h>
#define GetCurrentDir getcwd
#include <common/shader.hpp>
#include <common/texture.hpp>
#include <common/controls.hpp>
#include "Player.cpp"
#include "Turret.hpp"
#include "Wall.hpp"
The code works well until I put the #include "Wall.hpp" in the files controls.hpp or/and controls.cpp .
I am facing the error "redefinition of Wall", and also after this one "Gl.h included before glew.h".
I think the problem is in the way you include the headers in controls.cpp:
Wall.hpp -> definition for class WALL
controls.hpp -> includes wall.hpp
controls.cpp -> includes wall.hpp and controls.hpp
So basically in controls.cpp you include the definitian for class WALL twice; this might be the cause of your error. If you include only controls.hpp, it you should get rid of the error
I don't think there is enough information here to answer exactly why you are seeing the errors you list in the question. It could be caused by a recursive include cycle of some kind although I have not spotted it yet.
However, I think these problems will disappear if you follow some basic C++ hygiene.
Do not include cpp files. You are supposed to compile each of these separately and then use the linker to link them together. I am not familiar with XCode, but most IDEs will be able to do this automatically if the project is set up correctly. It is also quite common to use an external Makefile to handle these tasks.
In the comments you mentioned that when you did not include cpp files from main.cpp, you got a symbol resolution error. This most likely means that the linking step I mentioned above is not being done properly.
Put include guards in every header (hpp) file but not in the cpp files (they are not needed since from step 1, you do not include them).
I have found plenty of problems in the past stemming from incorrect include guards.
The most common one is copy/pasting from one file to another and forgetting to change the macro name.
Another that can occur is where you have multiple directories in your include tree, with files of the same name in two different directories. In that case you need to include the directory name in the guard macro name.
Yet another source of problems can be mismatching #if / #endif pairs. In most cases this will be obvious because the pre-processor will complain, but if you had two sets of mismatching pairs then it might even out with unexpected results. It's good practice to place comments after the #endif to show which guard they are associated with, eg:
#ifndef WALL_H
#define WALL_H
...
#endif /* WALL_H */
Some IDEs (eg. Eclipse CDT) will add these guards in this format automatically.
Do not include leading underscores in the guard macros (or any other macros for that matter) to avoid possible clashes with system defined macros.
In each file (cpp or hpp) include only the hpp files needed to define the classes, functions, variables etc that you are using in that file.
Each cpp file should have a corresponding hpp file, which declares the items defined in the corresponding cpp file. The cpp file should include the hpp file. Anything included by the hpp file does not need to be re-included by the cpp file.
Many C++ programmers insist on the rule that each class should be defined in a separate file. It's not strictly necessary but can make it easier to maintain the code.

'ifinite' was not declared in this scope

I'm trying to compile shogun toolbox and I'm getting this fault
C:/shogun-3.0.0/shogun-3.0.0/src/shogun/../shogun/mathematics/Math.h: In static
member function 'static int shogun::CMath::is_finite(double)':
C:/shogun-3.0.0/shogun-3.0.0/src/shogun/../shogun/mathematics/Math.h:1255:20: er
ror: 'ifinite' was not declared in this scope
return ifinite(f);
function itself looks like this.
inline static int is_finite(double)
{
#if defined(isfinite) && !defined(SUNOS)
return ifinite(f);
#else
return finite(f);
#endif
}
I believe similar is described here: http://www.alecjacobson.com/weblog/?p=1768, but I'm not sure as I don't include cmath. Any idea what it can be?
Function is isfinite, not ifinite.
You don't include <cmath> but according to Shogun source here, it does include both <cmath> and <math.h> in the wrong order:
#include <shogun/base/SGObject.h>
#include <shogun/lib/common.h>
#include <cmath> <<<<<<
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/lapack.h>
#include <shogun/io/SGIO.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h> <<<<<<
So you are supposed to use std::isfinite.
I just downloaded shogun-3.0.0 from here, and there is no occurrence of the string “ifinite” anywhere in the source. The definition of is_finite in Math.h is:
/// checks whether a float is finite
inline static int is_finite(double f)
{
#if defined(isfinite) && !defined(SUNOS)
return isfinite(f);
#else
return finite(f);
#endif
}
If the errors and source text you entered into the question are correct, perhaps the sources you have were corrupted. You should download the source and try again.

myvector does not name a type

main.cpp
#include <vector>
#include <iostream>
#include "normal.h"
using namespace std;
int main()
{
return 0;
}
normal.h
#ifndef NORMAL_H
#define NORMAL_H
#include <vector>
#include <iostream>
using namespace std;
vector < int > myvector;
myvector.push_back(12);//does not name a type
#endif
I know I need to somehow include vector<int> myvector in main.cpp but can't figure the way. I've looked at my previous programs and didn't need to include anything in main.cpp.
The problem is that the code
myvector.push_back(12); is not inside any function. Outside of functions you may only declare (and possibly initialize) variables, you cannot put other code.
So, even though you can declare your vector in the .h file (probably to have it available in many files) you should move this line inside the main() or some other function.