CLASS ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tvn007

    #1

    CLASS ?

    Could someone please help me or point/explain to me why the error in
    line 46 ?

    ////////////////////////Error message:////////////////////////////////

    bash-2.05b$ g++ test3.cpp
    test3.cpp: In function `int main()':
    test3.cpp:46: error: request for member `print_data' in `ptrstudent',
    which is
    of non-aggregate type `Record_info*'
    //////////////////////////////////////////////////////////////////////////////////////////////////
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    using namespace std;
    const int MAX=50;
    typedef double Number;
    class Record_info {
    public:
    string bypassed;
    string name;
    int midterm;
    Number quiz;
    //double quiz;
    Number final;
    string testname;
    void print_data();
    };
    void Record_info:: print_data(){
    cout <<"name is : "<<name<<en dl;
    cout <<"Midterm is : "<<midterm<<end l;
    cout <<"Quiz is : "<<quiz<<en dl;
    cout <<"Final is : "<<final<<e ndl;
    cout <<"Testname is : "<<testname<<en dl;
    }

    int main (void){
    Record_info student[MAX],*ptrstudent;
    ptrstudent = student;
    ifstream in ("test2.txt" );
    string line;
    string line2;
    char buffer[40];
    while (getline(in,lin e)){
    if (line.find("#") !=string::npos) continue ;
    istringstream stringstreams(l ine);

    stringstreams>> ptrstudent->bypassed>>ptrs tudent->name>>ptrstude nt->midterm;
    stringstreams>> ptrstudent->quiz>>ptrstude nt->final>>ptrstud ent->testname;
    ptrstudent++;
    }

    for (ptrstudent=stu dent; ptrstudent->midterm;ptrstu dent++){
    if (ptrstudent->bypassed.find( "Y")!=string::n pos)continue ;
    double test= ptrstudent->quiz +100;
    cout << "test is : "<<test<<en dl;
    ptrstudent.prin t_data(); //line 46
    stringstream stream,stream2;
    stream << "Do Add Bonus: "<<ptrstude nt->name <<" "
    <<ptrstudent->quiz;
    string task(stream.str ());
    cout << task << endl;

    stream2 <<"Do ExtraCredit: "<<ptrstude nt->name<<" :"
    <<ptrstudent->final<< " has done extra credit " <<ptrstudent->final+5;
    string task2(stream2.s tr());
    cout <<task2<<endl ;
    }

    return 0;
    }


    //////////////////////////input file:
    test2.txt//////////////////////////

    ###
    Y Tony 90 -15.2 98.2 math
    N Michael 95 17.2 92.2 physics
    N Amy 82 13.9 78.2 accounting

  • Victor Bazarov

    #2
    Re: CLASS ?

    tvn007 wrote:[color=blue]
    > Could someone please help me or point/explain to me why the error in
    > line 46 ?
    >
    > ////////////////////////Error message:////////////////////////////////
    >
    > bash-2.05b$ g++ test3.cpp
    > test3.cpp: In function `int main()':
    > test3.cpp:46: error: request for member `print_data' in `ptrstudent',
    > which is
    > of non-aggregate type `Record_info*'
    > //////////////////////////////////////////////////////////////////////////////////////////////////
    > [...]
    > int main (void){
    > Record_info student[MAX],*ptrstudent;[/color]

    So, 'ptrstudent' is a _pointer_...
    [color=blue]
    > [...]
    > ptrstudent.prin t_data(); //line 46[/color]

    Pointers do not have members themselves. If you need to access a member
    of a class to which the pointer points, you need to use '->' ("arrow")
    operator:

    ptrstudent->print_data() ;
    [color=blue]
    > [...][/color]

    This is really a very basic stuff, I am surprised you needed to summon
    the help of a newsgroup for that.

    V


    Comment

    • Alf P. Steinbach

      #3
      Re: CLASS ?

      * tvn007:[color=blue]
      > Could someone please help me or point/explain to me why the error in
      > line 46 ?[/color]
      [color=blue]
      > ptrstudent.prin t_data(); //line 46[/color]

      Presumably this is your own code.

      And if it is, ask yourself why you put the three characters 'ptr' at the
      front of the name.

      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is it such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      Comment

      Working...