2014-02-14 38 views
0

Main.cpp的警告:未使用的變量 'arrPixel'[-Wunused可變]

#include <iostream> 
#include <fstream> 
#include "steganography.h" // call steganography class 

const int arrSize = 30000;//array for contains pixels 
using namespace std; 
int main() 

{ 
    char FileName[20] = "new.txt"; 
    char NewFile[20] = "new.ppm"; 
    char arrPixel[arrSize] = {}; 
    int count = 0; 
    int option; 
    Steganography A;//create the reference of steganopragpy class 
    cout<<"Choose Enocde/Decode[1/2]"; // take option from user 
    cin>>option; 
    switch(option) 
    { 
    case 1: 
    cout << "Enter PPM File Name" << endl; 
    cin>>FileName; 
    cout << "Enter Output File Name"<< endl; 
    cin>>NewFile; 
    A.readImage(FileName, arrPixel);//call readImage method 
    cout << "Encoded Successfully completed:" << endl; 
    A.printImage(NewFile, arrPixel);//write ppm 
    break; 
    case 2: 
    cout << "Enter Input File Name" << endl; 
    cin>>FileName; 
    cout << "Enter Output PPM File Name"<< endl; 
    cin>>NewFile; 
    A.readCipherText(NewFile, arrPixel);//call read file method 
    cout << "Decoded Successfully completed:" << endl; 
    A.printCipherText(FileName, arrPixel);//write ppm 
    break; 

     default: 
      cout<<"wrong choice"; 
    } 

    // cout << NewFile << endl; 
    for(int ct = 0; ct > arrSize; ct++) 
    { 
     cout << arrPixel[ct]; 
    } 


    return 0; 
} 

steganography.cpp

#include <iostream> 
#include <fstream> 
#include <string> 
#include "steganography.h" // call steganography class 
const int arrSize = 30000;//array for contains pixels 
using namespace std; 

Steganography::Steganography()//call steganography constructor 
{ 

    char arrPixel[arrSize] = {}; 
} 


void Steganography::readImage(char* FileName, char* arrPixel)//read image 
{ 
    ifstream infile (FileName);//open file 
    if(infile.is_open()) 
    { 
     for(int count = 0; count < arrSize; count++) 
     { 
      infile >> noskipws >> arrPixel[count]; 
     } 
    } 
    else 
    { 
     cout << "Error opening new file." << endl; 
     //abort(); 
    } 
    infile.close(); 
    } 

void Steganography::readCipherText(char* FileName, char* arrPixel)//read text file contains ppm info 
{ 
    ifstream infile (FileName); 
    if(infile.is_open()) 
    { 
     for(int count = 0; count < arrSize; count++) 
     { 
      infile >> noskipws >> arrPixel[count]; 
     } 
    } 
    else 
    { 
     cout << "Error opening new file." << endl; 
     //abort(); 
    } 
    infile.close(); 
    } 

void Steganography::printImage(char* NewFile, char* arrPixel)//write image 
{ 

    ofstream outfile (NewFile); 

    if(outfile.is_open()) 

    { 

     int count = arrSize; 
     for(int i = 0; i < (count - 1); i++) 
     { 
      outfile << arrPixel[i]; 
     } 
    } 
    else 
    { 
     cout << "Error opening new file." << endl; 
     // abort(); 
} 
    outfile.close(); 

} 
void Steganography::printCipherText(char* NewFile, char* arrPixel)//write ppm file 
{ 

    ofstream outfile (NewFile); 

    if(outfile.is_open()) 

    { 

     int count = arrSize; 
     for(int i = 0; i < (count - 1); i++) 
     { 
      outfile << arrPixel[i]; 
     } 
    } 
    else 
    { 
     cout << "Error opening new file." << endl; 
     // abort(); 
} 
    outfile.close(); 

} 

steganography.h

#ifndef STEGANOGRAPHY_H 
#define STEGANOGRAPHY_H 
#include <string> 
#include <vector> 
class Steganography { 
private: 
    // Image header 
    std::string image_type; 
    int width, height; 
    int max_color_depth; 
    // Image data 
    std::vector<int> color_data; 

    // Hidden data 
    std::string ciphertext;  
    int getNthBit(char cipher_char, int n); 

public: 
    Steganography(void);  //Constructor  
    void readImage(char*,char*); 



    void printImage(char*,char*); 



    void readCipherText(char*,char*); 


    void printCipherText(char*,char*); 


    void cleanImage(); 


    void encipher(); 



    void decipher(); 

}; 

#endif 

當我編譯,我得到這個警告:

steganography.cpp: In constructor ‘Steganography::Steganography()’: 
steganography.cpp:11:10: warning: unused variable ‘arrPixel’ [-Wunused-variable] 

有人可以幫我找出問題。另外,我必須寫這行const int arrSize = 30000;在Main.cpp和steganography.cpp中,爲了避免出現錯誤,是否有一種方法只在steganography.cpp上編寫而不會出錯?

回答

3

這裏:

Steganography::Steganography()//call steganography constructor 
{ 

    char arrPixel[arrSize] = {}; 
} 

您聲明稱爲arrPixel一個局部變量,你不使用它。您可以刪除該行。

請注意,您有警告​​,而不是錯誤。

+0

我刪除它,它的工作,謝謝。你知道如何去除const int arrSize = 30000; from main.cpp – user3093902

+0

@ user3093902您可以將大小作爲函數參數傳遞,使用'std :: vector '而不是數組,或使用函數模板來推導大小。閱讀「將數組傳遞給函數」,有很多關於SO的問題。 – juanchopanza