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

C++ My functions work within my Image class but not in the main routine

本文关键字:工作 我的 例程 C++ 函数 Image      更新时间:2023-10-16

这些方法在我的其他图像函数中起作用,但在我的主要例程中不起作用。 我确定这是一个简单的问题,但我就是想不通......

我收到编译时错误

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 中,并且所有其他图像函数都可以正常工作,它只是似乎以某种方式超出范围的数组函数。

我的问题是如何解决此错误,我是否在程序的正确位置删除了方法本身?(它们在图像中.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.");
}
}

这是主要的.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;
}

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