Section of my code where error is occuring:
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;
}
my error:
disk.cpp: In member function 'int disk::access(const char*)':
disk.cpp:67: warning: cannot pass objects of non-POD type 'class segment' through '...'; call will abort at runtime
EDIT line 67 is: fprintf(fp, "%s\n",sgm[i]);
EDIT SGM:
the cpp code:
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
}
the header code:
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);
};
I haven't come across a warning like this before. I did some searching and from what I gathered POD is "plain old data that is a struct without constructors, destructors and virtual members functions." -Greg Hewgill
So if I understood that correctly, my error is that I do have constructors or destructors and or virtual member functions because it is non-POD?
I think I'm just confusing myself, I'm not sure how to go about fixing this error or even pinpointing where the problem is occuring.
All suggestions are welcome and greatly appreciated,
thanks.
I'll venture a guess and say "segment" is a struct or a class and you're trying to print it as a "%s" string.
you need to either implement a function that converts segment to string. or print individual native fields of the segment.
e.g. if you defined segment like this
should be handled either as
or as
and your method call becomes:
you can also hand code a toString() type function to get a string representation of a segment.