Keenan Z
Keenan Z

Reputation: 141

How to initialise and return an array in a c++ header file?

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()’

Upvotes: 0

Views: 1718

Answers (3)

trojanfoe
trojanfoe

Reputation: 122391

You are not returning the correct type. Try:

vector<double> add() {
   ...
   return myvec;
}

However I would personally pass a reference to the vector within the scope of the caller and return boolean success (optional):

bool add(vector<double> &myvec) {
   ...
   return true;
}

As that avoids copying the vector which could be expensive, unless the C++ compiler is able to use RVO to optimize the copy operation, in which case you can use the former method semantics.

(Thanks to @aryjczyk and @AlexB for pointing this last point out).

Upvotes: 1

user1764961
user1764961

Reputation: 693

  1. parse the line after calling getline().
  2. convert each parsed value to double.
  3. call push_back on vector to add the double.

Also, consider passing a reference to vector.

so, the signature of your function would change to:

int add( std::vector<double> & values )

That way you will avoid unnecessary copying when returning from the function.

Upvotes: 0

sasha.sochka
sasha.sochka

Reputation: 14715

You should change return type of add from int to vector<double>

Upvotes: 2

Related Questions