I keep coming across this common error then compiling my program. I’ve already tried many of the suggestions on the internet but none have worked so far. I’ve tried compiling my code using g++ via CMD and Code::Blocks, but both come up with the same error:
C:\Users\Damian\Desktop\test program.cpp|17|undefined reference to `level1::segout(int, double, double)'
I’ve tried compiling in CMD using all the cpp files:
g++ "main program.cpp" "level1.cpp" -o "test.exe"
Here is my code below… maybe I am missing something obvious. It’s been a while since I did C/C++ coding.
main program.cpp
#define _USE_MATH_DEFINES
#include <iostream>
#include <C:\Users\Damian\Desktop\level1.h>
#include <string>
#include <math.h>
using namespace std;
int main()
{
double pitch = 0;
double yaw = 0;
cout << "Low-Level Test Program\nPlease enter the pitch value (in degrees):\n";
cin >> pitch;
cout << "Please enter the yaw value (in degrees):\n";
cin >> yaw;
level1::segout(0, pitch * (M_PI/180), yaw * (M_PI/180));
return 0;
}
level1.h
//all dimensions in MM, all angles in RADIANS
#pragma once
class level1 {
static const double LENGTH_NEUTRAL, ANGLE_MAX, RADIUS, WIRE_RADIUS, ENCODER_RES;
double L1, L2, L3;
public:
static void segout(int, double, double);
};
level1.cpp
#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>
#include <stdlib.h>
//#include <level0.h> --not implemented yet
using namespace std;
const double LENGTH_NEUTRAL = 30;
const double ANGLE_MAX = M_PI/4;
const double RADIUS = 16;
const double WIRE_RADIUS = 0.1;
const double ENCODER_RES = (2*M_PI)/64;
void segout(int Seg, double P, double Y)
{
//work area check
if (sqrt(pow(P, 2) + pow(Y, 2)) <= LENGTH_NEUTRAL + ANGLE_MAX)
{
cout << "ERROR (0001): Specified coordinates are outside work area.\n";
return;
}
//conversion
//stage 1
double L1 = LENGTH_NEUTRAL * (1 - ((sqrt(pow(P, 2) + pow(Y, 2)))/LENGTH_NEUTRAL) * RADIUS * sin((M_PI/2)+atan2(P, Y)));
double L2 = LENGTH_NEUTRAL * (1 + ((sqrt(pow(P, 2) + pow(Y, 2)))/LENGTH_NEUTRAL) * RADIUS * sin((5*M_PI/6)+atan2(P, Y)));
double L3 = LENGTH_NEUTRAL * (1 + ((sqrt(pow(P, 2) + pow(Y, 2)))/LENGTH_NEUTRAL) * RADIUS * cos((2*M_PI/3)+atan2(P, Y)));
//stage 2
L1 = labs((sqrt((L1 * sqrt(4 * pow(LENGTH_NEUTRAL, 2)+ pow(L1, 2)))/(pow(WIRE_RADIUS, 2)) - (pow(L1, 2))/(pow(WIRE_RADIUS, 2)))/sqrt(2)) / ENCODER_RES);
L2 = labs((sqrt((L2 * sqrt(4 * pow(LENGTH_NEUTRAL, 2)+ pow(L2, 2)))/(pow(WIRE_RADIUS, 2)) - (pow(L2, 2))/(pow(WIRE_RADIUS, 2)))/sqrt(2)) / ENCODER_RES);
L3 = labs((sqrt((L3 * sqrt(4 * pow(LENGTH_NEUTRAL, 2)+ pow(L3, 2)))/(pow(WIRE_RADIUS, 2)) - (pow(L3, 2))/(pow(WIRE_RADIUS, 2)))/sqrt(2)) / ENCODER_RES);
//output
//temp output for debugging
cout << "-------\nResults:\n";
cout << "L1: " << L1 << "\n";
cout << "L2: " << L2 << "\n";
cout << "L3: " << L3 << "\n";
}
Here’s an inexhaustive list of the things I’ve tried so far:
- Changing level1 from a static class to a normal class (so I have to create an object for it to work)
- Including the desktop in the search paths
- Compiling level1 to an object first and then compiling it with the main program.
- Turning on Linker AND/OR Compile for the header file in Code::Blocks
- Putting a level1.h #include header in level1.cpp
There’s probably a few more, but’s it’s late and my memory’s going… thanks very much in advance for your help.
Your problem is that
is not the same as
So you are declaring one function and defining another. That’s why the linker can’t find the definition for your function. When working with member functions, be they static or non-static, you need to scope them within their class.
Also, bracket <includes> are for system libraries. Anything else should use quoted “includes”.