2

我正在编写一个 c++ 库并对其进行测试,我使用命令行工具编译它而不使用任何 ide,当我编译库和测试程序时没有发现错误,但是当我运行程序时它有时会产生错误,有时会正常工作,当我得到一个错误是:“找不到指定的过程”我在我的库头中使用了 dllexport,并在我的测试中使用了编译指示注释(lib,...),我的编译命令就像这样:

cl -o library.dll quaternion.cpp /link /DLL
cl program.cpp

完整的运行输出:

q = 10 + 10 i + 10 j + 10 k
p = 1 + 1 i + 1 j + 1 k
testing *= operator:
q = -20 + 20 i + 20 j + 20 k
testing * operator:
q = -20 + 20 i + 20 j + 20 k
testing /= operator:
q = 10
testing / operator:
q = 10
testing += operator:
q = 11 + 11 i + 11 j + 11 k
testing + operator:
q = 11 + 11 i + 11 j + 11 k
testing -= operator:
q = 9 + 9 i + 9 j + 9 k
testing - operator:
q = 9 + 9 i + 9 j + 9 k
testing = operator:
q = 1 + 1 i + 1 j + 1 k
testing ^ operator:
power 2:
q = -200 + 200 i + 200 j + 200 k
power 3:
q = -80000 - 80000 i - 80000 j - 80000 k
power 4:
q = -1.28e+010 + 1.28e+010 i + 1.28e+010 j + 1.28e+010 k
power 0:
q = 1
testing norm function:
||q|| = 20

和错误运行输出:

q = 10 + 10 i + 10 j + 10 k
p = 1 + 1 i + 1 j + 1 k
testing *= operator:
q = -20 + 20 i + 20 j + 20 k
testing * operator:
q = -20 + 20 i + 20 j + 20 k
testing /= operator:
q = 10
testing / operator:
q = 10
testing += operator:
q = 11 + 11 i + 11 j + 11 k
testing + operator:
error!The specified procedure could not be found.

这是我的代码:

四元数

#include <cstdlib>
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#ifndef QUATERNION_H
#define QUATERNION_H

#ifdef EXPORTING_DLL
    #define DllExport  __declspec( dllexport )
#else
    #define DllExport  __declspec( dllimport )
#endif
using namespace std;

class Quaternion
{
    public:
        float X, Y, Z, W;
        DllExport Quaternion();
        DllExport Quaternion(const Quaternion& orig);
        DllExport Quaternion(float x, float y, float z, float w);
        DllExport ~Quaternion();

        //base math
        DllExport Quaternion & operator +(const Quaternion &q);
        DllExport Quaternion & operator +=(const Quaternion &q);

        DllExport Quaternion & operator -(const Quaternion &q);
        DllExport Quaternion & operator -=(const Quaternion &q);

        DllExport Quaternion & operator *(const Quaternion &q);
        DllExport Quaternion & operator *=(const Quaternion &q);

        DllExport Quaternion & operator /(const Quaternion &q);
        DllExport Quaternion & operator /=(const Quaternion &q);

        DllExport Quaternion & operator =(const Quaternion &q);

        DllExport Quaternion & operator ^(int i);

        //aditional functions
        DllExport double Norm();// ||q||
        DllExport string ToString();
    private:
};

#endif

四元数.cpp:

#include "quaternion.h"

Quaternion::Quaternion()
{
    X = 0;
    Y = 0;
    Z = 0;
    W = 0;
}

Quaternion::Quaternion(const Quaternion& orig) 
{
    X = orig.X;
    Y = orig.Y;
    Z = orig.Z;
    W = orig.W;
}


Quaternion::Quaternion(float x, float y, float z, float w)
{
    X = x;
    Y = y;
    Z = z;
    W = w;
}

Quaternion::~Quaternion()
{

}

Quaternion & Quaternion::operator +(const Quaternion &q)
{
    return Quaternion(X + q.X, Y + q.Y, Z + q.Z, W + q.W);
}

Quaternion & Quaternion::operator +=(const Quaternion &q)
{
    *this = *this + q;
    return *this;
}

Quaternion & Quaternion::operator -(const Quaternion &q)
{
    return Quaternion(X - q.X, Y - q.Y, Z - q.Z, W - q.W);
}
Quaternion & Quaternion::operator -=(const Quaternion &q)
{
    *this = *this - q;
    return *this;
}

