当我只想在blankspace之前使用:' cin >> buffer
读取字符时,此程序有效。但是,我想读取用户输入的所有内容,包括空格。所以,我改变了使用">>"打个电话。我似乎传递了正确的参数,我已经完成了#include和#include。编译时的错误消息是:
Driver.cpp:在函数'int main(int,char * const *)'中: Driver.cpp:49:44:错误:没有匹配的函数来调用 'getline(std :: istream&,char [1024])' getline(cin,buffer); ^ Driver.cpp:49:44:注意:候选人是:/usr/include/wchar.h:4:0中包含的文件, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/cwchar:44, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/postypes.h:40, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iosfwd:40, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ios:38, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ostream:38, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iostream:39, 来自Driver.cpp:1:/usr/include/sys/stdio.h:37:9:注意:ssize_t getline(char **,size_t *,FILE *)ssize_t _EXFUN(getline, (char **,size_t *,FILE *)); ^ /usr/include/sys/stdio.h:37:9:注意:候选人需要3个参数,2个提供的文件包含在 /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/string:52:0, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/locale_classes.h:40, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/ios_base.h:41, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ios:42, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ostream:38, 来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iostream:39, 来自Driver.cpp:1:/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.h:2793:5: 注意:模板 std :: basic_istream< _CharT,_Traits>& std :: getline(std :: basic_istream< _CharT,_Traits>&amp ;, 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:
注意:模板参数扣除/替换失败: Driver.cpp:49:44:注意:不匹配的类型'std :: basic_string< _CharT, _Traits,_Alloc>'和'char [1024]' getline(cin,buffer); ^ Makefile:24:目标配方' Driver.o'失败的make: * [Driver.o]错误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");
}
这是我的第一篇文章,所以请告诉我是否搞砸了我的格式,如果需要更多信息来帮助我。谢谢!
答案 0 :(得分:6)
std::getline()
的所有重载都需要(basic) istream
和string
。要解决此问题,请将char buffer []
更改为std::string buffer
并记住#include <string>
。