繁体   English   中英

又一个:将结构的向量传递给函数 - C ++,MinGW

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

我知道这里有很多类似的问题,但我无法解决这个问题,没有同事,所以:

我正在使用MinGW(4.8.x)和Eclipse CDT Kepler。

1)我有我自己的代码并清理它我改变它使用结构的向量 - 一切都很好,除了接收它的函数抱怨无效参数'。

2)我将代码缩减到最小的工作示例,如果我将它全部放在一个文件中它可以工作,但是如果我将我的定义移出到标题(我需要在我的主代码中执行)它突然无法解决结构中的字段...下面的代码用于三个文件配置,header / function / main。

(在我的主要代码中,我使用namespace std - 但这似乎不是问题。另外,在这里有最多工作示例的无关标题,但是在我的主代码中需要它们。)

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;
}   

编辑:

截图 代码截图

编辑2:有趣的更新 - 它在使用GCC的Linux上编译良好。 总比没有好,但我还是想知道出了什么问题,加上我希望我的代码能够跨平台......

编辑3:这越来越奇怪了 - 我刚刚在家用电脑上测试了我的代码(在Linux上编译的完整项目),运行Windows 7,在我的笔记本电脑运行Windows 8时出现问题并且出现问题。 C ++构建的设置完全相同。 两者都运行MinGW 4.8.1 ......两者都运行最新的Eclipse Kepler ......

是的,我知道我还需要测试一些建议。

#ifndef MY_HEADERS
#define MY_HEADERS

应该在你的文件的开头。 由于你不知道编译器将以什么顺序包含头文件,这可能会导致问题...特别是如果你将你的个人头文件包含在多个文件中,那么肯定会让它表现得如此。 另外,请记住,既然您没有提供默认构造函数,而是使用编译器为您提供的构造函数,那么结构中的那些变量很可能不会像您期望的那样初始化为零。

编辑#1您是否正在编译所有内容而不仅仅是主...我只是将您的代码复制到VS中并且它可以工作!

编辑#2尝试定义内联函数而不是单独的实现文件。

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;
}  

编辑#3好了,从屏幕截图来看这绝对是有效的代码。 为了尝试一切; 实现自己的构造函数和结构的复制构造函数。 我知道这可能听起来很愚蠢但也许Eclipse不这么认为。

好的 - 我找到了答案 - 我认为 - 它归结为Eclipse。 - >项目 - > C / C ++索引 - >重建

这解决了这个问题。 实际上,这个问题在早期的Eclipse CDT版本中是已知的: https//bugs.eclipse.org/bugs/show_bug.cgi? id = 348170

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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