c ++编译器错误无法访问类’std :: basic_ios ‘中声明的私有成员

嘿我得到一个错误我认为与从阅读其他post复制流变量有关,我试图改变

std::ofstream outfil; 

 std::ofstream & outfil; 

但我得到错误

 reference value "outfil" requires an initializer 

代码是:

 #include  #include  #include  #include  #include  #include  #include  #include  std::vector GetValues_n(const std::vector& src, int start, int end) { std::vector ret; for(int i = start; i <= end; ++i) { ret.push_back(std::strtod(src[i].c_str(), nullptr)); } return ret; } std::vector GetValues_c(const std::vector& src, int start, int end) { std::vector ret; for(int i = start; i <= end; ++i) { ret.push_back(std::atoi(src[i].c_str())); } return ret; } std::vector polycentre(const std::vector& x,const std::vector& y,size_t ID) { std::vector C(3, 0); std::vector x1(x.size(),0); std::vector y1(y.size(),0); size_t sizx = x.size(); size_t sizy = y.size(); if(sizy != sizx) { std::cerr << "polycentre inputs not equal length"; } double x0 = x[0]; double y0 = y[0]; for(int aa = 1; aa < sizx; ++aa) { if(x[aa] < x0){x0 = x[aa];} if(y[aa] < y0){y0 = y[aa];} } double A = 0.0; double B = 0.0; for(size_t aa = 0; aa < sizx; ++aa) { x1[aa] = x[aa] - x0; y1[aa] = y[aa] - x0; if(aa != sizx-1) { A = A + (x1[aa]*y1[aa+1] - x1[aa+1]*y1[aa]); B = B + ((x1[aa]+x1[aa+1])*(x1[aa]*y1[aa-1]-x1[aa-1]*y1[aa])); } else if(aa == sizx-1) { A = A + (x1[aa] - y1[aa]); B = B + ((x1[aa]+1)*(x1[aa]*1-1*y1[aa])); } } A = A*0.5; C[0] = ID; C[1] = ((1/6/A)*B)+x0; C[2] = ((1/6/A)*B)+y0; return C; } template  void PrintValues(const std::string& title, std::vector<std::vector>& v, std::ofstream outfil) { if(outfil.is_open()) { outfil << "ID,X,Y,Z \n"; std::cout << title << std::endl; for(size_t line = 0; line < v.size(); ++line) { for(size_t val = 0; val < v[line].size(); ++val) { std::cout << v[line][val] << " "; outfil << v[line][val] << ","; } outfil << "\n"; std::cout << std::endl; } std::cout << std::endl; } } int main(int argc, char* argv[]) { std::ofstream & outfil; if (argc < 2) { std::cerr << argv[0] << " needs to get input file (2dm)" << std::endl; } else if (argc == 3) { outfil.open(argv[2]); } else { outfil.open(std::string(argv[1]) + ".csv"); } std::vector<std::vector> values; std::ifstream fin(argv[1]); for (std::string line; std::getline(fin, line); ) { std::istringstream in(line); values.push_back( std::vector(std::istream_iterator(in), std::istream_iterator())); } std::vector<std::vector> cells; std::vector<std::vector> nodes; for (size_t i = 0; i < values.size(); ++i) { if(values[i][0] == "E3T") { cells.push_back(GetValues_c(values[i], 1, 5)); } else if(values[i][0] == "E4Q") { cells.push_back(GetValues_c(values[i], 1, 6)); } else if(values[i][0] == "ND") { nodes.push_back(GetValues_n(values[i], 1, 4)); } } std::vector<std::vector> cell_centres; for (size_t aa = 0; aa < cells.size(); ++aa) { if(cells[aa].size() == 5) { std::vector xs; xs.at(0) = nodes[cells[aa][1] - 1][1]; xs.at(1) = nodes[cells[aa][2] - 1][1]; xs.at(2) = nodes[cells[aa][3] - 1][1]; std::vector ys; ys.at(0) = nodes[cells[aa][1] - 1][2]; ys.at(1) = nodes[cells[aa][2] - 1][2]; ys.at(2) = nodes[cells[aa][3] - 1][2]; cell_centres.push_back(polycentre(xs,ys,aa+1)); } else if(cells[aa].size() == 6) { std::vector xs; xs.at(0) = nodes[cells[aa][1] - 1][1]; xs.at(1) = nodes[cells[aa][2] - 1][1]; xs.at(2) = nodes[cells[aa][3] - 1][1]; xs.at(3) = nodes[cells[aa][4] - 1][1]; std::vector ys; ys.at(0) = nodes[cells[aa][1] - 1][2]; ys.at(1) = nodes[cells[aa][2] - 1][2]; ys.at(2) = nodes[cells[aa][3] - 1][2]; ys.at(3) = nodes[cells[aa][4] - 1][2]; cell_centres.push_back(polycentre(xs,ys,aa+1)); } } PrintValues("Cell Centres", cell_centres, outfil); //PrintValues("Cells", cells, outfil); //PrintValues("Nodes", nodes, outfil); return 0; } 

而错误是:

 1>------ Build started: Project: cell_centres_v2, Configuration: Debug Win32 ------ 1> main.cpp 1>c:\users\********\documents\visual studio 2010\projects\cell_centres_v2\cell_centres_v2\main.cpp(43): warning C4018: 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(1116): error C2248: 'std::basic_ios::basic_ios' : cannot access private member declared in class 'std::basic_ios' 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits 1> ] 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios::basic_ios' 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits 1> ] 1> This diagnostic occurred in the compiler generated function 'std::basic_ofstream::basic_ofstream(const std::basic_ofstream &)' 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits 1> ] ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

非常感谢。

void PrintValues(const std::string& title, std::vector>& v, std::ofstream outfil)取值为std::ofstream ,即复制你传递的实际参数对它,但fstreams可能不会被复制,因此你得到的有关私有基础的错误。

当您将引用视为一种解决方案时,您就做对了,但是您错过了应该使用它的点。 解决问题的正确方法是在PrintValues 通过引用获取std::ofstream ,为此,只需将其声明更改为以下内容

 Your guess is correct, `void PrintValues(const std::string& title, std::vector>& v, std::ofstream& outfil) 

您需要先了解哪些参考资料。 引用就像是指向另一个变量的永久指针,该变量无法更改为引用其他变量,并且不能为null。 因为它不能为null,所以在不说出引用的情况下声明引用是没有意义的。

你可以说

 std::ofstream outfile_; std::ofstream& outfil = outfile_; 

如果你仍然有兴趣玩参考。

您将outfil声明为对std::ofstream类型的对象的引用,并在main中使用以下行:

 std::ofstream & outfil; 

参考资料必须在申报时初始化。 您的解决方案是将outfil声明为:

 std::ofstream outfil; 

必须在创建引用时初始化引用。 无论如何,似乎没有用于引用。