Question

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.

Était-ce utile?

La solution

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

struct segment { char name[10]; int index; }

should be handled either as

print("%s:%d\n", seg.name, seg.index);

or as

inline std::ostream& operator << (std::ostream& os, const struct segment& seg)
{
return os<<seg.name<<":"<<seg.index;
}

and your method call becomes:

std::ostringstream os;
os<<seg;
std::string segStr = os.str();
printf("%s\n", segStr.c_str());

you can also hand code a toString() type function to get a string representation of a segment.

Autres conseils

If sgm contains virtual methods or is a class or struct, you're relying on the implementation on the line:

fprintf(fp, "%s\n",sgm[i]);

With %s you're telling fprintf that sgm[i] is a char*. If sgm[i] has virtual functions, this will most likely be false (because of the vfptr). If the member layout of the type doesn't put a char* as the first data member, this will, again, be false.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top