当前位置: 动力学知识库 > 问答 > 编程问答 >

C++ compilation error: reference to constructor is ambiguous

问题描述:

I have a single source file C++ program I'm trying to compile.

The header file looks like:

class merge {

public:

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

virtual ~merge();

};

And the source file looks like:

int main(int argc, char* argv[])

{

merge mrg(argc,argv);

return 0;

}

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

//code

}

Trying to compile with:

g++ merge.cpp -o merge

I get:

 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> >’

Any idea what's wrong?

网友答案:

In the class declaration you have

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

But when you provide a definition for the constructor you have

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

Of course, the two must match.

Also, it seems you have a using namespace std; statement in your code somewhere. The compiler cannot resolve whether the calls to merge are to your merge class constructor or to std::merge.

The lesson to take away from this (other than fix your constructor):

DO NOT add using namespace std; anywhere in your code. std:: is not very cumbersome to type.

网友答案:

The signature of merge's constructor does not match its definition.

In your class declaration, you defined merge::merge to take in two parameters, one int and the other char*. In the definition for merge::merge, however, you define it as taking in only one parameter of type map<string,string>&

分享给朋友:
您可能感兴趣的文章:
随机阅读: