虚拟函数更改变量C++; 我是一个新的堆栈溢出和C++,我希望能够在一个不同的类中使用一个函数,但是为一个新的类改变一个变量。

虚拟函数更改变量C++; 我是一个新的堆栈溢出和C++,我希望能够在一个不同的类中使用一个函数,但是为一个新的类改变一个变量。,c++,C++,因此,在下面的代码中,我在类业务中使用了函数readfile,我有一个名为Class Customer:public Business的新类Customer 我想为每个文件打开不同的文件。在Business文件中是“Business.csv”,在Customer文件中是“Customer.csv”,但当我调用Customer readfile时,它打开了“Business.csv”文件 如何让它打开“Customer.csv”文件?(此外,我还将使用相同的功能打开其他文件! 下面是基类 // B

因此,在下面的代码中,我在类业务中使用了函数
readfile
,我有一个名为
Class Customer:public Business
的新类Customer

我想为每个文件打开不同的文件。在Business文件中是“Business.csv”,在Customer文件中是“Customer.csv”,但当我调用Customer readfile时,它打开了“Business.csv”文件

如何让它打开“Customer.csv”文件?(此外,我还将使用相同的功能打开其他文件! 下面是基类

// Base Class Business
int Business::readfile(string file) // Read the file and store to vector
{
    // create loops to take in necessary information   
    ifstream openfile; // create instance of ofstream function For reading the file
    openfile.open(file); // Open file
    if (!openfile)
    { // check if file opened, if not return with error message
        cout << "Error, cannot open Business.txt file \n";
        return(0);
    }
    // open the file and count the number of lines so we know when to stop and where to add a new line
    while (openfile)
    {
        getline(openfile, buffer, '\n'); //Open the file and read until return
        i++; // count the number of lines
    }
    openfile.close();

    // open the file and store the data into a vector
    openfile.open(file); // Open file
    cout << file;
    for (row = 0; row<(i - 1); row++) // loop to go through rows to i
    { 
        vec.push_back(vector<string>()); //create an empty row in vector
        for (column = 0; column<j; column++) // loop to go through the columns
        {       
            getline(openfile, buffer, '\t'); // read a string until next tab
            vec[row].push_back(buffer); //add data to column of vector  

        }
        cout << setw(30) << setfill(' ') << vec[row][0]; // print data out onscreen
        getline(openfile, buffer, '\n'); // read a string until return
    }
    cout << endl;
    openfile.close();
    return(0);
}
//基类业务
int Business::readfile(string file)//读取文件并存储到vector
{
//创建循环以获取必要的信息
ifstream openfile;//创建用于读取文件的ofstream函数实例
openfile.open(file);//打开文件
如果(!openfile)
{//检查文件是否已打开,如果未打开,返回错误消息

cout调用
Business::openfile()时传递了什么参数
?请尝试创建一个函数并展示给我们,而不是描述您的代码。因为它应该是最小的,所以我们不需要不相关的代码,比如用于实际读取数据的代码。另外,非常重要的是,如果您要重写基类中的函数,请记住在基类中将该函数标记为
虚拟的
。否则,您将不需要使用似乎没有必要读取两次文件,只需执行getline,然后在向量上推回,就可以设置。例如,
while(openfile){getline(openfile,buffer,'\n');vec.push_back(buffer);}
Martin Zhai-你是说业务::readfile(string文件)?如果是这样,那么我在Business和Customer类中有一个字符串变量文件,它们有不同的值,这就是我想要使用的,但它总是只使用基类业务值