以下程序,用了using namespace std;这条语句,没有问题。但如果不用using namespace std;这条语句,而是采用std:string word;这种形式,如果修改才能通过编译呢?望各位高人指教,谢谢!#include <iostream>
#include <fstream>
#include <string>
using namespace std;int main()
{
ofstream outfile("out_file");
ifstream infile("in_file");if(!infile){
cerr<<"error:unable to open input file!"<<endl;
return -1;
}
if(!outfile){
cerr<<"error:unable to open output file!"<<endl;
return -2;
}string word;
while(infile>>word)
outfile<<word<<' ';return 0;
}

解决方案 »

  1.   

    #include "iostream"
    void main(){ std::cout<<"fanqing";
    }
      

  2.   

    #include <iostream>
    #include <fstream>
    #include <string>int main()
    {
    std::ofstream outfile("out_file");
    std::ifstream infile("in_file");if(!infile){
    std::cerr<<"error:unable to open input file!"<<std::endl;
    return -1;
    }
    if(!outfile){
    std::cerr<<"error:unable to open output file!"<<std::endl;
    return -2;
    }std::string word;
    while(infile>>word)
    outfile<<word<<' ';return 0;
    }
      

  3.   

    哦,原来就是都加std::阿,多谢Avoid兄!
      

  4.   

    晕??不明白你不是说不用using namespace std;吗?
    我举了个最简单的例子(只是提供思路,接下来你照样话葫芦不就可以了)
      

  5.   

    cerr ofstream ifstream string 什么的都是std命名空间的 所以如果没有用using namespace std;
    就要在他们前面都加上std:: 建议看看c++的书。
      

  6.   

    我正看着呢,C++ Primer,那个例子就是这本书上的,没有用using namespace std;这条语句,所以通不过编译。C++我原来学过粗浅,这次想学精些,所以想把这些都搞清楚。多谢fanqing、Avoid、Varg兄的指教!
      

  7.   

    晕,系统提示“贴子回复次数大于跟给分次数”,看样子暂时没法给分了。原来一上手就是public class什么的,像这种基础的问题反而忽略了。