当我只想在blankspace之前读取字符时,使用:' cin >> buffer.但是,我想读取用户输入的所有内容,包括空格.所以,我从使用">>"更改为getline调用.我似乎传递了正确的参数,我已经完成了#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来自/ usr/lib/gcc/x86_64 -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++/的文件字符串: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 /来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ios:42 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>&,std :: basic_string <_CharT,_Traits,_Alloc> &&)getline(basic_istream <_CharT,_Traits>&__ is,^
Run Code Online (Sandbox Code Playgroud)^ /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");
}
Run Code Online (Sandbox Code Playgroud)
这是我的第一篇文章,请告诉我是否搞砸了我的格式,如果需要更多信息来帮助我.谢谢!
std::getline()a (basic) istream和a的所有重载都是string.要解决问题,请char buffer []转到std::string buffer并记住#include <string>.