C ++我的函数在我的Image类中工作,但不在主例程中

时间:2018-02-11 04:24:40

标签: c++ function oop main

这些方法适用于我的其他Image函数,但不适用于我的主程序。 我确定这是一个简单的问题,但我无法弄清楚......

我收到编译时错误

    make
g++ -c main.cpp
main.cpp: In function ‘int main()’:
main.cpp:21:35: error: ‘create2dArray’ was not declared in this scope
     testData = create2dArray(10, 8);
                                   ^
main.cpp:35:35: error: ‘fillArray’ was not declared in this scope
     fillArray(moreData, 8, 15, 255);
                                   ^
makefile:4: recipe for target 'main.o' failed
make: *** [main.o] Error 1

我已经将image.h文件正确地包含在main中,并且所有其他图像函数都正常工作,它只是以某种方式看起来超出范围的数组函数。

我的问题是如何修复此错误,我是否在程序中的正确位置声明了方法本身? (它们在image.cpp中)

提前感谢您的帮助。

这是我的Image.h文件:

#ifndef IMAGE_H
#define IMAGE_H

#include <iostream>
#include <iomanip>

using namespace std;

class Image {
    public:
        Image();    // Default constructor
        ~Image();   // Default destructor
        Image(std::string title, int rows, int cols, int **I);  // Creates a Image object
        Image &operator=(const Image &I);   // Overloads the = operator for image objects
        bool operator==(const Image &I);    // Checks if two images are equal returning bool
        Image operator+(const Image &I);    // Adds the two images returning the resulting image
        int getCols();  // Returns the col of the object
        int getRows();  // Returns the rows of the object
        string getTitle(); // Returns the title of the image
        void histogram(int n); // Displays a histogram of the image to the user

        // Overloads the cout<< operator to print Image objects
        friend ostream& operator<<(ostream &output, const Image &I);

        // Overloads the cin>> operator to input Image objects
        friend istream& operator>>(istream &input, Image &I);

        // Creates a 2d array of size rows, containing arrays of size cols, 
        //  returns the resulting 2d array  
        friend int** create2dArray(int rows, int cols); 
        // Deletes a 2d array of int** taking in the array and size of the array as parameters
        friend void delete2dArray(int** array, int size);
        // Fills a 2d array with test data within the range of 1 and rangeHigh, 
        //  returns a 2d array. Takes the array, the size of the array, 
        //  and the highest number you want to generate as prams.
        friend void fillArray(int** array, int size, int rangeLow, int rangeHigh);
    private:
        string title;
        int cols; 
        int rows;
        int **data; 
};

#endif

以下是功能本身:

// Creates a 2d array of size rows, containing arrays of size cols, 
//  returns the resulting 2d array  
int** create2dArray(int rows, int cols)
{
    int **data;
    data = new int*[rows];

    for(int i = 0; i < rows; i++) {
        data[i] = new int[cols];
    }

    return data;
}

// Deletes a 2d array of type int** taking in the array and the size of the array as parameters
void deleteArray(int** array, int size)
{
    for(int i = 0; i < size; i++) {
        delete[] array[i];
    }

    delete[] array;
}

// Fills a 2d array with test data within the range of 1 and rangeHigh, returns a 2d array
// Takes the array, the size of the array, and the highest number you want to generate as prams.
void fillArray(int** array, int rows, int cols, int rangeHigh)
{
    int randomInt;

    // Fills each element of the array with a value between 1 and rangeHigh
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            randomInt = (rand()%rangeHigh) + 1;
            array[i][j] = randomInt;
        }
    }
}

这些功能可以在其他功能中使用,如下所示:

// Adds the two images returning the resulting image
Image Image::operator+(const Image &I)
{   
    // Checks to see if the images are the same size
    if(this->rows == I.rows && this->cols == I.cols) {
        int **newData = create2dArray(this->rows, this->cols);

        // Loops through each element of both arrays and creates a new array of the same
        //  size containing the average of the two arrays at that position
        for(int i = 0; i < this->rows; i++) {
            for(int j = 0; j < this->cols; j++) {
                newData[i][j] = (this->data[i][j] + I.data[i][j]) / 2;
            }
        }

        cout << "working" << endl;

        Image img(this->title + " " + I.title, this->rows, this->cols, newData);

        cout << img;
        return img;

    } else {
        throw string("Incompatible size.");
    }
}

这是main.cpp:

#include "image.h"

using namespace std;

int main()
{
    int **testData;

    // Creates a 2d array of size 10 rows * 8 cols
    testData = create2dArray(10, 8);

    // Adds data to the array
    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 8; j++) {
            testData[i][j] = i*j;
        }
    }

    // Creates another array of size 8 rows * 15 cols
    int **moreData;
    moreData = create2dArray(8, 15);

    // Adds data to the array
    fillArray(moreData, 8, 15, 255);

    Image testImage("Test", 10, 8, testData);
    Image additionalImage("Additional Image", 15, 10, moreData);

    cout << testImage;
    cout << additionalImage;

    Image testImage2;
    testImage2 = testImage;

    cout << testImage2;

    Image newImage;
    cin >> newImage;

    try {
        cout << "testImage + testImage2 = \n" << testImage + testImage2 << endl;
    } catch (string & e) {
        cout << "testImage + testImage2: " << e << endl;
    }

    try {
        cout << "testImage + newImage = \n" << testImage + newImage << endl;
    } catch (string & e) {
        cout << "testImage + newImage: " << e  << endl;
    }


    cout << "good" << endl;
    if(testImage == newImage) {
        cout << endl << testImage.getTitle() << " is equal to " << newImage.getTitle() << endl;
    }

    if(!(testImage == additionalImage)) {
        cout << endl << testImage.getTitle() << " is not equal to " << additionalImage.getTitle() << endl;
    }

    delete testData;
}

1 个答案:

答案 0 :(得分:0)

您需要将create2dArray()fillArray()中缺少的函数原型放在头文件中,以便在main.cpp内声明它们,并确保编译和链接源文件与main.cpp一起实现它们。