1

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

C:\Users\Damian\Desktop\test 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:\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;
}

级别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);
};

一级.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编译成一个对象,再用主程序编译。
  • 在 Code::Blocks 中为头文件打开链接器和/或编译
  • 将 level1.h #include 标头放入 level1.cpp

可能还有一些,但是已经晚了,我的记忆正在消失……非常感谢您的帮助。

4

1 回答 1

4

你的问题是

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

不一样

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

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

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

于 2012-05-19T02:55:34.070 回答