Quaternion & Quaternion::operator *(const Quaternion &q)
{
    return Quaternion(
        X * q.X - Y * q.Y - Z * q.Z - W * q.W,
        X * q.Y + Y * q.X - Z * q.W + W * q.Z,
        X * q.Z + Y * q.W + Z * q.X - W * q.Y,
        X * q.W - Y * q.Z + Z * q.Y + W * q.X
    );
}
Quaternion & Quaternion::operator *=(const Quaternion &q)
{   
    *this = *this * q;
    return *this;
}

Quaternion & Quaternion::operator /(const Quaternion &q)
{
    float square = q.X * q.X + q.Y * q.Y + q.Z * q.Z + q.W * q.W;
    return Quaternion(
        (X * q.X + Y * q.Y + Z * q.Z + W * q.W) / square,
        (X * q.Z - Y * q.X - Z * q.W + W * q.Z) / square,
        (X * q.Y + Y * q.W - Z * q.X - W * q.Y) / square,
        (X * q.W - Y * q.Z + Z * q.Y - W * q.X) / square
    );
}
Quaternion & Quaternion::operator /=(const Quaternion &q)
{
    *this = *this / q;
    return *this;
}

Quaternion & Quaternion::operator =(const Quaternion &q)
{
    X = q.X;
    Y = q.Y;
    Z = q.Z;
    W = q.W;
    return *this;
}

Quaternion & Quaternion::operator ^(int i)
{
    if(i ==0)
    {
        X = 1;
        Y = Z = W = 0;
    }
    else
        while(i > 1)
        {
            *this *= *this;
            i--;
        }
    return *this;
}


double Quaternion::Norm()
{
    return sqrt(X * X + Y * Y + Z * Z + W * W);
}

string Quaternion::ToString()
{
    string result;
    stringstream ss (stringstream::in | stringstream::out);
    ss<<X;
    if(Y>0)
        ss<<" + "<<Y<<" i";
    else  if(Y<0)
        ss<<" - "<<-Y<<" i";
    if(Z>0)
        ss<<" + "<<Z<<" j";
    else if(Z<0)
        ss<<" - "<<-Z<<" j";
    if(W>0)
        ss<<" + "<<W<<" k";
    else if(W<0)
        ss<<" - "<<-W<<" k";
    result = ss.str();
    return result;
}

程序.cpp

#include <stdlib.h>
#include <iostream>
#include "quaternion.h"
#include <exception>
#include <windows.h>
#pragma comment(lib,"library")
#pragma comment(lib,"user32")
#pragma comment(lib,"kernel32")
using namespace std;

int main()
{
    try
    {
        cout<<"Quaternion class library unit test:\n";

        Quaternion p = Quaternion(1, 1, 1, 1);
        Quaternion q = Quaternion(10, 10, 10, 10);
        cout<<"q = "<<q.ToString()<<"\n";
        cout<<"p = "<<p.ToString()<<"\n";

        cout<<"testing *= operator:\n";
        q *= p;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"testing * operator:\n";
        q = q * p;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"testing /= operator:\n";
        q /= p;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"testing / operator:\n";
        q = q / p;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"testing += operator:\n";
        q += p;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"testing + operator:\n";
        q = q + p;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"testing -= operator:\n";
        q -= p;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"testing - operator:\n";
        q = q - p;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"testing = operator:\n";
        q = p;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"testing ^ operator:\n";
        cout<<"power 2:\n";
        q = q ^ 2;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"power 3:\n";
        q = q ^ 3;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"power 4:\n";
        q = q ^ 4;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"power 0:\n";
        q = q ^ 0;
        cout<<"q = "<<q.ToString()<<"\n";
        q = Quaternion(10, 10, 10, 10);

        cout<<"testing norm function:\n";
        cout<<"||q|| = "<<q.Norm()<<"\n";   
    }
    catch(...)
    {
        TCHAR errmsg[512];
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,::GetLastError(),0,errmsg,1024,NULL);
        cout<< "error!"<<errmsg;
    }
}

我没有在 msdn 或 cl 和链接选项中找到任何帮助。我希望任何人都可以提供帮助。

这是当前版本的链接 http://jumbofiles.com/rh0j46ibyt56

4

1 回答 1

5

这不是您问题的答案,但此代码有问题 -

Quaternion & Quaternion::operator +(const Quaternion &q)
 {     
    return Quaternion(X + q.X, Y + q.Y, Z + q.Z, W + q.W); 
} 

您正在创建一个临时对象,然后返回对它的引用。当你在调用者中使用引用时,它已经被破坏了。您不需要返回参考。

编辑:正如下面所指出的,这可能会损坏您的堆栈,因此很可能是您的问题的原因。

于 2012-07-09T07:59:59.803 回答