简体   繁体   中英

Yet Another: Passing Vector of Structs to Function - C++, MinGW

I am aware there are numerous similar queries on here, however I haven't been able to resolve this, not has a colleague, so:

I am using MinGW (4.8.x) with Eclipse CDT Kepler.

1) I have my own code and to clean it up I changed it to use a vector of structs - all is fine, except that the function that receives it complains about Invalid Argument'.

2) I reduced my code down to a minimum working example, if I place it all in a single file it works, however if I move out my definitions to the header (which I need to do in my main code) it suddenly cannot resolve the fields in the struct... The code below is for a three file configuration, header/function/main.

(In my main code I use namespace std - but that doesn't seem to be the problem. Also, there are extraneous headers for a minimum working example in this, however they are needed in my main code.)

myheaders.h

/*************************/
/****** myheaders.h ******/
/*************************/

/**-- Header Files --**/

// File Streams and IO
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <fstream>

// For strtod -> string to double
#include <stdlib.h>

// Math Operations
//#include <math.h>
#include <cmath>

// To get the CPU time
#include <time.h>

// For Vectors
#include <vector>

// For strings, C strings right now...
#include <cstring>

// Needed globally for the function definitions
// using namespace std;

#ifndef MY_HEADERS
#define MY_HEADERS

struct SpeciesLoss {
    int ReactionID;
    int SpeciesID;
    double coefficient;
};

std::vector< double > SpeciesLossRate(std::vector<SpeciesLoss> , int, const std::vector< double > & );

#endif

function.cpp

/*************************/
/****** function.cpp *****/
/*************************/

#include "myheaders.h"

std::vector< double > SpeciesLossRate(
    std::vector< SpeciesLoss > SpeciesLossList,
    int Number_Species,
    const std::vector< double >& Combined_Rates
    )
{
    std::vector< double > temp_species_loss;
    temp_species_loss.resize(1);

    temp_species_loss[0]=SpeciesLossList[0].ReactionID;

    return temp_species_loss;
}    

main.cpp

/*************************/
/******** main.cpp *******/
/*************************/

#include "myheaders.h"

std::vector< SpeciesLoss > SpeciesLossAll; // New vector for recording species loss, uses a vector of structs

int main(int argc, char* argv[])
{
    std::vector< double > Rates;
    Rates.push_back(1);

    SpeciesLossAll.push_back(SpeciesLoss());
    SpeciesLossAll[0].ReactionID = 0;
    SpeciesLossAll[0].SpeciesID = 0;
    SpeciesLossAll[0].coefficient = 0;

    std::vector< double > SpeciesConcentrationChange = SpeciesLossRate(SpeciesLossAll,1, Rates);

    return 0;
}   

Edit:

Screenshot 代码截图

Edit 2: And interesting update - it compiles fine on Linux with GCC. Better than nothing, but I still want to know what is going wrong, plus I'd like my code to be cross platform...

Edit 3: This is more and more bizarre - I just tested my code (the full project that compiles on Linux) on my home PC which runs Windows 7 where it builds fine while my laptop runs Windows 8 and the problem occurs. The Settings for the C++ build are absolutely identical. Both run MinGW 4.8.1... Both run the latest Eclipse Kepler...

And yes, I am aware that I need to test some suggestions still.

#ifndef MY_HEADERS
#define MY_HEADERS

Should be at the beginning of your file. Since you have no idea in what order the compiler is going to include headers this might be causing a problem... Especially if you are including your personal header in multiple files, wich will definitely make it behave like so. Also, keep in mind that since you are not providing a default constructor but rather using the one the compiler provides for you, those variables inside the struct will most likely not be initialized to zero as you expect them.

EDIT#1 Are you compiling everything NOT just main... I just copied your code into VS and it works!

EDIT#2 Try defining the function inline instead of a separate implementation file.

static std::vector< double > SpeciesLossRate(
    std::vector< SpeciesLoss > SpeciesLossList,
    int Number_Species,
    const std::vector< double >& Combined_Rates
    )
{
    std::vector< double > temp_species_loss;
    temp_species_loss.resize(1);

    temp_species_loss[0]=SpeciesLossList[0].ReactionID;

    return temp_species_loss;
}  

EDIT#3 Ok, from the screen-shot this is definitely valid code. For sake of trying everything; implement your own constructor and copy constructor of the struct. I know this might sound silly but maybe Eclipse doesn't think so.

OK - I have found the answer - I think - and it boils down to Eclipse. -> Project -> C/C++ Index -> Rebuild

This resolves the issue. In fact, this problem is known on earlier Eclipse CDT versions: https://bugs.eclipse.org/bugs/show_bug.cgi?id=348170

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