gpt4 book ai didi

c++ - 另一个 : Passing Vector of Structs to Function - C++, MinGW

转载 作者:行者123 更新时间:2023-11-28 03:00:33 28 4
gpt4 key购买 nike

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

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

1) 我有自己的代码,为了清理它,我将其更改为使用结构 vector - 一切都很好,除了接收它的函数提示“参数无效”。

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

(在我的主要代码中,我使用 namespace std - 但这似乎不是问题所在。此外,这里有一个最小工作示例的无关 header ,但是它们在我的主要代码。)

我的标题.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

函数.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;
}

主要.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;
}

编辑:

截图 Screenshot of Code

编辑 2:有趣的更新——它在 Linux 上用 GCC 编译得很好。总比没有好,但我仍然想知道出了什么问题,而且我希望我的代码是跨平台的...

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

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

最佳答案

#ifndef MY_HEADERS
#define MY_HEADERS

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

编辑#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 不这么认为。

关于c++ - 另一个 : Passing Vector of Structs to Function - C++, MinGW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20949295/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com