Invalid use of void expression.

0

Witam.
Od dłuższego czasu męczy mnie błąd "Invalid use of void expression". W całości wygląda tak:

AccountSystem\AccountSystem.cpp: In function 'bool DeleteAccount(std::__cxx11::string&)':
AccountSystem\AccountSystem.cpp:42:12: error: invalid use of void expression
         }());

Kod wygląda tak:

Część kodu z AccountSsytem\AccountSystem.cpp->DeleteAccount()
ErrHandler(0, "AccountSystem/DeleteAccount", "Cannot access datafile with accounts.", false, true, [&AccPath](){
            std::ofstream stream(AccPath.c_str());
            stream << HCipher::encode("temp") << ' ' <<HCipher::encode("temp");
            std::cout<<"Temporality account created. You can log in into them with login \"temp\" and password \"temp\"\nand then change login and password.";
            stream.close();
        }());

oraz deklaracja ErrHandler():

template <typename ErrCode>
bool ErrHandler(ErrCode code,
                const char* function,
                const char* msg,
                bool IsFatal,
                bool OtherAction = false,
                void(*func)() = nullfunc)

Nie wiem w czym jest problem.

Z góry dziękuję za pomoc.

Pozdrawiam.

5

Nie możesz konwertować lambdy z nie-pustym capture list na function pointer.

0
ErrHandler(0, "AccountSystem/DeleteAccount", "Cannot access datafile with accounts.", false, true, [](){
            // std::ofstream stream(AccPath.c_str());
            // stream << HCipher::encode("temp") << ' ' <<HCipher::encode("temp");
            // std::cout<<"Temporality account created. You can log in into them with login \"temp\" and password \"temp\"\nand then change login and password.";
            // stream.close();
        }());

AccountSystem\AccountSystem.cpp: In function 'bool DeleteAccount(std::string&)':
AccountSystem\AccountSystem.cpp12: error: invalid use of void expression
}());

Nie sądzę, żeby chodziło o to.

2

Ach tak, bo Ty tę lambdę od razu wywołujesz, co zwraca void, który próbujesz przekazać do funkcji ErrHandler. [](){ }() <-- ten ostatni nawias to wywołanie funkcji. Zauważ, że moja poprzednia odpowiedź wciąż jest aktualna.

0

Racja. Teraz działa, dziękuję. Jednakże w tej lambdzie muszę użyć zmiennej AccPath. Co mogę zrobić, żebym mógł jej użyć... ?

2

Jeśli możesz, to w deklaracji ErrHandler użyj std::function. To wszystko. Do std::function skonwertujesz zarówno function pointer, jak i lambde z dowolnym capture list.

1 użytkowników online, w tym zalogowanych: 0, gości: 1