C++ C++;程序读取ppm和pgm文件,对其进行操作并输出。编译错误
我的代码运行良好,直到我为块添加注释,然后我收到两个随机错误。一个声明我不能在pxm_utils.cpp的前面一行中的“}”之前调用函数,另一个声明我在输入的末尾缺少一个括号,但我所有的括号都已签出。非常感谢您提供任何有关此功能和转换功能的帮助 为了全面了解我在做什么,我正试图解决这个问题。 -实现读取和写入PGM和PPM图像文件的功能。在此阶段,将图像存储为1D数组。不要使用向量——自己处理所有内存分配,并将此代码放在支持函数pxm::newimg()和pxm::deleteimg()中。 读取图像文件时,提取文件后缀。确保后缀有效(pgm或ppm),并且magicid(P5或P6)与之匹配。然后提取剩余的头信息,为新图像分配内存,并从输入文件读取图像数据 写入图像文件时,更改名称以指示所做的操作,并确保后缀与文件包含的内容匹配。例如,如果输入为“test.pgm”,唯一的操作为“-invert”,则输出应命名为“test_i.pgm”。如果“-invert”后跟“-convert”,则输出应命名为“test_ic.ppm”。注意后缀的变化。有关“-invert”和“-convert”操作对图像的作用,请参见下面的说明 -实现函数pxm::negative(),该函数计算pgm和ppm照片底片 -实现函数pxm::convert(),该函数将PPM彩色图像更改为PGM灰度图像,反之亦然,具体取决于当前激活的格式 -修改上述代码以使用二维图像索引方案。除了索引本身,您的代码还必须能够分配和释放此类数据结构。此时,您的所有代码都必须基于一组嵌套的循环,这些循环扫过图像的行和列 我现在正在处理两个文件。A pxm_utils.h和pxm_utils.cpp。pxm_magic.cpp已经准备好拥有一个工作代码pxm_magic.cpp Pxm_utils.hC++ C++;程序读取ppm和pgm文件,对其进行操作并输出。编译错误,c++,C++,我的代码运行良好,直到我为块添加注释,然后我收到两个随机错误。一个声明我不能在pxm_utils.cpp的前面一行中的“}”之前调用函数,另一个声明我在输入的末尾缺少一个括号,但我所有的括号都已签出。非常感谢您提供任何有关此功能和转换功能的帮助 为了全面了解我在做什么,我正试图解决这个问题。 -实现读取和写入PGM和PPM图像文件的功能。在此阶段,将图像存储为1D数组。不要使用向量——自己处理所有内存分配,并将此代码放在支持函数pxm::newimg()和pxm::deleteimg()中。 读
#ifndef PXMUTILS_H
#define PXMUTILS_H
#include <string>
using namespace std;
typedef unsigned char uchar;
class pxm {
public:
pxm();
~pxm();
void read(const string &);
void write(const string &, const string &);
void negative();
void convert();
void set_cmap(const char *cmap_fname="jet.cmap");
private:
string magicid; // file identifier: P5 for PGM, P6 for PPM
int maxvalue; // always 255
int nrows, ncols; // data dependent
int bpp; // bytes-per-pixel: 1 for PGM, 3 for PPM
//uchar *cmap;
uchar **cmap;
//store image as 1D array
//uchar *img;
//uchar *newimg(int, int, int);
//void deleteimg(uchar *);
// store image as 2D array
uchar **img;
uchar **newimg(int, int, int);
void deleteimg(uchar **);
};
#endif
\ifndef PXMUTILS\u H
#定义PXMUTILS\u H
#包括
使用名称空间std;
typedef无符号字符;
类pxm{
公众:
pxm();
~pxm();
无效读取(常量字符串&);
无效写入(常量字符串&,常量字符串&);
无效负();
void convert();
void set_cmap(const char*cmap_fname=“jet.cmap”);
私人:
字符串magicid;//文件标识符:P5表示PGM,P6表示PPM
int maxvalue;//始终为255
int nrows,ncols;//依赖于数据
int bpp;//每像素字节数:1表示PGM,3表示PPM
//uchar*cmap;
uchar**cmap;
//将图像存储为1D数组
//乌查尔*img;
//uchar*newimg(int,int,int);
//无效删除img(uchar*);
//将图像存储为二维数组
乌查尔**img;
uchar**newimg(int,int,int);
无效删除IMG(uchar**);
};
#恩迪夫
pxm_utils.cpp
#include "pxm_utils.h"
#include string
#include iostream
#include fstream
#include cstdlib
using namespace std;
//class constructor
pxm::pxm()
{
typedef unsigned char uchar;
uchar ** newimg(int nrows, int ncols,string magicid)
{
uchar **img= new uchar *[nrows];
img[0]=new uchar [nrows*ncols*magicid];
for(int i=1; i<nrows; i++)
{
img[i]=img[i-1]+ncols*magicid;
}
return img;
}
string filetype = "pgm";
magicid = "P5";
nrows = 0;
ncols = 0;
img = NULL;
}
//class deconstructor
pxm::~pxm() {
deleteimg(img);
{if(img)
{ if(img[0])
{delete[] img[0];}
}
}
}
//Reads the header string along with the binary data of img
void pxm::read(const string & fname)
{
ifstream fin(fname.c_str());
fin.open ("test.pgm");
{
if (fin.fail())
{
cout << "Input file opening failed. " << endl;
exit(1);
}
fin >> magicid >> ncols >> nrows >> maxvalue;
{ if( magicid == "P5")
filetype = "PGM";
else if( magicid == "P6")
filetype = "PPM";
}
{ if(maxvalue != 225)
cout << "Image maxvalue was not 225!"<< endl;
exit(1);
}
{ if( ncols >= 0)
cout << "Number of columns can not be negative! \n";
exit(1);
if( nrows >= 0)
cout << "Number of rows can not be negative! \n";
exit(1);
}
while (fin.get() != '\n') {}
img = newimg(nrows, ncols);
fin.read((char *)img[0], nrows*ncols);
cout << "The magicid was: " << magicid << endl;
cout << "Which means the file type is " << filetype << endl;
}
}
//allocate data for cmap
void pxm::set_cmap(const char *cmpa_fname="jet.cmap")
{
cmap = newimg(nrows,ncols,3);
unchar gray, red, green, blue;
for (int i=0; i<nrows; i++){
for (int j=0; j<ncols; j++){
gray= img[i][j];
red= cmap_fname[gray][0];
green= cmap_fname[gray][1];
blue= cmap_fname[gray][2];
cmap[i][(j*3)-2]= red;
cmap[i][(j*3)-1]= green;
cmap[i][(j*3)]= blue;
}
}
}
for (int i=0; i < 256; i++){
cmap[i][0]=cmap_fname[i*3 -21];
cmap[i][1]=cmap_fname[i*3-1];
cmap[1][2]=cmap_fname[i*3];
}
}
//inverses the colors of the image
void pxm::negative()
{
for(int i=0; i<nrows; i++) {
for(int j=0; j<ncols; j++){
int y=img[i][j];
img[i][j]=(255-y);
}
}
}
/*
void pxm:convert()
{
if(magicid=="P6"){
for(int i=0; i<nrows; i++)
{
for(int j=0; j<ncols; j++)
{
int p=img[i][j];
img[i][j]=(0.229*)
}
}
}
if(magicid=="P5")
for(int i=0; i<nrows; i++)
{
for int (j=0; j<ncols; j++)
{
int y= img[i]
}
}
}
*/
//writes out the img file after operation is complete
void pxm::write(const string & fname, const string & fname);
{
ofstream fout (fname.c_str(), ios::out);
size_p dp;
if ((dp= fnamerfind(".pgm")) != string::npos)
{
fout<<"P6"<<endl;
}
if((dp= fname.rfind(".ppm")) != string::npos)
{
fout<<"P6"<<endl;
}
fout<< ncols<< " " << nrows << endl;
fout<< maxvalue << endl;
for(int i=0; i<nrows; i++)
{
for(int j=0; j<ncols; j++)
{ fout<< img[i][j]<< " "; }
fout << endl;
}
fout.close();
}
#包括“pxm_utils.h”
#包含字符串
#包括iostream
#包括fstream
#包括cstdlib
使用名称空间std;
//类构造函数
pxm::pxm()
{
typedef无符号字符;
uchar**newimg(整数nrows、整数ncols、字符串magicid)
{
uchar**img=新uchar*[nrows];
img[0]=新的uchar[nrows*ncols*magicid];
对于(int i=1;i>ncols>>nrows>>maxvalue;
{if(magicid==“P5”)
filetype=“PGM”;
否则如果(magicid==“P6”)
filetype=“PPM”;
}
{如果(最大值!=225)
CUT< P>您正在编写嵌套函数,C++中不允许这样做。特别是<代码> NeWMG被定义为<代码> PMX构造函数。< /P>
有些编译器可能允许它作为一个扩展,但在我看来,这并不值得
解决方法就是将函数移动到类范围。如果不需要访问这个对象,就静态执行
,因为代码的奇怪缩进,这并不明显。 您正在嵌套函数,而C++中不允许这样做。特别是<代码> NeWMG被定义为<代码> PMX构造函数。< /P>
有些编译器可能允许它作为一个扩展,但在我看来,这并不值得
解决方法就是将函数移动到类范围。如果不需要访问这个对象,就静态执行
顺便说一句,这并不明显,因为您的代码有奇怪的缩进。所有这些文本都没有精确的编译器错误……很抱歉,在构造函数“pxm::pxm()”:pxm_utils.cpp:18:错误:在此之前,函数定义是不允许的{'token pxm_utils.cpp:176:错误:输入结束时应为'}'您是否试图在pxm()中定义函数
constructor?为什么?你希望它做什么?正确的缩进不仅仅是看起来漂亮的问题,它是为了让代码的结构对观察者来说是显而易见的。如果缩进错误,你会让读者或你自己感到困惑。我通常不会这样缩进。我对缩进非常敏感,但当我在上面复制代码时,我会感到困惑我不知道有什么简单的方法可以让它与描述的其他部分相比脱颖而出。我会进去手动更改它。谢谢你的帮助。你认为这就是导致错误的原因吗?所有的文本都丢失了,但确切的编译器错误都丢失了…很抱歉,很难尝试解释在构造函数的pxm::pxm()中发生了什么“:pxm_utils.cpp:18:错误:此处不允许在输入结束时的“{”标记pxm_utils.cpp:176:错误:预期“}”之前定义函数您是否试图在pxm()
构造函数中定义函数?为什么?您希望它做什么?正确的缩进不仅仅是