2011-11-03 74 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