Hey guys,

i am a beginner in c++ and i don't know what's wrong with my code. i'dont get any errors, but when i run the programme the following:
"...
Line: 251
Expression: vector iterators incompatible
..."

I think there is a problem with "b.getmark()" and "b.getcoursename()" but i don't know how to solve it since i cannot use "coursename" or "mark" instead

Thanks in advance, guys. I hope someone can help me!

My code looks a little bit strange cos some parts are not displayed properly... strangely the problematic parts.
Sorry about that.

This is my code:

   #include <iostream>
   #include <string>
   #include <vector>

   using namespace std;

   class physics {

friend ostream & operator<<(ostream &os, physics &b);

    private:
    string studentname;
    int studentid;
    vector<string> coursename;
    vector<double> mark;

    public:
    //access functions
    string getstudentname(){return studentname;}
    int getstudentid(){return studentid;}
    vector<string> getcoursename(){return coursename;}
    vector<double> getmark(){return mark;}

    void enter(){
        studentname = "John";
        studentid = 12345;
        coursename.push_back("astro");
        coursename.push_back("qm");
        mark.push_back(60);
        mark.push_back(71);
    }

 }; // END of CLASS physics

//output stream

ostream & operator<<(ostream &os, physics &b){
os<<"Student name: "<<b.getstudentname()<<" "<<"Student ID: "<<b.getstudentid()<<" "<<endl;

vector<string>::iterator courseit;

vector<double>::iterator markit;

for(markit=b.getmark().begin(), courseit=b.getcoursename().begin();markit<b.getmark().end();++markit,++courseit){

os<<" "<<courseit&amp;amp;lt;&amp;amp;lt;" "&amp;amp;lt;&amp;amp;lt;markit<<endl;

}

return os;

} // end of ostream

 // Main Programme
int main()
 {

 physics ph;
 ph.enter();

cout<<ph<<endl;

 return 0;

 } // END OF MAIN

Recommended Answers

All 5 Replies

one line doesn't make sense:
it should be:

os<<" "<<courseit&amp;amp;lt;&amp;amp;lt;" "&amp;amp;lt;&amp;amp;lt;markit<<endl;

instead of:

os<<" "<<courseit&amp;amp;lt;&amp;amp;lt;" "&amp;amp;lt;&amp;amp;lt;markit<<endl;

sorry about that. i tried to edit my post but i didn't work.

What happens if you change your for loop to

for(markit = mark.begin(), courseit = course.begin(); markit < mark.end(), 
courseit < course.end(); ++markit, ++courseit)

I think the problem you are having is that you are trying to use iterators from temporary objects but I could be wrong.

hey thanks for replying.

unfortunately, that doesn't work since i define my operator function outside my class. i've already tried it.i got 6 errors:
error C2065: 'mark' : undeclared identifier
error C2228: left of '.begin' must have class/struct/union

(3 times)

do you have another good idea how to solve this problem?

when i use a normal function inside the class to print out to screen or file, it works cos i can use mark.begin() instead of getmark().begin() and so on

What if you make your get functions return references?

vector<string> & getcoursename() {return coursename;}
vector<double> & getmark() {return mark;}
commented: I missed that one. Good Job +0

thanks a lot, mate!
That works fine. i don't understand why but as long as it does its job i'm happy :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.