簡體   English   中英

從函數中返回C ++中的結構

[英]Returning Structures in C++ from functions

我試圖從函數返回一個結構,並遇到數據類型的問題。 我不斷得到編譯器錯誤“從”系數“到”int“的合適轉換函數存在”return coeff;“”來自下面的代碼。 我見過很多人遇到這些問題,因為他們在函數中定義了結構,但是我的定義是從一開始就定義的。 任何反饋都會很棒。

#include <iostream> //initialise the code familly used in most commands
using namespace std; //initialise the coding name space

struct coefficients {
    float A;
    float B;
    float C;
} coeff;

int matrix_multiplication(int r1,int r2,int r3,int r4,int r5) //matrix  multiplication between the readings taken and the regression matrix
{
float a1=3.2, b1=-2.8, c1=-0.8, d1=2.2, e1=-0.8; //teach the variables a1-a5   x a1 - d1 the values of the regression matrix
float a2=-3.0, b2=4.3, c2=0.9, d2=-3.4, e2=1.3;
float a3=0.9, b3=-1.6, c3=-0.1, d3=2.2, e3=-0.8;

coeff.A = (a1*r1)+(b1*r2)+(c1*r3)+(d1*r4)+(e1*r5);
coeff.B = (a2*r1)+(b2*r2)+(c2*r3)+(d2*r4)+(e2*r5);
coeff.C = (a3*r1)+(b3*r2)+(c3*r3)+(d3*r4)+(e3*r5);
return coeff;
}

更改

int matrix_multiplication(int r1,int r2,int r3,int r4,int r5)

coefficients matrix_multiplication(int r1,int r2,int r3,int r4,int r5)
//^^^^^^^^^^ <- You are returning a coefficients object, not an int.

因為return coeff返回一個coefficients對象,而不是一個int

此外,嘗試使用數組來簡化代碼。

您的matrix_multiplication函數被告知返回int值,但您正在嘗試返回另一種類型。 應該像這樣:

coefficients matrix_multiplication(int r1,int r2,int r3,int r4,int r5) //matrix  multiplication between the readings taken and the regression matrix

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM