-2

// Ive found useful information in other questions relating to static variables and their initialization. I found a work around (didnt feel like solving this with a separate file as suggested here: Initialize static variables in C++ class?), but that would be the solution in order to be able to use a static variable as an accumulator for operations performed on specific instances through their methods.

Im sorry for the time wasted, I dont think this question adds any value to the community and should be closed.

Static Function mostrarTotal() which attempts to print two static accumulators doesnt work, because they are incorrectly defined.


class Vendedor {
    private:
        static double sumatot;
        static double comtot;
    public:
        static void mostrarTotal();
};

//Static method to print static variables sumatot and comtot
void Vendedor :: mostrarTotal() {
    cout << "Las ventas totales fueron: " << sumatot << " para una comisión de: " << comtot;
}

int main () {

//This line throws the error
    Vendedor::mostrarTotal();

    return 0;
}

1 Answers1

4

You should call static functions like ClassName::functionName(), in your case :

Vendedor::mostrarTotal();
Roya Ghasemzadeh
  • 673
  • 5
  • 16
  • Now this happens /tmp/ccZ4ZNRb.o: In function `Vendedor::setVentas()': main.cpp:(.text+0xd2): undefined reference to `Vendedor::sumatot' main.cpp:(.text+0xdf): undefined reference to `Vendedor::sumatot' /tmp/ccZ4ZNRb.o: In function `Vendedor::mostrarTotal()': main.cpp:(.text+0x1e0): undefined reference to `Vendedor::comtot' main.cpp:(.text+0x1e7): undefined reference to `Vendedor::sumatot' – Samuel Franco Jun 27 '19 at 06:15
  • Well, you should initialize your static member variables of your class. I dont know the file structure of your project but adding these two lines before your main function should solve your problem: double Vendedor::sumatot = 0; double Vendedor::comtot= 0; – Roya Ghasemzadeh Jun 27 '19 at 06:42