2017-07-18 91 views
-1

嗨我有一個問題,使用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); 
} 

任何想法,什麼是錯的結構?

+1

不要在C++中使用typedef結構。顯示編譯器錯誤消息。 – 2017-07-18 21:40:18

+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沒有發佈真正的代碼,這可能就是這種情況)。 –