2011-11-03 54 views
-1

我在初始化頭文件中的結構向量時遇到問題。結構不匹配的C++向量

bins.h

#ifndef BINS_H 
#define BINS_H 

#include <vector>; 

using namespace std; 

struct bin 
{ 
//... 

    bin() { 
     //... 
     } 

}; 

class Bins { 

public: 

Bins(); 
vector<bin> getBins(); 
bin getBin(int i); 
//... 

private: 

vector<bin> bins; 

}; 

#endif 

錯誤

(This is line: vector<bin> getBins();) 
C:\...\bins.h:34: error: type/value mismatch at argument 1 in template parameter list for 'template<class _Tp, class _Alloc> class std::vector' 
C:\...\bins.h:34: error: expected a type, got 'bin' 
C:\...\bins.h:34: error: template argument 2 is invalid 

(This is line: bin getBin(int i);) 
C:\...\bins.h:35: error: 'bin' does not name a type 

(This is line: vector<bin> bins;) 
C:\...\bins.h:43: error: expected a type, got 'bin' 
C:\...\bins.h:43: error: template argument 2 is invalid 

我沒有與C++太多的經驗;不過,我之前用這種方法使用過載體,沒有任何問題。任何建議表示讚賞。

編輯:這與代碼的所有其他部分註釋掉。

+5

你不需要使用命名空間std一個預處理指令 – Marlon

+3

'後分號;在'頭文件通常被認爲是不好的,因爲它在每個使用你的頭文件的人身上強迫它們不知道。 – Flexo

+2

問題出在你的程序中你沒有發佈的部分。請複製您的程序,並刪除與此問題無關的每一行。請將生成的程序複製粘貼到您的問題中。查看http://sscce.org是爲什麼發佈一個類似程序的程序不起作用。 –

回答

3

幾個問題與您的上述代碼。

沒有在這裏需要分號包括標題

#include <vector>; 

時,不要把你的頭文件使用指令。它會更好,如果你完全限定其使用,而不是

using namespace std;

你得到的另一個與命名衝突與斌 - 顯然標準庫已經使用它的東西。您可以附上斌在自己的命名空間或將作爲您的垃圾箱類的一部分它可能屬於:

#include <vector> 

// dumping std namespace inside your header is bad 
// don't do this! 
// using namespace std; 

class Bins 
{ 
public: 
    struct bin 
    { 
    bin() {} 
    }; 

    Bins(); 
    std::vector<bin> getBins(); 
    bin getBin(int i); 

private: 
    std::vector<bin> bins; 
}; 
+0

就是這樣,謝謝。 – Pax

1

水晶球回答:你有這些行之一的Bins聲明:

int bin; 

bin bin; 

These是你可能會得到錯誤信息的排序。

+0

我在垃圾箱裏沒有這樣的聲明。在嘗試編譯之前,我還確保註釋掉代碼的其他部分。 – Pax