简体   繁体   中英

C++ Sys/Stat.h has an error?

I'm trying to create a cross-platform program. I just created a class and made a function which gets the path of the current user. I wanted to use that path later. But somehow I get these errors :

"/usr/include/x86_64-linux-gnu/sys/stat.h:-1: In member function 'void FileManager::p_getfilepath()':"

"/usr/include/x86_64-linux-gnu/sys/stat.h:105: error: expected unqualified-id before string constant"

"/home/david/VocabularyTrainer/filemanager.cpp:31: error: expected '}' at end of input"

btw the 31th line is the last line here in this code :

void FileManager::p_getfilepath()
{
#ifdef Q_OS_WIN32
#include <windows.h>
#endif

#ifdef Q_OS_LINUX
#include <sys/stat.h>

struct passwd *p;
uid_t  uid;

if ((p = getpwuid(uid = geteuid())) == NULL)
{
    QMessageBox* mb;
    mb->setText("");
    mb->exec();
    delete mb;
}
else
{
    filepath = p->pw_dir;
}
#endif
}

Anyone knows what's wrong? I'm on linux mint.

By including your headers inside your class functions, you're making everything in the header a part of the function.

#ifdef Q_OS_WIN32
#include <windows.h>
#endif

#ifdef Q_OS_LINUX
#include <sys/stat.h>
#endif

void FileManager::p_getfilepath()
{
#ifdef Q_OS_LINUX

    struct passwd *p;
    uid_t  uid;

    if ((p = getpwuid(uid = geteuid())) == NULL)
    {
        QMessageBox* mb;
        mb->setText("");
        mb->exec();
        delete mb;
    }
    else
    {
        filepath = p->pw_dir;
    }

#endif
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM