C++ 我的函数在我的 Image 类中工作,但不在主例程中工作
C++ My functions work within my Image class but not in the main routine
这些方法在我的其他图像函数中起作用,但在我的主要例程中不起作用。 我确定这是一个简单的问题,但我就是想不通......
我收到编译时错误
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
。
相关文章:
- 为什么我的 std::ref 无法按预期工作?
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 我的评分程序无法正常工作
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 编译在我的 Mac 上工作,但在集群 (Linux) 上不起作用
- #if 如何工作?我误解了 #if~#endif 的形式吗?
- 如何使实例化在我的 OpenGL 程序中工作?
- 我的文件无法正常工作,无法从C++文件中读取上下文
- 我的代码无法正常工作。谁能指出错误?
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- cout 在我的程序上无法正常工作,有人可以帮助我吗?
- 为什么我的 while 循环对于特定输入中断,而对于其他输入工作正常?
- 为什么我的模运算符无法正常工作?
- 我无法让我的程序工作,我一直得到未定义的符号:C
- 在矩阵逆变器上工作,由于某种原因,我的指数循环不起作用
- 为什么我的类工作正常,即使在返回垃圾值作为赋值运算符和空复制构造函数的返回之后
- 我的随机生成器是否不工作,或者我决定人/骨架是否击中对手的方式是否有错误
- 我的C++程序的工作目录是什么
- 为什么这段代码不能正常工作???我的意思是,为什么当我运行它时它没有显示任何内容?