static function

i have a class named 'student'
inside the class i have a variable called 'studentG'
and another one called 'sumG' (sumG is static)
inside a static member function, i want to add all three 'studentG' values
into 'sumG' because i have created 3 objects for this class

any ideas on how to make it?

i am trying to do the following
sumg += studentG
is it correct or what? i appreciate any help

EDIT: i just wanted to add, i am getting this error
error C2597: illegal reference to non-static member 'Student::studentG'
so as you can see i can't do it as i was trying to :(
Last edited on
i have created 3 objects for this class


The problem with this is that your static function has no way to know how many objects there are, nor any way to know what those objects' studentG values are.

Whatever code that does the summing will need to know (and have access to) all the student objects whose grades you want to sum. This probably will not be a static member or your class -- not unless you want to pass all objects as a parameter to that function.

Example, something like this will work:

1
2
3
4
5
6
7
8
Student studs[3];  // 3 objects

 // set all their studentG's here

// sum them:
int sum = 0;
for(int i = 0; i < 3; ++i) // loop over each student
  sum += studs[i].studentG;  // now we can use studentG because we know what object we want to access. 
but the problem is that i have that function inside the public in the class,
not in the main function, and it must be static and i can only create the objects in main
The only other way to make that work would be to have your student class keep track of all the objects that have been created (like make a std::list -- whenever an object is constructed, add a pointer to it to that list, and whenever an object is destroyed, remove that pointer). If that list is static, then your static function can use it to iterate over all the created objects so it can sum their data.

But that's overkill.

Really, this is more of a design issue. You're trying to have a section of code that doesn't know how many objects there are sum data from all existing objects. That just isn't going to work. In order for it to sum them, it needs to have direct access to the objects somehow. The only way that can happen is one of the below:

1) Pass all objects to the function
2) Make and maintain a static list as described above
3) make all student objects globally accessable (ewww!!!)


IMO none of those are good solutions because the problem itself is flawed.
i just got an idea out of your first suggestions
how about i make for that function 3 parameters where i will pass the three
'studentG' so this way it will be able to calculate the sum

is it a bad idea?
if you have a static function in a class, all it is is a normal function inside a class, allowing it access to the private/protected members

if you must have a static function that adds objects together, do it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class a
{
public:
    a () {};
        //default constructor
    
    a (int newA, int newB)
        //overloaded constructor
    {
        a = newA;
        b = newB;
    }

    static a AddObjects (a* obOne, a* obTwo)
    {
        return new a (obOne->a + obTwo->a, obOne->b + obTwo->b);
    }

protected:

private:
    int a,
        b;
};


basically it has to create a new object with the given values, or you could overload it with a non-static function that sets the value of "this" to the sums and returns void
Last edited on
Every member function gets a secret argument which is the this pointer of every object so they know with which object the are working.
Static fucntions (even though they can called by any object ) belong to the class and not in any individual object , so they dont get the this pointer as an argument.
Because of that they cant access private data of any object because they dont know which object.But they have access to static data members and other static functions because they also belong to the class.

So you have to find another way to sum your StudentG variable.Because a static function does not have access to this variable.
i am getting this error, does have anything to do with this subject?
i tried to manually pass the values of the studentG values in main, but i get these errors
1
2
3
4
Student.obj : error LNK2001: unresolved external symbol "private: static double Student::numberOfStudents" (?numberOfStudents@Student@@0NA)
Student.obj : error LNK2001: unresolved external symbol "private: static double Student::averageGPA" (?averageGPA@Student@@0NA)
Student.obj : error LNK2001: unresolved external symbol "private: static double Student::sumGPA" (?sumGPA@Student@@0NA)
main.obj : error LNK2019: unresolved external symbol "public: __thiscall Student::~Student(void)" (??1Student@@QAE@XZ) referenced in function _main
bump please look into my error, i need urgent help
nvm guys i found out my error,
i forgot the static initialization in the .cpp file
Topic archived. No new replies allowed.