简体   繁体   中英

Can't figure out how to pass struct array to function

I have a header file, main cpp file and functions file and am trying to pass a stuct array from the main function to the functions file and can't figure out the syntax. I have stripped the code done to show what I am doing without showing hundreds of lines of code.

I have tried every variation i know of to pass the structure array and it won't compile; I have read everything I could find on it and can't find the solution; I know it is just a syntax thing I'm not figuring out.

header.h file

#ifndef myHeader_h
#define myHeader_h


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>


using namespace std;


struct MyStruct
{
    float var1;
    float var2;
    float var3;
};

struct MyStruct2
{
    string var4;
    string var5;
    bool var6;
    MyStruct struct1;
};


int enterFunction(int maxNumber, int & currentNumber, structList);

#endif

Main Program

#include "myHeader.h"

int main()
{
    int currentNumber = 0, maxNumber = 0;

    MyStruct *structList;
    structList = new MyStuct[maxNumber];

    enterFunction(currentNumber, maxNumber, structList);
    return 0;
}

Functions file

#include "myHeader.h"

int enterFunction(int maxNumber, int & currentNumber, structList)
{
// DO MORE STUFF HERE
}

Instead of error-prone manual memory management you should use RAII to manage resources. C++ has std::vector for this and it makes life a lot easier. I guess that the function enterFunction should read a number of structs from a file or the keyboard, so the task would be to create a list of input values. You can pass a reference to the vector:

int enterFunction(int maxNumber, int & currentNumber, std::vector<MyStruct2>& structlist);

However, I would change the signature to return the vector with the input values. This makes the function easier to comprehend and reason about:

std::vector<MyStruct2> enterFunction(int maxNumber) {
    std::vector<MyStruct2> inputs;
    // let's assume that >> is overloaded for MyStruct2
    while (inputs.size() < maxNumber) {
        MyStruct2 x;
        cin >> x;
        inputs.push_back( std::move(x) );
    }

    return inputs;
}

Please note that this will not copy the vector as the return value because it uses named return value optimization.

In your header.h file, instead of

int enterFunction(int maxNumber, int & currentNumber, structList);

you need

int enterFunction(int maxNumber, int & currentNumber, MyStruct * structList);

And in your Functions file, likewise you need

int enterFunction(int maxNumber, int & currentNumber, MyStruct * structList)

And that's it!

By the way, it is dangerous to put using namespace std (or any using directive) in a header file like that. It might break other people's code if they have to include your header. Better to put it in your C file. (And even if nobody is ever going to include your header, it's a bad habit to get into.) See this stackoverflow question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM