将指针传递给C ++中的对象&错误:没有匹配的函数用于调用

时间:2014-03-10 03:27:27

标签: c++

我正在尝试将指针从vector传递到另一个自由浮动函数。这一切都很好,但是,当尝试向此向量添加内容时,我得到一个奇怪的编译错误。

$ g++ PeekClientRMS.cpp
PeekClientRMS.cpp: In function ‘void testNewWord(std::string, std::vector<PeekDeque<StringWrap> >*)’:
PeekClientRMS.cpp:116:26: error: no matching function for call to ‘std::vector<PeekDeque<StringWrap> >::push_back(PeekDeque<StringWrap>*&)’
  chainsp->push_back(newpd);
                          ^
PeekClientRMS.cpp:116:26: note: candidate is:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/vector:64:0,
                 from PeekClientRMS.cpp:13:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_vector.h:901:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = PeekDeque<StringWrap>; _Alloc = std::allocator<PeekDeque<StringWrap> >; std::vector<_Tp, _Alloc>::value_type = PeekDeque<StringWrap>]
       push_back(const value_type& __x)
       ^
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_vector.h:901:7: note:   no known conversion for argument 1 from ‘PeekDeque<StringWrap>*’ to ‘const value_type& {aka const PeekDeque<StringWrap>&}’

问题似乎是通过引入名为StringWrap而不是string的自定义类来解决的。导致此编译错误的代码行是chainsp->push_back(newpd);

void testNewWord(string word, vector<PeekDeque<StringWrap> >* chainsp) {

    PeekDeque<StringWrap>* newpd = new PeekDeque<StringWrap>(100);
    chainsp->push_back(newpd);
}

int main(int argc, char* argv[]){   
    if(argc != 2){
        cerr << "Please specify a SINGLE .txt file to process." << endl;
        return(1);
    }

    vector<PeekDeque<StringWrap> >* chainsp = new vector<PeekDeque<StringWrap> >(); 

    string word;

    string infileName = argv[1];
    ifstream* INFILEp = new ifstream(infileName.c_str(), ios_base::in);

    while ((*INFILEp) >> word) {
        cout << word << endl;
        testNewWord(word, chainsp);
    }

    INFILEp->close();
}

这很奇怪,因为我使用vector<string>制作了这个程序的更简单版本,并且工作正常。我不确定这个编译器错误说的是什么以及从哪里开始调试?

0 个答案:

没有答案