Connecting Tech Pros Worldwide Forums | Help | Site Map

CLASS ?

tvn007
Guest
 
Posts: n/a
#1: Dec 3 '05
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<<endl;
cout <<"Midterm is : "<<midterm<<endl;
cout <<"Quiz is : "<<quiz<<endl;
cout <<"Final is : "<<final<<endl;
cout <<"Testname is : "<<testname<<endl;
}

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

stringstreams>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm;
stringstreams>>ptrstudent->quiz>>ptrstudent->final>>ptrstudent->testname;
ptrstudent++;
}

for (ptrstudent=student; ptrstudent->midterm;ptrstudent++){
if (ptrstudent->bypassed.find("Y")!=string::npos)continue ;
double test= ptrstudent->quiz +100;
cout << "test is : "<<test<<endl;
ptrstudent.print_data(); //line 46
stringstream stream,stream2;
stream << "Do Add Bonus: "<<ptrstudent->name <<" "
<<ptrstudent->quiz;
string task(stream.str());
cout << task << endl;

stream2 <<"Do ExtraCredit: "<<ptrstudent->name<<" :"
<<ptrstudent->final<< " has done extra credit " <<ptrstudent->final+5;
string task2(stream2.str());
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
Guest
 
Posts: n/a
#2: Dec 3 '05

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.print_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


Alf P. Steinbach
Guest
 
Posts: n/a
#3: Dec 3 '05

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.print_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?
Closed Thread