// Ive在其他与静态变量及其初始化有关的问题中找到了有用的信息。我找到了解决方法(感觉好像不想像这里建议的那样使用一个单独的文件来解决此问题:Initialize static variables in C++ class?),但这将是解决方案,以便能够将静态变量用作对特定对象执行的操作的累加器实例通过其方法。
很抱歉浪费时间,我认为这个问题不会给社区带来任何价值,应该关闭。
尝试打印两个静态累加器的静态函数mostrarTotal()无效,因为它们定义不正确。
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;
}
答案 0 :(得分:4)
在您的情况下,应调用静态函数,例如ClassName :: functionName():
Vendedor::mostrarTotal();