簡體   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