1

运行 Ubuntu 12.04,使用 g++ 编译器并安装了 essential-build。目前试图让一个小程序来工作,代码如下:

#include "Muon.h"
#include "Electron.h"
#include "Photon.h"


#include <string>
using std::string;

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <ofstream>
using std::ofstream;
#include <vector>
using std::vector;

using namespace std;
int main(){
  cout << "Let's create a Particle vector!" << endl;
  Electron* a = new Electron(10.0,20.0,30.0);
  cout << "Electron created" << endl;

  Muon* b = new Muon(30.0,20.0,10.0);
  cout << "Muon created" << endl;

  Photon* c = new Photon(20.0,10.0,30.0);
  cout << "Photon created" << endl;

  vector<Particle*> particlesContainer;
  particlesContainer.push_back(a);
  particlesContainer.push_back(b);
  particlesContainer.push_back(c);
  int size = particlesContainer.size();
  cout << "size is: " << size << endl;

  ofstream file("Muon_Vector.data");
  if (file.is_open()){
    for (int i = 0; i < size; i++){
      file << particlesContainer[0]->getType << endl;
    }
  }
  else {cout << "Unable to open file" << endl;}
  file.close();
  return 0;
}

在尝试编译时,我收到错误:

“intParticles.cpp:15:20:致命错误:ofstream:没有此类文件或目录编译终止。”

这是在输入:“g++ -Wall Particle.cpp intParticles.cpp -o run”之后

任何有关出了什么问题的帮助将不胜感激。我以为ofstream是标准库的一部分?

4

1 回答 1

4

ofsteam 是类但不是头文件。它在“fstream”文件中定义

于 2012-11-24T10:48:37.823 回答