编辑:这已经解决了。请查看我的第二个编码,看看编辑了什么来修复链接器错误问题。如果可能,请关闭此主题。 感谢您的回答/回复/反馈< 3
我正在尝试访问我的私有静态变量,以使用函数getvalue()返回公司总销售额(Year_Sales)。我试图用
触发函数cout << DivisionSale[0].getvalue();
但我收到以下错误......
[链接器错误]未定义引用;私有静态变量
这是我对项目的编码
#include <iostream>
using namespace std;
// class declaration
class DivSales // The 6 divisions will use this
{
private:
// Holds the total corporate sales for all divisions
static int Year_Sales;
public:
static int Qtrsale[4]; // Elements for sale figures, 4 quarterly sales
static void AddSales (int,int,int,int); // calculates annual ammount
int Sales(int); // output
static int getvalue() { return Year_Sales; }
};
// This member function takes arguments and is copied into array holding sales data
// Year_Sales contains total of the 4 sales
void DivSales::AddSales(int sale1, int sale2, int sale3, int sale4)
{
// arrays 0 through 3 store the quarterly sales
Qtrsale[0] = sale1;
Qtrsale[1] = sale2;
Qtrsale[2] = sale3;
Qtrsale[3] = sale4;
// private variable access
// combines the 4 arguments
Year_Sales = Year_Sales + sale1 + sale2 + sale3 + sale4;
}
// returns values of quarterly sales for each division
// Used for table formatting in main
int DivSales::Sales(int n)
{
// Qtrsale[n] holds value from input to return for output
// returns sale(n)
int value = Qtrsale[n];
return value;
}
// this function is triggered if user enters a negative quarterly sale value
void error()
{
system("cls");
cout << "You have entered a negative value. Restart program and try again";
cout << "Press any key to restart program\n";
system("pause");
exit(0);
}
// Overload constructer
// Definition of the static member of DivSales class
// for access to the private static variable
int DivSales::Year_Sales = 0;
// Start main function
int main()
{
// insurance for protecting the array storage(s)
const int DS = 6;
DivSales DivisionSale[DS]; // 6 division with each of their own array
int quarter, division; // quarter = 4; division = 6
// This will pass quarter sales to AddSales
for (division = 0; division < 6; division++)
{
// 4 quarters for array storage
int Qrt1, Qrt2, Qrt3, Qrt4;
int Q; // used to check for negative input
// Prompts the user for quarter sales of each division
// divisions 1 - 6
cout << "Enter Sales of Division: " << division + 1 << endl;
// Each quarter is assigned to Q which is used for error checking.
// Error checking occurs when negative value is detected
cout << "Enter Q1 Sales: ";
cin >> Qrt1;
Q = Qrt1;
if (Q < 0)
error();
cout << "Enter Q2 Sales: ";
cin >> Qrt2;
Q = Qrt2;
if (Q < 0)
error();
cout << "Enter Q3 Sales: ";
cin >> Qrt3;
Q = Qrt3;
if (Q < 0)
error();
cout << "Enter Q4 Sales: ";
cin >> Qrt4;
Q = Qrt4;
if (Q < 0)
error();
// passes Qrt1 through Qrt4 to AddSales
DivisionSale[division].AddSales(Qrt1,Qrt2,Qrt3,Qrt4);
}
cout << "----------------------------------------\n";
cout << "\t" << "Q1" << "\t" << "Q2" << "\t" << "Q3" << "\t" << "Q4" << endl;
// table for quarterly sales of each division
for (division = 0; division < 6; division++)
{
cout << "Div " << division + 1;
for (quarter = 0; quarter < 4; quarter++)
{
// calling function Sales 6 times
cout << "\t" << DivisionSale[division].Sales(quarter);
cout << endl;
}
}
cout << "----------------------------------------\n";
cout << "\n Total All Division Sales for the Year: ";
cout << DivisionSale[0].getvalue();
system("pause");
return 0;
}
以下是我需要做的事情:
公司有六个部门,每个部门负责销售到不同的地理位置。设计一个DivSales类,用于保存部门的销售数据,具有以下成员:
•一个包含四个元素的数组,用于保存该部门的四分之三的销售数据
•一个私有静态变量,用于保存全年所有部门的公司总销售额。
•一个成员函数,它接受四个参数,每个参数假定为四分之一的销售额。应将参数的值复制到包含销售数据的数组中。应将四个参数的总和添加到包含年度公司总销售额的静态变量中。
•一个函数,它接受0到3范围内的整数参数。该参数将用作分区季度销售数组的下标。该函数应该返回带有该下标的数组元素的值。
编写一个程序,创建一个包含六个DivSales对象的数组。该程序应要求用户输入每个部门四个季度的销售额。输入数据后,程序应显示一个表格,显示每个季度的部门销售额。然后,该程序应显示该年度的公司总销售额。
输入验证:仅接受季度销售数字的正值。
链接器错误是唯一阻碍我的错误。我很清楚可能会有一些代码优化,但我确实需要修复链接器错误。
编辑:修改了什么是静态而非静态并使用了int DivSales :: Year_Sales
新编码:
#include <iostream>
using namespace std;
// class declartion
class DivSales // The 6 divisions will use this
{
private:
// Holds the total corporate sales for all divisions
static int Year_Sales;
int Qtrsale[4]; // Elements for sale figures, 4 quarterly sales
public:
void AddSales (int,int,int,int); // calculates annual ammount
int Sales(int); // output
int getvalue() { return Year_Sales; }
};
// This member function takes arguments and is copied into array holding sales data
// Year_Sales contains total of the 4 sales
void DivSales::AddSales(int sale1, int sale2, int sale3, int sale4)
{
// arrays 0 through 3 store the quarterly sales
Qtrsale[0] = sale1;
Qtrsale[1] = sale2;
Qtrsale[2] = sale3;
Qtrsale[3] = sale4;
// private variable access
// combines the 4 arguments
Year_Sales = Year_Sales + sale1 + sale2 + sale3 + sale4;
}
// returns values of quarterly sales for each division
// Used for table formatting in main
int DivSales::Sales(int n)
{
// Qtrsale[n] holds value from input to return for output
// returns sale(n)
int value = Qtrsale[n];
return value;
}
// this function is triggered if user enters a negative quarterly sale value
void error()
{
system("cls");
cout << "You have entered a negative value. Restart program and try again";
cout << "Press any key to restart program\n";
system("pause");
exit(0);
}
// Overload constructer
// Definition of the static member of DivSales class
// for access to the private static variable
int DivSales::Year_Sales;
// Start main function
int main()
{
// insurance for protecting the array storage(s)
const int DS = 6;
DivSales DivisionSale[DS]; // 6 division with each of their own array
int quarter, division; // quarter = 4; division = 6
// This will pass quarter sales to AddSales
for (division = 0; division < 6; division++)
{
// 4 quarters for array storage
int Qrt1, Qrt2, Qrt3, Qrt4;
int Q; // used to check for negative input
// Prompts the user for quarter sales of each division
// divisions 1 - 6
cout << "Enter Sales of Division: " << division + 1 << endl;
// Each quarter is assigned to Q which is used for error checking.
// Error checking occurs when negative value is detected
cout << "Enter Q1 Sales: ";
cin >> Qrt1;
Q = Qrt1;
if (Q < 0)
error();
cout << "Enter Q2 Sales: ";
cin >> Qrt2;
Q = Qrt2;
if (Q < 0)
error();
cout << "Enter Q3 Sales: ";
cin >> Qrt3;
Q = Qrt3;
if (Q < 0)
error();
cout << "Enter Q4 Sales: ";
cin >> Qrt4;
Q = Qrt4;
if (Q < 0)
error();
// passes Qrt1 through Qrt4 to AddSales
DivisionSale[division].AddSales(Qrt1,Qrt2,Qrt3,Qrt4);
}
cout << "\n----------------------------------------\n";
cout << "\t" << "Q1" << "\t" << "Q2" << "\t" << "Q3" << "\t" << "Q4" << endl;
// table for quarterly sales of each division
for (division = 0; division < 6; division++)
{
cout << "Div " << division + 1;
for (quarter = 0; quarter < 4; quarter++)
{
// calling function Sales 6 times
cout << "\t$" << DivisionSale[division].Sales(quarter);
}
cout << endl;
}
cout << "----------------------------------------\n";
cout << "\nTotal corporate sales for the year: ";
cout << "$" << DivisionSale[0].getvalue() << endl;
system("pause");
return 0;
}
答案 0 :(得分:0)
您只需要在某处获得变量的定义:
int DivSales::Qtrsale[4];
你在课堂上有一个宣言,但在项目的某个地方也需要有一个定义(在一个地方)。
这必须是一个C ++常见问题解答,但我现在懒得查找它。