发生错误的代码部分:
int disk::access (const char fname[]){
int char_count=77;
int actual_count=0; //just to pass the tests.
char c;
cout << "the file name is " << fname << ", and the mode is " << mode << endl;
if(strcmp(mode, "w")==0){
for (int i = 0; i < size; i++) {
//cout << "sgm to be written is " << sgm[i];
fp=fopen(fname, "w");
fprintf(fp, "%s\n",sgm[i]);
}
fclose(fp);
}
if(strcmp(mode,"a")==0){
fp=fopen(fname, "a");
fprintf(fp, "%s\n", sgm);
fclose(fp);
}
fp=fopen(fname, "r");
do{
c=fgetc(fp);
if(c!=' ' && c!='\n')
actual_count++;
}while(c!=EOF);
fclose(fp);
return actual_count;
}
我的错误:
disk.cpp:在成员函数'int disk::access(const char*)'中:
disk.cpp:67:警告:不能通过“...”传递非 POD 类型“类段”的对象;调用将在运行时中止
编辑第 67 行是: fprintf(fp, "%s\n",sgm[i]);
编辑SGM:
cpp代码:
disk::disk(int num_of_segments, const char* temp_mode){
size=num_of_segments;
sgm = new segment[size]; //initializes num_of_segments in a disk
count=0;
if(strcmp(mode, "w")!=0||strcmp(mode, "a")!=0){
strcpy(mode, "w");
}
}
disk::disk(){
sgm = new segment[20]; //initialize 20 segments in a disk
size=20; //keeps track of how many are initialized
count=0; //keeps track of how many are added
strcpy(mode, "w"); //initialize disk access mode to w
}
标头代码:
class disk {
private:
int size, count; //to keep a track of number of segments
char mode [2]; //a for append and w for write
segment* sgm;
FILE *fp;
public:
disk(int num_of_segments, const char *temp_mode);
disk();
~disk();
const char* get_mode( ) const;
segment get_segment(int pos) const;
int get_segment_count( ) const;
const segment* get_all_segments( ) const;
int access(const char fname[ ]);
disk& operator+=(const segment &rhs);
disk& operator=(const disk &dk);
};
我以前没有遇到过这样的警告。我做了一些搜索,从我收集的 POD 中得到“普通的旧数据,它是一个没有构造函数、析构函数和虚拟成员函数的结构”。——格雷格·休吉尔
因此,如果我理解正确,我的错误是我确实有构造函数或析构函数和/或虚拟成员函数,因为它是非 POD?
我想我只是让自己感到困惑,我不确定如何解决这个错误,甚至不知道问题出在哪里。
欢迎所有建议并非常感谢,
谢谢。