2016-11-24 123 views
1

我有我在代碼的這部分標題中提到的錯誤。錯誤C2664:無法將參數1從'imaging :: component_t *'轉換爲'const imaging :: component_t *''

component_t *buffer = new component_t[3 * width*height]; 
component_t getRawDataPtr(); 

... 
    for (unsigned int i = 0; i < width*height * 3; i = i + 3) { 
     file.read((char *)cR, sizeof(char)); 
     file.read((char *)cG, sizeof(char)); 
     file.read((char *)cB, sizeof(char)); 
     buffer[i] = cR/255.0f; 
     buffer[i + 1] = cG/255.0f; 
     buffer[i + 2] = cB/255.0f; 
    } 
    file.close(); 

    image->setData(buffer); 

... 

void Image::setData(const component_t * & data_ptr) { 
    if (height == 0 || width == 0 || buffer == nullptr) 
     return; 
    for (unsigned int i = 0; i < height*width * 3; i++) 
     buffer[i] = data_ptr[i]; 
} 

我試過圖像 - >使用setData(*緩衝區)或圖像 - >使用setData(&緩存),但也不能工作。如果有人知道如何解決這個問題,我會很感激。 在此先感謝。

+1

如果你想傳遞一個const引用給指針,它的格式爲'component_t * const&'。不是通過引用傳遞是一個好主意。 –

回答

0

您可以更改:

void Image::setData(const component_t * & data_ptr) { 

到:

void Image::setData(const component_t * data_ptr) { 

或:

image->setData(buffer); 

const component_t *cbuffer = buffer; 
image->setData(cbuffer); 
+0

非常感謝你,這正是我想要的:D – Sakis95

0

您嘗試一個const指針賦給一個非const指針

buffer[i] = data_ptr[i]; 

這是不允許的,因爲這違反了data_ptr const的承諾。

相關問題