生成DLL错误

时间:2017-11-14 22:27:28

标签: c++ dll

首先,我仍然接受dll的概念,现在我正在尝试为自己制作一个,但是当我尝试构建时,我得到了这个:

error C2059: syntax error: '__declspec(dllimport)' 
error C2238: unexpected token(s) preceding ';' 
error C2059: syntax error: '__declspec(dllimport)' 
error C2238: unexpected token(s) preceding ';'  

Vec2.h:

#pragma once

#ifdef VECTORLIB_EXPORTS  
#define VECTORLIB_API __declspec(dllexport)   
#else  
#define VECTORLIB_API __declspec(dllimport)   
#endif  

class Vec2
{
public:
    Vec2() = default;
    VECTORLIB_API Vec2(double x, double y);
    Vec2 VECTORLIB_API operator+(Vec2& rhs);
    Vec2 VECTORLIB_API operator*(double& rhs);
    Vec2& VECTORLIB_API operator+=(Vec2& rhs);
    Vec2& VECTORLIB_API operator*=(double& rhs);


public:
    //public vars///////
    double x, y;
    ////////////////////
};

stdafx.h中:

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN// Exclude rarely-used stuff from Windows headers               
// Windows Header Files:
#include <windows.h>

#include "Vec2.h"


// TODO: reference additional headers your program requires here

Vector2.cpp:

// Vector2.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"


Vec2::Vec2(double x, double y) : x(x), y(y) {}

Vec2 Vec2::operator+(Vec2& rhs)
{
    return Vec2( x + rhs.x, y + rhs.y );
}


Vec2 Vec2::operator*(double& rhs)
{
    return Vec2( x * rhs, y * rhs );
}


Vec2& Vec2::operator+=(Vec2& rhs)
{
    return ( *this = *this + rhs );
}

Vec2& Vec2::operator*=(double& rhs)
{
    return ( *this = *this * rhs);
}

dll是vector2d类的开头供我稍后使用。 我知道代码可能不是它应该的方式,但我只是开始依赖这些概念,所以任何提示都非常受欢迎。

1 个答案:

答案 0 :(得分:1)

VECTORLIB_API放在返回类型之前。见here

VECTORLIB_API Vec2 operator+(Vec2& rhs);

您也可以将它添加到课程中。见here

class VECTORLIB_API Vec2 {
  ...
};