-1

I use the online c++ editor https://www.tutorialspoint.com/compile_cpp_online.php and wasn't sure why It gives me this error:

main.cpp: In function 'int main(int, char**)':                                                                                                                
main.cpp:87:127: error: invalid use of non-static member function                                                                       
cout<<setw(20)<<student[i].getLastName()<<setw(20)<<student[i].getFirstName()<<setw(20)<<fixed<<setprecision(2)<<student[i].getGPA<<endl;  

main.cpp:90:16: error: 'system' was not declared in this scope                                                                                       
system("PAUSE"); 

It points at the student[i].getFirstName() and I am not sure why. To me the syntax looks ok, but I am not sure if I am missing something?

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Student {
    private:
        string sFirstName;
        string sLastName;
        float sGPA;

    public:
        Student();
        void setFirstName(string name);
        void setLastName(string address);
        void setGPA(float gpa);
        string getFirstName();
        string getLastName();
        float getGPA();
};

Student::Student()
{
    sFirstName="";
    sLastName="";
    sGPA=0.00;
}

void Student::setFirstName(string fName)
{
    sFirstName = fName;
}

void Student::setLastName(string lName)
{
    sLastName = lName;
}

void Student::setGPA(float gpa)
{
    sGPA=gpa;
}

string Student::getFirstName()
{
    return sFirstName;
}

string Student::getLastName()
{
    return sLastName;
}

float Student::getGPA()
{
    return sGPA;
}


//int main(int argc, char** argv) {
int main(int argc, char** argv) {
    //int size =10;
    Student student[3];
    string tempString;
    string tempString2;
    float tempFloat;
    int tempInt;
    for(int i = 0; i <3; i++)
    {
        cout<<"Please enter student's first name: "<<endl;
        cin>>tempString;
        cout<< "Please enter student's last name: "<<endl;
        cin>>tempString2;
        cout<< "Please enter student's first name:" <<endl;
        cin>>tempFloat;

        student[i].setFirstName(tempString);
        student[i].setLastName(tempString2);
        student[i].setGPA(tempFloat);
    }

    //20
    cout<<setw(20)<<"Last Name"<<setw(20)<<"First Name"<<setw(20)<<"GPA"<<endl;
    for(int i = 0; i<3; i++)
    {
        cout<<setw(20)<<student[i].getLastName()<<setw(20)<<student[i].getFirstName()<<setw(20)<<fixed<<setprecision(2)<<student[i].getGPA<<endl;

    }
    system("PAUSE");
    return 0;
}
Sol
  • 255
  • 2
  • 9
  • 1
    Related: http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong – Saravanan Sachi Nov 21 '16 at 01:19
  • I see nothing relevant to OOP. –  Nov 21 '16 at 01:20
  • 4
    getGPA is missing parenthesis. – Captain Giraffe Nov 21 '16 at 01:20
  • There is a Class in here for OOP. Class Student. I don't get people being so negative for asking a question and trying to find a solution. (down votes) – Sol Nov 21 '16 at 01:31
  • I have seen that about system("PAUSE") being a bad idea, but never had seen any solution for it (such as another one line command, because I really don't like do while loops very much either, because they can be buggy sometimes). – Sol Nov 21 '16 at 01:36

2 Answers2

1

In this statement

   cout<<setw(20)<<student[i].getLastName()<<setw(20)<<student[i].getFirstName()<<setw(20)<<fixed<<setprecision(2)<<student[i].getGPA<<endl

use

student[i].getGPA()

instead of

student[i].getGPA

and include header <cstdlib>

#include <cstdlib>
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Without the () operator, the function student[i].getGPA() isn't called. The call to student[i].getGPA is evaluating the address of the method getGPA. As for the other error, include cstdlib as pointed out by Vlad

Conjecture
  • 353
  • 4
  • 18