2017-07-18 38 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没有发布真正的代码,这可能就是这种情况)。 –