2017-07-18 5 views
-1

こんにちは、structをパラメータとして使用している問題があります。structが "仮パラメータリストの不一致"を引き起こすパラメータとして

typedef struct temps { 
    string name; 
    float max; 
} temps; 

私のような問題もなく私のメインでそれを使用することができます:

temps t; 
t.max = 1.0; 

が、このような関数のシグネチャでそれを使用して:

void printTemps(const temps& t) { 
    cout << t.name << endl << "MAX: " << t.max << endl; 
} 

私の構造体には次のようになります

それは私に次のコンパイラメッセージを与えます:

エラーC2563:構造体の

#include <iostream> 
#include <fstream> 
#include <string> 
#include <windows.h> 
#include <Lmcons.h> 

using namespace std; 

typedef struct temps { 
    string name; 
    float max; 
} temps; 

void printTemps(const temps& t) { 
    cout << t.name << endl << "MAX: " << t.max << endl; 
} 

int main(int argc, char **argv) 
{ 
    temps t; 
    t.max = 1.0; 
    printTemps(t); 
} 

任意のアイデアをいただきました!間違っている:仮パラメータ・リストここで

の不一致はMWEのですか?

+1

C++でtypedef構造体を使用しないでください。コンパイラのエラーメッセージを表示します。 –

+0

どのヘッダとステートメントを使用しましたか?完全な[mcve]を投稿してください。 – wally

+0

'#include 'と 'using namespace std;'を一番上に置き、g ++ 6.3.0の上に正しくコンパイルしました。 – Luke

答えて

-1

あなたはCのtypedef構造体宣言を使用しています。

typedef struct temps { 
    string name; 
    float max; 
} temps; 

//void printTemps(const temps& t) { 
void printTemps(const struct temps& t) { // should match the C idiom 
    cout << t.name << endl << "MAX: " << t.max << endl; 
} 

しかし、C++でプログラミングしているので、C++の構造体宣言方法を使用する必要があります。

struct temps { 
    string name; 
    float max; 
}; 

void printTemps(const temps& t) { 
    cout << t.name << '\n' << "MAX: " << t.max << '\n'; // using \n is more efficient 
}              // since std::endl 
                 // flushes the stream 
+0

OPはtypedefを避けるべきですが、typedefはプログラムに何の変更も加えません。コンパイラが盗聴されたり、OPが実際のコードを投稿していない限り、問題を修正してください。 –

関連する問題