0

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()’
2
  • This worked for me. Thanks for the quick replies everyone!. std::vector<double> add(void) { ... return myvec; } Commented Jun 29, 2013 at 12:27
  • You might live to regret putting "using namespace std;" in a header Commented Jun 29, 2013 at 13:09

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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).

3 Comments

compiler will elide the copy, it's called RVO. and passing output arguments is ugly, avoid it when you can
No, no, no, no. If you are using a compiler that's not over 10 years old, you should absolutely return by value. It will either RVO or a move constructor.
Thanks guys; I've edited my answer to mention the use of RVO.
0
  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.

3 Comments

Nope. Go ahead and return by value.
@aschelper, like it or not, it's more efficient to use the reference than returning a vector by value. You cannot be 100% sure that compiler will perform the RVO. With the reference there is no doubt what will happen.
With C++11, you can be 100% sure that it will not call a copy constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.