在 c++ 中对 class::function 的另一个未定义的引用
Another undefined reference to class::function in c++
我在编译程序时不断遇到这个常见错误。我已经尝试了互联网上的许多建议,但到目前为止都没有奏效。我尝试通过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)
因此,您正在声明一个函数并定义另一个函数。这就是链接器找不到函数定义的原因。使用成员函数(无论是静态函数还是非静态函数(时,都需要在其类中限定它们的作用域。
此外,括号<包括>用于系统库。其他任何内容都应使用带引号的"包含"。包括>
相关文章:
- 在VSCode中编译C++给了我一个未定义的参考
- 我对C++程序有一个未定义的参考,不知道如何处理它
- 是否有可能通过指向另一个未关联的子对象的指针来获取指向一个子对象的指针?
- C++:如何从另一个谓词定义泛型而不是谓词
- 为什么这是一个未定义的行为?
- GLM如何在不声明内联函数并在另一个(未连接的?)文件中将其定义的函数逃脱
- 在链接链接静态(GSOAP)库的共享库时,为什么会得到一个未定义的符号
- yaml-cpp:创建一个未定义的节点
- mac构建有一个未定义的符号,而linux构建并没有用于相同的代码
- 为什么我有一个未定义的对友元函数的引用
- 对于自定义类的一个未定义集,是否有一个默认的散列函数
- 从Java到C++:如何在另一个自定义类中使用一个自定义类名var
- 将std::字符串的一部分复制到另一个未初始化的字符串
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 在定义之前声明一个未定义的 std::function 的目的是什么?
- 在 c++ 中对 class::function 的另一个未定义的引用
- 具有两个窗口的C++中的应用程序.任务栏中显示的一个窗口.另一个未显示
- 有没有办法放入一个未定义大小的数组并作为静态数组继承
- 另一个未定义的引用错误
- 对boost::system::generic的另一个未定义引用