在 c++ 中对 class::function 的另一个未定义的引用

Another undefined reference to class::function in c++

本文关键字:另一个 未定义 引用 c++ function class 中对      更新时间:2023-10-16

我在编译程序时不断遇到这个常见错误。我已经尝试了互联网上的许多建议,但到目前为止都没有奏效。我尝试通过CMD和Code::Blocks使用g ++编译我的代码,但两者都出现了相同的错误:

C:UsersDamianDesktoptest program.cpp|17|undefined reference to `level1::segout(int, double, double)'

我尝试使用所有 cpp 文件在 CMD 中编译:

g++ "main program.cpp" "level1.cpp" -o "test.exe"

这是我下面的代码...也许我错过了一些明显的东西。自从我做 C/C++ 编码以来已经有一段时间了。

主程序.cpp

#define _USE_MATH_DEFINES
#include <iostream>
#include <C:UsersDamianDesktoplevel1.h>
#include <string>
#include <math.h>
using namespace std;
int main()
{
    double pitch = 0;
    double yaw = 0;
    cout << "Low-Level Test ProgramnPlease 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;
}

1.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);
};

级别1.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";
}

以下是我迄今为止尝试过的事情的详尽列表:

  • 将 level1 从静态类更改为普通类(所以我必须创建一个对象才能让它工作(
  • 在搜索路径中包含桌面
  • 首先将 level1 编译为对象,然后使用主程序编译它。
  • 在代码::块中为头文件启用链接器和/或编译
  • 将 level1.h #include 标头放入 level1.cpp

可能还有几个,但是已经很晚了,我的记忆还在继续......提前非常感谢您的帮助。

你的问题是

void segout(int Seg, double P, double Y)

void level1::segout(int Seg, double P, double Y)

因此,您正在声明一个函数并定义另一个函数。这就是链接器找不到函数定义的原因。使用成员函数(无论是静态函数还是非静态函数(时,都需要在其类中限定它们的作用域。

此外,括号<包括>用于系统库。其他任何内容都应使用带引号的"包含"。