View Full Version : Class problems
recluse
03-24-2002, 02:02 PM
Student.cpp: In function `class Student & operator =(const Student &)':
Student.cpp:25: invalid use of `this' in non-member function
Student.cpp: In function `const char * SetLastName(const char *)':
Student.cpp:29: `last_name' undeclared (first use this function)
Student.cpp:29: (Each undeclared identifier is reported only once
Student.cpp:29: for each function it appears in.)
Student.cpp: In function `const char * SetFirstName(const char *)':
Student.cpp:37: `first_name' undeclared (first use this function)
Student.cpp: In function `bool SetProjectGrade(int, int)':
Student.cpp:46: `project_grades' undeclared (first use this function)
Student.cpp: In function `bool SetTestGrade(int, int)':
Student.cpp:61: `test_grades' undeclared (first use this function
These are some of the errors that I'm getting. My guess is that my class implementation doesn't have access to the private data members in the header file. But I did #include "Student.h" , so I'm a bit lost why it can't 'find' these variables.
recluse
03-24-2002, 02:08 PM
OK OK OK. I'll admit it I'm and idiot! Just after posting this I went back to look at the problem and kazzaam! vim spoke to me and said you're missing the thingymajig!
So I added the the Student:: part to the GetLastName() part, and majic occured.
Though I still have a question, what is the thingymajig? Isn't it something like the scope operator?
Now to go work on operator overloading problems... sigh.
yeah. it tells the compiler what scope a function or variable is in. Assuming you're talking about ::.
MattD
03-30-2002, 11:43 PM
Student.cpp: In function `class Student & operator =(const Student &)':
Student.cpp:25: invalid use of `this' in non-member function
well, your using the this operator in a non meber function,
make sure that your operator overload is actually in class scope
its kinda hard to guess the rest without seeing the code,
but before you go off and start coding, take a step back and make sure youve got a handle on *what* your coding and where and why certain function calls need <classname>:: added to them..
Matt D
-= CHECK YOUR HEAD COMEON!!! - ultrasonic =-
Dru Lee Parsec
03-31-2002, 12:54 PM
I once had a particularly arrogant boss tell me that when he sees people use "this" it means :
I'm new at "this".
HE SAID The only place where this should be used if if you have a private member of a class
int age = 0;
and you have a setter like this example:
public void setAge(int age) {
this.age = age;
}
That resolvesthe ambiguity.
Don't know if this is advice or just a story. :)
MattD
04-01-2002, 04:42 AM
Dru Lee Parsec:
yes, you are correct in that 'this' is just another way of expressing 'the current object' in class meber functions.
this is actually a pointer, not a static reference
so your example should be this->age = age :)
as far as im aware, the this operator is able to be referenced
from any meber function, not only those flagged as 'private'
to quote bjarne's c++ book
"in a nonstatic meber function, the keyword 'this' is a pointer to the object for which the function was invoked. in a non-const member function of class X, the type of 'this' is X*. however this is not an ordinary variable; it is not possible to take the address of 'this', or assign to 'this'. in a const meber function the type of 'this' is const X* to prevent modification of the object itself"
the first compiler error is due to the use of the 'this' operator outside of a member function.
I think i know whats causing the other errors, i just dont want to solve the probelm for him/her :)
Recluse:
If you have c++ book handy, re read the chapters that cover member functions, and self reference. Check for how to name member functions, and how to reference member variables.
Matt D
Dru Lee Parsec
04-01-2002, 12:47 PM
this is actually a pointer, not a static reference
so your example should be this->age = age
Once again, years of Thinking In Java has messed me up when trying to think in C. :D
Still, I thought my ex-boss's "I'm New At This" comment was pretty arrogant and rude. But then again I've now got a good career writing code and he's uh . . . I have no idea where he is and I really don't give a damn. :shocked:
You know, a couple of us Java folks were talking at lunch on Friday about how we should beef up our C++ skills again just to know that we still have those skills. Hmmm. A C++ coding Project for Coder Forums?? . . . . .
recluse
04-01-2002, 01:28 PM
Yeah I finally staightened everything out. Most of the problems were due to a lack of sleep. I've been experiencing a slight case of insomina the past week. Just been off my schedule. *shrug*
Dru Lee Parsec: As far as your story/advice. I like both. Real world examples are great.
Yeah I guess I should learn a different method other than using 'this'. Just so I know. ;)
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.