2

我有一个正在尝试编译的单一源文件 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> >’

知道有什么问题吗?

4

2 回答 2

4

在你的类声明中

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

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

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

当然,两者必须匹配。

此外,您的代码中似乎有一个using namespace std;声明。编译器无法解析merge对您的merge类构造函数的调用还是对std::merge.

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

不要在代码中的using namespace std; 任何地方添加。std::打字不是很麻烦。

于 2012-10-19T21:02:14.577 回答
3

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

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

于 2012-10-19T20:56:38.663 回答