2014-08-31 58 views
0

假設我已經在與我的main.cpp相同的目錄中給出了名爲in.png的圖像。我必須讀取該圖像並將其放入我在main正文中創建的b。我得到了一張class PNG,以及讀取圖像的功能。我也在下面發佈了我的算法,我最後在3年前拿到了C編程語言,現在在C++中,我忘記了很多。爲什麼我的算法沒有工作,出了什麼問題從同一目錄讀取圖像?

在png.h;

class PNG 
{ 
public: 
    PNG(string const & file_name); 
    /** 
    * Copy constructor: creates a new PNG image that is a copy of 
    * another. 
    * @param other PNG to be copied. 
    */ 
}; 

在png.cpp中,我們有;

PNG::PNG(string const & file_name) 
{ 
    _pixels = NULL; 
    _read_file(file_name); 
} 

在main.cpp中,這是我的代碼;

int main 
{ 
    PNG a;       
    PNG*b = NULL; 
    b = a.PNG(string const & in.png); 
    if(*b = NULL) 
     cout << "The image wasn't read" << endl; 
    return 0; 
} 

感謝

編輯------------------------------------- -----------------

你好Mr. R Sahu, 所以,我希望能夠旋轉此圖像。所以,我創建了另一個名爲「b」的對象,我希望能夠從「a」得到旋轉的數據並將其放入「b」中。我決定使用在PNG中聲明的函數,但它被聲明爲一個像素的「類」,就像PNG是一個圖像的類。但是我有錯誤使用它,我的代碼有什麼問題。帶「//」的代碼是我之前嘗試過的並且無法使用的代碼。

代碼有多個文件;

在PNG.h中,我們現在將函數定義爲;

#include <iostream> 
#include "rgbapixel.h" 
class PNG 
{ 
public: 
RGBAPixel * operator()(size_t x, size_t y); 
/** 
* Const pixel access operator. Const version of the previous 
* operator(). Does not allow the image to be changed via the 
* pointer. 
* @param x X-coordinate for the pixel pointer to be grabbed from. 
* @param y Y-cooridnate for the pixel pointer to be grabbed from. 
* @return A pointer to the pixel at the given coordinates (can't 
*  change the pixel through this pointer). 
*/ 
size_t width() const; // returns width 
size_t width() const; 
private: 
// storage 
size_t _width; 
size_t _height; 
RGBAPixel * _pixels; 
}; 

函數在png.cpp中爲我們實現。所以,在main.cpp中,我有我的代碼來使用它們;

#include <algorithm> 
#include <iostream> 

#include "rgbapixel.h" 
#include "png.h" 
using namespace std; 
int main() 
{ 
cout << "me..........." << endl; 
PNG a("in.png"); 
PNG b; 
for(size_t i = 0; i < a.width(); i++) 
{ 
for(size_t j = 0; j <a.height(); j++) 
{ 
// *b(i, j) = *a(j, i);        erata 
// b(i,j) = RGBAPixel * operator()(size_t x, size_t y); 
// b(i, j) = operator()(i, j);    
//b(i,j) = *operator(i,j); 
//b(j,i) = a*operator(i,j); 
//b(j,i) = a.operator(i,j); 
//b(j, i) = a.*operator(i,j); 
} 
} 
b.writeToFile("output.png"); 
return 0; 
} 

我收到使用該功能的錯誤,我不能說出了什麼問題。所以,我不知道我的alogarithm是否會得到旋轉的圖像

有些錯誤;

[[email protected] lab_intro]$ make 
clang++ -std=c++1y -stdlib=libc++ -c -g -O0 -Wall -Wextra -pedantic main.cpp 
main.cpp:24:29: error: expected ')' 
b(i,j) = *operator(i,j); 
         ^
main.cpp:24:28: note: to match this '(' 
b(i,j) = *operator(i,j); 
        ^
main.cpp:24:20: error: use of undeclared 'operator()' 
b(i,j) = *operator(i,j); 
      ^
2 errors generated. 
make: *** [main.o] Error 1 

感謝

回答

1

只需使用:

int main() 
{ 
    // Construct a PNG object by passing the name 
    // of the png file to the constructor. 
    PNG a("in.png"); 
    return 0; 
} 
+0

所以如果我想的形象寫到這的確給定函數的相對的文件,我做貴方覺得a.write_out(「輸出.png「)....會工作嗎? – user124627 2014-08-31 05:15:31

+0

@ user124627,如果'PNG'類具有這樣的功能,那麼是的,它應該。 – 2014-08-31 05:30:49

+0

非常感謝,它確實有這個功能 – user124627 2014-08-31 05:35:14