I'm not very good with header files but I want to use a header file to read data from a file and return the data as a vector in the main cpp file.
Here is my readposcar.h file:
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int add(void) {
double a1x, a1y, a1z, a2x, a2y, a2z, a3x, a3y, a3z; // I want all this stuff in vector form
int i;
double scale;
string line; stringstream dum;
ifstream poscar ("POSCAR");
for (i=1; i<=5; i++) {
getline(poscar,line);
if (i==2) {stringstream dum(line); dum >> scale;}
if (i==3) {stringstream dum(line); dum >> a1x >> a1y >> a1z;}
if (i==4) {stringstream dum(line); dum >> a2x >> a2y >> a2z;}
if (i==5) {stringstream dum(line); dum >> a3x >> a3y >> a3z;}
}
vector<double> myvec(3);
myvec[0] = a1x;
myvec[1] = a1y;
myvec[2] = a1z;
return myvec;
}
Here is my .cpp file:
#include <iostream>
#include <fstream>
#include "readposcar.h"
using namespace std;
int main(void) {
int nbasis = 2;
int nkpts = 10;
vector<double> myvec2(3);
myvec2 = add();
cout << "No. of k-points: " << nkpts << endl;
return 0;
}
This obviously does not work. Can someone please advise on what's wrong and what I need to do to make it work? I can only get it to work if I do return say myvec[2] in the .h file but not the entire array.
I don't mind having it as an array if vectors don't work. Is it perhaps possible to just initialise the array in the header file as a sort of global array and then simply call it in the .cpp file?
Here are the errors I get:
In file included from main.cpp:4:0:
readposcar.h: In function ‘int add()’:
readposcar.h:27:9: error: cannot convert ‘std::vector<double>’ to ‘int’ in return
main.cpp: In function ‘int main()’:
main.cpp:12:15: error: no match for ‘operator=’ in ‘myvec2 = add()’