小编Dra*_*law的帖子

使用fstream对象作为函数参数

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

void vowel(fstream a){
    char ch;
    int ctr = 0;
    while(!a.eof()){
        a.get(ch);
        if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){
            cout << ch;
            ctr++;
        }
    }
    cout << "Number of Vowels: " << ctr;
}

main(){
    fstream a;
    a.open("temp.txt", ios::in);
    vowel(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这个简单的程序中,我试着计算temp.txt文件中的元音数量.但是,我收到错误:

在函数fstream :: fstream(fstream&)中无法访问ios :: ios(ios&)

而是在函数本身中打开文件来完成工作.为什么会这样?非常感谢

注意:

如何通过函数参数使用fstream(特别是ofstream)

在这里它说,它应该按我尝试的方式工作.

干草堆

c++ fstream function

10
推荐指数
1
解决办法
2万
查看次数

在成员函数中调用类构造函数

这是一个程序,我试图multi::multi(int, int)在函数中调用类构造函数void multi::multiply().输出是

三十

三十

而不是预期的

三十

25

为什么?

#include <iostream.h>

class multi{
    private:
        int a;
        int b;
    public:
        multi(int m, int n){
            a = m;
            b = n;
        }

        void multiply(){
            cout << "\n\n" << a*b;
            multi (5, 5);
            cout << "\n" << a*b;
        }
};

main(){
    multi x(5,6);

    x.multiply();
return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ constructor function

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×2

function ×2

constructor ×1

fstream ×1