C ++链接器问题与静态方法 [英] C++ linker problems with static method

查看:207
本文介绍了C ++链接器问题与静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个Vector3D类,在VectorMath类上调用静态方法来执行计算。当我编译时,我得到这个:

I'm writing a Vector3D class that calls a static method on a VectorMath class to perform a calculation. When I compile, I get this:


bash-3.1$ g++ VectorMath.cpp Vector3D.cpp
/tmp/cc5cAPia.o: In function `main':
Vector3D.cpp:(.text+0x4f7): undefined reference to 'VectorMath::norm(Vector3D*)'
collect2: ld returned 1 exit status

代码:

VectorMath.h: p>

VectorMath.h:

#ifndef VECTOR3D_H
#include "Vector3D.h"
#endif

class VectorMath {
    public:
    static Vector3D* calculatePerpendicularVector(Vector3D*, Vector3D*);
    static Vector3D* norm(Vector3D*);
    static double length(Vector3D*);
};

VectorMath.cpp

#include "VectorMath.h"
Vector3D* norm(Vector3D* vector) { // can't be found by linker
    // do vector calculations
    return new Vector3D(xHead, yHead, zHead, xTail, yTail, zTail);
}
// other methods

Vector3D.cpp < b>

Vector3D.cpp

#include "Vector3D.h"
#include "VectorMath.h"
// ...
// vector implementation
// ...
int main(void) {
    Vector3D* v = new Vector3D(x, y, z);
    Vector3D* normVector = VectorMath::norm(v); // error here
}

为什么链接器找不到 VectorMath :: norm 方法?乍一看,我认为我需要像这样声明规范:

Why can't the linker find the VectorMath::norm method? At first glance I'd think that I'd need to declare norm like this:

Vector3D* VectorMath::norm(Vector3D* vector) {

但这不能帮助...

推荐答案

您错过了这一点:

//VectorMath.cpp
#include "VectorMath.h"

             |
             V - here
Vector3D* VectorMath::norm(Vector3D* vector)
{
    ...
}

norm 函数是 VectorMath :: 。没有它,你只有一个自由的功能。

The norm function is part of VectorMath::. Without that, you just have a free function.


,但为什么你使用指针的一切?这更干净:

This is more about your design, but why are you using pointers to everything? This is much cleaner:

class VectorMath {
    public:
    static Vector3D norm(const Vector3D&);
};

参考,你是C ++,所以不要写C代码。当我调用这个时会发生什么?

Take references, you're in C++ so don't write C code. What happens when I call this?

VectorMath::norm(0); // null

它会崩溃,你必须放入支票,在这种情况下,什么应该返回吗?

It will either crash, you have to put in a check, in which case, what should it return? This is all cleaned up by using references.

此外,为什么不让这些 Vector3D 类的成员?

Also, why not just make these members of the Vector3D class?

Vector3D* v = new Vector3D(x, y, z);
v->norm(); // normalize would be better, in my opinion

最后,堆栈分配的东西。您的代码现在有内存泄漏:

Lastly, stack-allocate things. Your code right now has a memory leak:

int main(void) {
    Vector3D* v = new Vector3D(x, y, z);
    Vector3D* normVector = VectorMath::norm(v); 

    // delete v;
    // ^ you're not deleting it!
}

将其更改为此,并使用RAII 概念:

Change it to this, and use RAII concepts:

int main(void) {
    Vector3D v(x, y, z);
    Vector3D* normVector = VectorMath::norm(v);

    // delete v;
    // ^ you're not deleting it!
}

通过 norm 一个成员函数,你最终得到的代码非常干净:

And by making norm a member function you end up with the very clean code:

int main(void) {
    Vector3D v(x, y, z);
    Vector3D normVector(v.norm());
}

没有指针,没有泄漏,所有性感。

No pointers, no leaks, all sexy.

这篇关于C ++链接器问题与静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