【发布时间】:2021-12-28 13:29:04
【问题描述】:
我提交了一个满足所有条件的作业,除了我不允许使用全局变量,除非它们是常量,并且我将我的向量设置为全局变量。
我现在发现在我的头文件以及我的类函数文件和我的主类文件中使用我的向量是不可能的。
我有一个主类和一个派生类,向量项属于派生类。现在,我已将矢量定义放在主要类的标题中:
vector<Stock> arrStock;
但是当尝试将对象从输入文件添加到向量中并随后对其执行操作时,我可以在 classes 函数类中设置函数,或者调用我在 main 中设置的函数类(如果我将函数设置为静态)。
我做错了什么?目前,按照代码的设置方式,我在主类中调用的所有函数都出现错误 E0245“非静态成员引用必须相对于特定对象”。可能值得注意的是,当我将向量设置为全局变量时,这些函数被定义为静态并且它们工作得很好,所以我对它们需要是静态的并且我的问题在于应用程序的事实持开放态度代替 Assignment2_classes.cpp 文件中的数组。
请看下面我的代码sn-ps:
作业2.h:
class Product
{
private:
string title, surname;
long long int isbn;
double wholesalePrice;
public:
string getTitle();
string getSurname();
long long int getIsbn();
double getWholesalePrice();
void setTitle(string);
void setSurname(string);
void setIsbn(long long int);
void setWholesalePrice(double);
Product();
~Product();
Product(string, string, long long int, double);
void importProducts();
void newProduct();
void delProduct();
void runReport();
void checkStock();
void clrStock();
vector<Stock> arrStock;
};
// creating the derived class Stock
class Stock :public Product
{
public:
double retailPrice;
char bookFormat;
int stockLevel;
double getRetailPrice();
char getBookFormat();
int getStockLevel();
void setRetailPrice(double);
void setBookFormat(char);
void setStockLevel(int);
Stock();
~Stock();
Stock(string, int, char, string, double, long long int, double);
void setStockInfo(long long int, string, string, double, int, double, char);
};
Assignment2_classes.cpp:
void Product::importProducts()
{
// code adapted from: https://stackoverflow.com/questions/16878259/how-to-read-in-a-set-of-values-from-a-text-file-then-go-to-the-next-line-and-do
// telling the function which input file it is reading from
ifstream productsFile("products_v5.txt");
// creating local variables
Stock aStock;
double tempRetailPrice = 0;
string undsc = "_";
string space = " ";
size_t position;
std::cout << "Importing books...\n";
// reading the books into an array
while (productsFile >> aStock.title >> aStock.stockLevel >> aStock.bookFormat >> aStock.surname >> aStock.wholesalePrice >> aStock.isbn)
{
// replacing the underscores in the title names with spaces so the output looks better
// code adapted from https://www.educba.com/c-plus-plus-replace/
while ((position = aStock.title.find(undsc)) != string::npos)
{
aStock.title.replace(position, 1, space);
}
// calculating the retail prices of the books depending on their format
switch (aStock.bookFormat)
{
case 'a': tempRetailPrice = aStock.wholesalePrice * 1.43;
break;
case 'e': tempRetailPrice = aStock.wholesalePrice * 1.08;
break;
case 'h': tempRetailPrice = aStock.wholesalePrice * 1.45;
break;
case 's': tempRetailPrice = aStock.wholesalePrice * 1.27;
break;
}
aStock.setRetailPrice(tempRetailPrice);
arrStock.push_back(aStock);
}
// letting the user know how many books have been added and how many books are currently in the array
std::cout << "\n" << to_string(arrStock.size()) << " books have been added.\n";
std::cout << "\nBiblioden Books currently has " << to_string(arrStock.size()) << " different books.\n";
}
Assignment2_main.cpp:
int main()
{
char createBook;
char deleteBook;
char viewReport;
char checkOrders;
// creating the heading of the output
cout << "-----------------------------------------------------------------------------------------\n" << " Biblioden Systems\n" << "-----------------------------------------------------------------------------------------\n";
ifstream productsFile("products_v5.txt");
// checking whether the file is open and gracefully exiting if it can't be opened
if (!productsFile.is_open())
{
cout << "\nCannot open file.\n";
return 1;
}
Product::importProducts();
//closing the file
productsFile.close();
cout << "\nWould you like to enter a new book? (Y/N): ";
cin >> createBook;
if (createBook == 'Y' || createBook == 'y')
{
Product::newProduct();
}
cout << "\nWould you like to delete a book? (Y/N) ";
cin >> deleteBook;
if (deleteBook == 'Y' || deleteBook == 'y')
{
Product::delProduct();
}
cout << "\nWould you like to view a report? (Y/N) ";
cin >> viewReport;
if (viewReport == 'Y' || viewReport == 'y')
{
ofstream report("report.txt");
// checking whether the file is open and gracefully exiting if it can't be opened
if (!report.is_open())
{
cout << "\nCannot open file.\n";
return 1;
}
else
{
Product::runReport();
// closing the file
report.close();
}
}
cout << "\nWould you like to check the order list against the stock list? (Y/N) ";
cin >> checkOrders;
if (checkOrders == 'Y' || checkOrders == 'y')
{
ifstream ordersFile("orders_v5.txt");
// checking whether the file is open and gracefully exiting if it can't be opened
if (!ordersFile.is_open())
{
cout << "\nCannot open file.\n";
return 1;
}
else
{
Product::checkStock();
// closing the file
ordersFile.close();
}
}
// clearing out the array once the user is finished with it
Product::clrStock();
return 0;
}
【问题讨论】:
-
调用
MyClass::myFunc();要求myFunc()是static的MyClass成员,这里不是这种情况。 -
嗨@Fareanor,我知道这是问题所在,但我该如何解决呢?函数是否应该是 static ,如果是这样,我如何使用向量修复 Assignment2_classes.cpp 文件中抛出的错误(相同的 E0245 错误刚刚应用于函数中的向量调用)?或者它们不应该是静态的,如果是,我该如何修复 Assignment2_main.cpp 文件中的错误?
-
当然不是,我想你会感兴趣阅读一本好的 C++ 书籍以更好地理解
static的作用。在您的情况下,您需要一个适当的类实例来处理(请参阅我的答案)。 -
除非您希望类的所有可能实例共享相同的唯一成员(或者如果您根本不想要一个实例,因此不需要一个唯一向量等......),那么您可以你所有的成员
static,它应该可以工作。 -
@Fareanor,我确实认为我希望所有可能的向量实例共享相同的唯一成员......但是你在句子的前半部分提到的是什么?
标签: c++ static stdvector derived-class