C++ getline没有匹配的函数

C++ getline没有匹配的函数,c++,getline,C++,Getline,当我只想通过使用:'cin>>缓冲区读取空格前的字符时,这个程序就工作了。但是,我想阅读用户输入的所有内容,包括空格。因此,我从使用>>改为使用getline调用。我似乎在传递正确的参数,我已经完成了include和include。编译时的错误消息包括: Driver.cpp:在函数“int maiint,char*const*”中: Driver.cpp:49:44:错误:没有用于调用的匹配函数 'getlinestd::istream&,char[1024]' getlinecin,缓冲液;

当我只想通过使用:'cin>>缓冲区读取空格前的字符时,这个程序就工作了。但是,我想阅读用户输入的所有内容,包括空格。因此,我从使用>>改为使用getline调用。我似乎在传递正确的参数,我已经完成了include和include。编译时的错误消息包括:

Driver.cpp:在函数“int maiint,char*const*”中: Driver.cpp:49:44:错误:没有用于调用的匹配函数 'getlinestd::istream&,char[1024]' getlinecin,缓冲液; ^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:note:ssize\u t getlinechar**,size\u t*,FILE*ssize\u t\u EXFUNgetline, 字符**,大小*文件*; ^/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::getlinestd::basic_istream&, 基本字符串& getlinebasic_是流&_是, ^

注意:模板参数扣除/替换失败: Driver.cpp:49:44:注意:不匹配的类型“std::basic_string”和“char[1024]” getlinecin,缓冲液; ^生成文件:24:目标“Driver.o”的配方生成失败::[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");
}

这是我的第一篇帖子,所以如果我的格式搞砸了,请告诉我,如果需要更多的信息来帮助我。谢谢

std::getline的所有重载都采用一个基本的istream和一个字符串。要解决此问题,请将char buffer[]更改为std::string buffer,并记住包含。

std::getline用于std::string,您应该自动使用它,但更让我担心的是您的整个空spiel。那部分很糟糕。包括CSTDDF或其他特定的头,例如CSTDIO和NULL,将被评估为0。@ Ghost确保C++中的NULL评估为0并不像您所说的那样糟糕。是的,标准库标题将处理它。但通常会包含一个写得不好的C头,它以一种不受欢迎的方式定义NULL。当然,您可以通过使用nullptr来解决所有问题。好的,我包括了cstdlib,现在不必定义NULL。但是在getline的主题上,为什么我要用stdd::?command=NULL自动在getline前面加上前缀?它没有做你认为它在做的事情。你不必用std::,作为前缀,由于您正在使用命名空间std,所有教科书都建议初学者使用,而且中间教科书似乎从未重温过。虽然解决方案还可以,但请注意有一个成员函数。这解决了问题!然而,现在,所有涉及学生的行都给出了错误。我已经有好几个季度没有处理名称空间了。
#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");
}