Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Программирование под Unix/Linux > struct DIR was not declared


Автор: andrew_121 9.8.2009, 13:27
Здравствуйте!

Есть банальный кусок кода:
Код

#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>

#include <string>
#include <vector>

#include "fileinfo.h"
#include "filetools.h"
#include "scandir.h"

/**************************************************************************/

bool scandir(std::vector<FileInfo>* list, const std::string& path) {
    FileInfo fi;
    struct stat st;
    struct dirent *dir_info = NULL;
    DIR* dir = opendir(path.c_str());
    if ( !dir ) return false;
    
    list->clear();

    while ( (dir_info = readdir(dir)) != NULL ) {
        if ( dir_info->d_name[0] == '.' && !isalpha(dir_info->d_name[1]) ) continue;
        if ( extract_ext(dir_info->d_name) != ".torrent" ) continue;
        fi.name = path + "/" + std::string(dir_info->d_name);
        stat(fi.name.c_str(), &st);
        fi.type = ((st.st_mode & S_IFMT) == S_IFDIR) ? FileInfo::dir : FileInfo::file;
        /** add to the list */
        list->push_back(fi);
    }

    closedir(dir);

   return true;
}

/**************************************************************************/

Компилятор напрочь отказывается его компилировать.
Цитата

scandir.cpp: In function 'bool scandir(std::vector<FileInfo, std::allocator<FileInfo> >*, const std::string&)':
scandir.cpp:19: error: 'DIR' was not declared in this scope
scandir.cpp:19: error: 'dir' was not declared in this scope
scandir.cpp:19: error: 'opendir' was not declared in this scope
scandir.cpp:24: error: 'readdir' was not declared in this scope
scandir.cpp:25: error: invalid use of incomplete type 'struct dirent'
scandir.cpp:18: error: forward declaration of 'struct dirent'
scandir.cpp:25: error: invalid use of incomplete type 'struct dirent'
scandir.cpp:18: error: forward declaration of 'struct dirent'
scandir.cpp:26: error: invalid use of incomplete type 'struct dirent'
scandir.cpp:18: error: forward declaration of 'struct dirent'
scandir.cpp:27: error: invalid use of incomplete type 'struct dirent'
scandir.cpp:18: error: forward declaration of 'struct dirent'
scandir.cpp:34: error: 'closedir' was not declared in this scope

Я написал это:
Код


#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main() {
    DIR *dir = opendir(".");
    if ( dir ) {
    closedir(dir);
    }
    return 0;
}

Компилируется.
Что не так?

Автор: kemiisto 9.8.2009, 13:40
В качестве небольшой ремарки. Друг мой, можно Вас попросить вот после таких тем более не писать:
Цитата(andrew_121 @  4.8.2009,  20:12 Найти цитируемый пост)
Используя С++, ты царь и бог. Та все знаешь и понимаешь. Если не понимаешь, посмотрел исходники и понял.

А то как-то одно с другим не вяжется... smile 

Автор: andrew_121 9.8.2009, 13:55
kemiisto, "друг мой", пост выше внимательно почитай. Я бы не стал постить если бы все было так просто.
И второй пример посмотри.

А по теме что-то полезное можете сообщить? ...нет? ...досвиданЯ.
"друг мой" хе...

Автор: GrayCardinal 9.8.2009, 15:58
[cut]

Добавлено через 2 минуты и 14 секунд
andrew_121, 
Попробуй в начало воткнуть
Цитата

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

Авось поможет... smile

Добавлено через 4 минуты и 32 секунды
Цитата

Что не так? 

В маленьком кусочке stdio.h включен smile

Автор: andrew_121 9.8.2009, 16:36
GrayCardinal, В первый добавил - так же.
Из второго удалил - так же.

какие-то хидеры перекрывают нужные. но как это исправить не понятно. пробовал в разных комбинациях.

Автор: GrayCardinal 9.8.2009, 16:39
andrew_121, 
Закомменть поочереди инклюды с <vector> Выскочит проблемный smile

Автор: andrew_121 9.8.2009, 20:45
Победил я его.
Оставил только эти хидеры, и скомпилилось:
Код

#include <sys/stat.h>
#include <dirent.h>
#include "filetools.h"

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)