2014-10-27 43 views
-3

實現類當我嘗試編譯一個簡單的.cu.h它給我的錯誤:CUDA C - 不能編譯在頭文件中聲明,並在.CU

name folloewd by "::" must be a class or namespace name 

我用下面的命令在cmd中

nvcc -c CuVec3.cu 

注:我已經能夠編譯以這種方式在以前的事了,但從來沒有在.h然後在實施聲明的類。

這裏是CuVec3.h

#define __CUVEC3_H__ 
#ifndef __CUVEC3_H__ 

class CuVec3{ 
public: 
    float x, y, z; 
public: 
    __host__ __device__ CuVec3(void); 
    __host__ __device__ CuVec3(float, float, float); 
    __host__ __device__ CuVec3 add(CuVec3*); 
    __host__ __device__ CuVec3 subtract(CuVec3*); 
    __host__ __device__ CuVec3 multiply(float); 
    __host__ __device__ CuVec3 divide(float); 
    __host__ __device__ float dot(CuVec3*); 
    __host__ __device__ CuVec3 cross(CuVec3*); 
    __host__ __device__ float magnitude(void); 
    __host__ __device__ CuVec3 normalize(void); 
public: 
    __host__ __device__ inline CuVec3 operator+(const CuVec3); 
    __host__ __device__ inline CuVec3 operator-(const CuVec3); 
    __host__ __device__ inline CuVec3 operator*(const CuVec3); 
    __host__ __device__ inline CuVec3 operator/(const CuVec3); 

    __host__ __device__ inline CuVec3 operator+=(const CuVec3); 
    __host__ __device__ inline CuVec3 operator-=(const CuVec3); 
    __host__ __device__ inline CuVec3 operator*=(const CuVec3); 
    __host__ __device__ inline CuVec3 operator/=(const CuVec3); 
}; 

#endif 

這裏是CuVec3.cu

#include "CuVec3.h" 

__host__ __device__ CuVec3::CuVec3(void){ // Error thrown on this line <----- 
    x = 0.0f; y = 0.0f; z = 0.0f; 
} 
__host__ __device__ CuVec3::CuVec3(float x, float y, float z){ 
    this->x = x; 
    this->y = y; 
    this->z = z; 
} 
__host__ __device__ CuVec3 CuVec3::add(CuVec3 *v){ 
    x += v->x; 
    y += v->y; 
    z += v->z; 
    return *this; 
} 
// ... Other things implemented 
+2

頭文件中有一個破壞的預處理器定義序列。 'CuVec3'永遠不會被導入翻譯單元。 – talonmies 2014-10-27 07:04:59

+2

如果我可以澄清@talonmies說的話,那麼你有前兩行'CuVec3.h'開關。因此,編譯器從不會看到'#ifndef'中的代碼。 – 2014-10-27 07:32:15

+0

@AviGinsburg想提供一個答案?我會upvote。這顯然是一個關鍵問題。 – 2014-10-27 13:12:50

回答

2

至於建議由羅伯特Crovella,我改變了我的評論一個答案。

CuVec3.h前兩行是(#混合版本)所謂的「#include guard」。爲了避免由於多個#includes多次包含單個文件。更多細節請參見Wikipedia

如果我們看預處理器看到的前兩行,我們可以理解發生了什麼。第一行定義了__CUVEC3_H__,基本上是一個標誌。第二行(#ifndef)檢查是否定義了宏名稱(__CUVEC3_H__),如果它是而不是,則使用該代碼直到#else#endif。使用#ifdef會做相反的事情(儘管這裏沒有幫助)。所以,預處理器看到__CUVEC3_H__已經被定義並跳過了所附的代碼。實際上,在.cu文件中的#include "CuVec3.h"添加了以下行。

#define __CUVEC3_H__ 

切換.h文件中的前兩行,它應該編譯。

+0

我真的很討厭這就是答案。 – MichaelMitchell 2014-10-27 23:57:23