c++中的调试错误

Debugging errors in c++

本文关键字:错误 调试 c++      更新时间:2023-10-16

我正在编写一个程序,该程序根据用户的输入打印字数、字符数和行数。但我一直在犯这些我完全不知道的错误。我想知道是否有人能帮上忙。**我已经从以前的错误中更改了它,并且仍然收到错误。对不起,我是C++新手。

我得到的错误是

 filestat.cpp:47: error: ‘line’ was not declared in this scope
 filestat.cpp: In function ‘int wc(std::string)’:
 filestat.cpp:55: error: ‘line’ was not declared in this scope
 filestat.cpp: In function ‘int cc(std::string)’:
 filestat.cpp:67: error: ‘line’ was not declared in this scope

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int lc(string fname);
int wc(string fname);
int cc(string fname);
int main(){
string fname,line,command;
ifstream ifs;
int i;
while(true){
    cout<<"---- Enter a file name : ";
    if(getline(cin,line)){
        if(line.length()== 4 && line.compare("exit")== 0){
            cout<<"Exiting";
            exit(0);
        }else{
            string command = line.substr(0,2);
            fname= line.substr(4, line.length() -5);
                if( ifs.fail()){
                    ifs.open(fname.c_str());
                    cerr<< "File not found" <<fname <<endl;
                    ifs.clear();
                }else{
                    if(command.compare("lc")){
                        lc(fname);
                    }else if (command.compare("wc")){
                        wc(fname);
                    }else  if(command.compare("cc")){           
                                      cc(fname);                    
                    }else
                        cout<<"Command unknown. ";

                }
        }
    }
}
return 0;
}
 int lc(string fname){
 int count;
 while(getline(fname, line)){
    count++;
 }
  cout<<"Number of lines: "<<count ; 
   }
  int wc(string fname){
int count;
while(getline(fname, line)){
    int pos=line.find_first_of("nt ",0);
    while(pos =! string::npos){
        int length=line.length();
        line = line.substr(pos+1, length - pos);
        count++;
    }
  }
cout<< "Number of words: " <<count; 
  }
 int cc(string fname){
int count;
while(getline(fname, line)){
    count = count + line.length();
}
cout<< "Number of words: " <<count;
    }

当我将line设置为全局变量时,我会得到错误:

filestat.cpp:48:错误:无法将参数"1"的"std::string"转换为"char**",以将其转换为"__ssize_t getline(char**,size_t*,FILE*)"

声明line的方式是,它是main函数的局部变量。您不能在其他功能(ccwc等)中使用它

将其声明为全局变量,或将其作为参数传递给ccwc和其他函数。

您还有其他错误。首先,您需要在wclccc函数中各有一个局部变量line

其次,不能使用fname调用getline。它需要一个istream。那么,为什么不将ifs传递到函数中呢?

int wc( ifstream &ifs )
{
    string line;
    int count = 0;
    while(getline(fname, line)){
        int pos=line.find_first_of("nt ",0);
        while(pos =! string::npos){
            int length=line.length();
            line = line.substr(pos+1, length - pos);
            count++;
        }
    }
    cout<< "Number of words: " <<count;
    return count;
}

在上面的文章中,我还初始化了count并返回了它(因为您有一个int返回类型,并且没有返回任何内容)。

您的其他功能也有类似的更改。

顺便说一下,您可能需要查找string::find_first_of函数,并决定是否每次都需要用子字符串替换line。看看第二个参数。

由于您的错误状态,line未在列出的"范围"(即函数)中声明。如果希望这些函数可以访问line,则需要将其设为全局变量(意味着在main之外声明)。

除了在全局范围中声明行之外,还可以。您需要在有问题的函数中从filename创建一个ifstream对象。例如

int cc(string fname){
ifstream f(fname);
int count;
while(getline(f, line)){
    count = count + line.length();
}
f.close();
}

这应该可以,但我建议将函数定义更改为

 int cc(ifstream& f);