getline() Ошибка C++

Рейтинг: 0Ответов: 1Опубликовано: 14.01.2023

Делаю консоль на С++ для себя, когда запускаю код то пишет ошибку

Код:

#include <iostream>
#include <string>

using namespace std;
int main() {
    string cmd();
    int a=0, b=1;
    cout << "Console On C++" << endl;
    while (a != b) {
        getline(cin, cmd);
    }
}

Ошибка:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\ProUser\Documents\Console.cpp||In function 'int main()':|
C:\Users\ProUser\Documents\Console.cpp|6|warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]|
C:\Users\ProUser\Documents\Console.cpp|6|note: remove parentheses to default-initialize a variable|
C:\Users\ProUser\Documents\Console.cpp|10|error: no matching function for call to 'getline(std::istream&, std::string (&)())'|
c:\application\gcc\include\c++\12.2.0\bits\basic_string.tcc|1012|note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'|
c:\application\gcc\include\c++\12.2.0\bits\basic_string.tcc|1012|note:   template argument deduction/substitution failed:|
C:\Users\ProUser\Documents\Console.cpp|10|note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'std::string()' {aka 'std::__cxx11::basic_string<char>()'}|
c:\application\gcc\include\c++\12.2.0\bits\basic_string.h|3923|note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'|
c:\application\gcc\include\c++\12.2.0\bits\basic_string.h|3923|note:   template argument deduction/substitution failed:|
C:\Users\ProUser\Documents\Console.cpp|10|note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'std::string()' {aka 'std::__cxx11::basic_string<char>()'}|
c:\application\gcc\include\c++\12.2.0\bits\basic_string.h|3931|note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'|
c:\application\gcc\include\c++\12.2.0\bits\basic_string.h|3931|note:   template argument deduction/substitution failed:|
C:\Users\ProUser\Documents\Console.cpp|10|note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'std::string()' {aka 'std::__cxx11::basic_string<char>()'}|
c:\application\gcc\include\c++\12.2.0\bits\basic_string.h|3938|note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(basic_istream<_CharT, _Traits>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'|
c:\application\gcc\include\c++\12.2.0\bits\basic_string.h|3938|note:   template argument deduction/substitution failed:|
C:\Users\ProUser\Documents\Console.cpp|10|note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'std::string()' {aka 'std::__cxx11::basic_string<char>()'}|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 2 second(s)) ===|

Ответы

▲ 4Принят

Вот это —

string cmd();

объявление функции с именем cmd. которая не принимает аргументы и возвращает значение типа string.

Переменная cmd типа string объявляется так:

string cmd;