這個程序工作時,我只想通過使用'cin >> buffer
在blankspace之前閱讀字符。不過,我想讀取用戶輸入的所有內容,包括空格。所以,我改變了使用「>>」到getline電話。我似乎正在傳遞正確的參數,我已經完成了#include和#include。當我編譯錯誤消息是:沒有getline的匹配函數
Driver.cpp: In function ‘int main(int, char* const*)’: Driver.cpp:49:44: error: no matching function for call to ‘getline(std::istream&, char [1024])’ getline(cin, buffer); ^Driver.cpp:49:44: note: candidates are: In file included from /usr/include/wchar.h:4:0, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/cwchar:44, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/postypes.h:40, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iosfwd:40, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ios:38, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ostream:38, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iostream:39, from Driver.cpp:1: /usr/include/sys/stdio.h:37:9: note: ssize_t getline(char**, size_t*, FILE*) ssize_t _EXFUN(getline, (char **, size_t *, FILE *)); ^/usr/include/sys/stdio.h:37:9: note: candidate expects 3 arguments, 2 provided In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:52:0, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/locale_classes.h:40, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/ios_base.h:41, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ios:42, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ostream:38, from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iostream:39, from Driver.cpp:1: /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.h:2793:5: note: template std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&) getline(basic_istream<_CharT, _Traits>& __is, ^
^/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.tcc:1068:5:
note: template argument deduction/substitution failed: Driver.cpp:49:44: note: mismatched types ‘std::basic_string<_CharT, _Traits, _Alloc>’ and ‘char [1024]’ getline(cin, buffer); ^Makefile:24: recipe for target 'Driver.o' failed make: * [Driver.o] Error 1
這裏是東西,不是這種情況下,一部分的代碼刪除:如果擰
#include <iostream>
#include <cstdio>
#include <string>
#include <getopt.h>
#include "Driver.hpp"
#include "SymTab.hpp"
using namespace std;
#ifdef NULL
#undef NULL
#define NULL 0
#endif
ostream & operator << (ostream & stream, const Student & stu) {
return stream << "name: " << stu.name
<< " with studentnum: " << stu.studentnum;
}
int main (int argc, char * const * argv) {
char buffer[BUFSIZ];
char command;
long number;
char option;
while ((option = getopt (argc, argv, "x")) != EOF) {
switch (option) {
case 'x': SymTab<Student>::Set_Debug_On();
break;
}
}
SymTab<Student> ST;
ST.Write (cout << "Initial Symbol Table:\n");
while (cin) {
command = NULL; // reset command each time in loop
cout << "Please enter a command ((i)nsert, "
<< "(l)ookup, (r)emove, (w)rite): ";
cin >> command;
switch (command) {
case 'i': {
cout << "Please enter student name to insert: ";
getline(cin, buffer);
cout << "Please enter student number: ";
cin >> number;
Student stu (buffer, number);
// create student and place in symbol table
ST.Insert (stu);
break;
}
}
ST.Write (cout << "\nFinal Symbol Table:\n");
}
這是我的第一篇文章,所以讓我知道我的格式化,如果需要更多的信息來幫助我。謝謝!
'std :: getline'用於'std :: string',你應該自動使用它,但是更讓我擔心的是你的整個'NULL'subiel。那部分非常糟糕。包括'cstddef'或其他特定的頭文件(比如'cstdio')和'NULL'將會作爲評估值爲0. – ghostofstandardspast
@ghost確保C++中的NULL值爲0並不像聲音那樣糟糕。是的,標準庫頭文件將會處理它。但是包含一個(寫得不好的)C頭文件是很常見的,它以不希望的方式定義NULL。當然,你可以用'nullptr'解決所有問題。 –
好的,所以我包含了cstdlib,現在不必定義NULL。 但是關於getline的話題,爲什麼我應該自動使用stdd :: getline getline? – user3750325