运行Ubuntu 12.04,使用g ++编译器并安装必要的构建版本。目前试图获得一个小程序来运行代码如下:
#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
是标准库的一部分?
答案 0 :(得分:3)
ofsteam是类,但不是头文件。它在'fstream'文件中定义