C ++编译错误:对构造函数的引用是不明确的

时间:2012-10-19 20:55:15

标签: c++ compilation

我有一个单一的源文件C ++程序我试图编译。

头文件如下所示:

class merge {
public:
explicit merge(int argc, char* argv[]);
virtual ~merge();
};

源文件如下:

int main(int argc, char* argv[])
{
merge mrg(argc,argv);
return 0;
}

merge::merge(map<string,string>& params) {
    //code
}

尝试编译: g ++ merge.cpp -o merge

我明白了:

 merge.cpp: In function ‘int main(int, char**)’:
    merge.cpp:10: error: reference to ‘merge’ is ambiguous
    merge.h:12: error: candidates are: class merge
    /usr/lib/gcc/x86_64-redhat-            linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:3180: error: template<class     _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>     _OutputIterator std::merge(_InputIterator1, _InputIterator1, _InputIterator2,     _InputIterator2, _OutputIterator, _Compare)
    /usr/lib/gcc/x86_64-redhat-    linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:3122: error: template<class     _InputIterator1, class _InputIterator2, class _OutputIterator> _OutputIterator     std::merge(_InputIterator1, _InputIterator1, _InputIterator2, _InputIterator2,     _OutputIterator)
    merge.cpp:10: error: reference to ‘merge’ is ambiguous
    merge.h:12: error: candidates are: class merge
    /usr/lib/gcc/x86_64-redhat-    linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:3180: error: template<class     _InputIterator1, class _InputIterator2, class _OutputIterator, class _Compare>     _OutputIterator std::merge(_InputIterator1, _InputIterator1, _InputIterator2,     _InputIterator2, _OutputIterator, _Compare)
    /usr/lib/gcc/x86_64-redhat-    linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:3122: error: template<class     _InputIterator1, class _InputIterator2, class _OutputIterator> _OutputIterator     std::merge(_InputIterator1, _InputIterator1, _InputIterator2, _InputIterator2,     _OutputIterator)
    merge.cpp:10: error: expected `;' before ‘mrg’
    merge.cpp: At global scope:
    merge.cpp:14: error: prototype for ‘merge::merge(std::map<std::basic_string<char,     std::char_traits<char>, std::allocator<char> >, std::basic_string<char,     std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char,     std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const     std::basic_string<char, std::char_traits<char>, std::allocator<char> >,     std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)’ does not     match any in class ‘merge’
    merge.h:12: error: candidates are: merge::merge(const merge&)
    merge.h:14: error:                 merge::merge(int, char**)
    merge.cpp: In constructor ‘merge::merge(std::map<std::basic_string<char,     std::char_traits<char>, std::allocator<char> >, std::basic_string<char,     std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char,     std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const     std::basic_string<char, std::char_traits<char>, std::allocator<char> >,     std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)’:
    merge.cpp:15: error: declaration of ‘std::map<std::basic_string<char,     std::char_traits<char>, std::allocator<char> >, std::basic_string<char,     std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char,     std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const     std::basic_string<char, std::char_traits<char>, std::allocator<char> >,     std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > params’     shadows a parameter
    merge.cpp:16: error: ‘argc’ was not declared in this scope
    merge.cpp:16: error: ‘argv’ was not declared in this scope
    /usr/lib/gcc/x86_64-redhat-    linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_iterator_base_types.h: At global scope:
    /usr/lib/gcc/x86_64-redhat-    linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_iterator_base_types.h: In instantiation     of ‘std::iterator_traits<std::basic_string<char, std::char_traits<char>,     std::allocator<char> > >’:
    /usr/lib/gcc/x86_64-redhat-    linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:803:   instantiated from ‘void     std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator,     __false_type) [with _InputIterator = std::basic_string<char, std::char_traits<char>,     std::allocator<char> >, _Tp = int, _Alloc = std::allocator<int>]’
    /usr/lib/gcc/x86_64-redhat-    linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:262:   instantiated from     ‘std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const _Alloc&) [with     _InputIterator = std::basic_string<char, std::char_traits<char>, std::allocator<char> >,     _Tp = int, _Alloc = std::allocator<int>]’
    merge.cpp:86:   instantiated from here
    /usr/lib/gcc/x86_64-redhat-    linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_iterator_base_types.h:129: error: no     type named ‘iterator_category’ in ‘struct std::basic_string<char, std::char_traits<char>,     std::allocator<char> >’

知道什么是错的吗?

2 个答案:

答案 0 :(得分:4)

在课堂宣言中

explicit merge(int argc, char* argv[]);

但是当您为构造函数提供定义时

merge::merge(map<string,string>& params) { /* ... */ }

当然,两者必须匹配。

此外,您的代码中似乎有using namespace std;语句。编译器无法解析对merge类构造函数或std::mergemerge调用。

从中学到的教训(除了修复你的构造函数):

  

请勿在代码中的任何位置添加using namespace std; 。键入std::并不是非常麻烦。

答案 1 :(得分:3)

merge的构造函数的签名与其定义不匹配。

在您的班级声明中,您定义了merge::merge以接受两个参数,一个int,另一个char*。但是,在merge::merge的定义中,您将其定义为只接受map<string,string>&类型的一个参数