c++中的调试错误
Debugging errors in c++
我正在编写一个程序,该程序根据用户的输入打印字数、字符数和行数。但我一直在犯这些我完全不知道的错误。我想知道是否有人能帮上忙。**我已经从以前的错误中更改了它,并且仍然收到错误。对不起,我是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
函数的局部变量。您不能在其他功能(cc
、wc
等)中使用它
将其声明为全局变量,或将其作为参数传递给cc
、wc
和其他函数。
您还有其他错误。首先,您需要在wc
、lc
和cc
函数中各有一个局部变量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);
相关文章:
- 无法使函数公开。获取:"LNK2005"错误。如何调试链接器错误
- C++为什么我的编译器成功了,但我的计算机给出了调试错误?
- 如何在 Python C++ 混合库中调试非确定性分段错误?
- 分段错误 .AnsiStringToUnicodeString 作为第一个调试错误行.杜比奥斯类等级制度
- _mm256_load_ps调试模式下导致谷歌/基准测试的分段错误
- 构建 Tensorflow 调试时出现错误LNK2019
- 如何使用realsense摄像头调试ROS错误消息
- 调试模板时出现问题.专门针对 Linux GCC 7、GCC 6、GCC 5、GCC 4.9 错误构建失败:模板参数 1
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 当 2 个线程共享同一物理内核时,具有错误共享的易失性增量在发布中的运行速度比在调试中慢
- 在 Windows 10 下使用 GTest 的 CMake - 致命错误 LNK1104:无法打开文件"gtest.lib",但存在调试"gtestd.lib"
- 全球免费给出一个错误.调试器不解释
- LNK1124错误调试器找不到.exe文件
- 我有视觉工作室错误调试断言失败
- 删除时出现C++内存错误-调试断言失败
- 当检查变量和0是否相等时,给出段错误(调试时)
- c++什么是一个很好的分段错误调试器
- 指针错误调试帮助
- 引发错误 调试断言失败 _BLOCK_TYPE_IS_VALID 使用删除时
- HugeInt错误.调试断言失败