プロが教えるわが家の防犯対策術!

下記で、大量のエラーが出る。
//C:/Users/usui/perl/LWP/170220-Iterative_processing/html/01.cpp {{{
#include <iostream>
#include <string>
#include <unistd.h>
#include <stdlib.h>

using namespace std; // }}}

class filter{
private:
int ret;
DIR *dir;
struct dirent *dp;
char path[]=".";
int ret;
string name;
public:
// コンストラクター
filter(string s){
name = s;
const char* cstr = name.c_str();
ret=chdir(cstr);
if(ret!=0){
cout << "chdirに失敗\n" << name <<endl;
exit(1);
}
//ret=chdir(path);
if((dir=opendir(path))==NULL){
perror("opendir");
exit(-1);
}

for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
if(strlen(dp->d_name)>10){
printf("<target>%s</target>\n",dp->d_name);
read_file(dp->d_name);
}
}
};
int main(){
filter dekita("C:/Users/usui/perl/LWP/170220-Iterative_processing/html");
}

// vim:set ft=cpp fdm=marker:

エラーの内容は下記。

01.cpp:12:2: error: 'DIR' does not name a type
DIR *dir;
^
01.cpp:14:15: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
char path[]=".";
^
01.cpp:15:6: error: redeclaration of 'int filter::ret'
int ret;
^
01.cpp:11:6: note: previous declaration 'int filter::ret'
int ret;
^
01.cpp:42:2: error: expected '}' at end of input
}
^
01.cpp:14:15: error: initializer-string for array of chars is too long [-fpermissive]
char path[]=".";
^
01.cpp: In constructor 'filter::filter(std::string)':
01.cpp:19:19: error: array used as initializer
filter(string s){
^
01.cpp:28:6: error: 'dir' was not declared in this scope
if((dir=opendir(path))==NULL){
^
01.cpp:28:22: error: 'opendir' was not declared in this scope
if((dir=opendir(path))==NULL){
^
01.cpp:29:19: error: 'perror' was not declared in this scope
perror("opendir");
^
01.cpp:33:17: error: 'dir' was not declared in this scope
for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
^
01.cpp:33:20: error: 'readdir' was not declared in this scope
for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
^
01.cpp:34:15: error: invalid use of incomplete type 'struct dirent'
if(strlen(dp->d_name)>10){
^
01.cpp:13:9: error: forward declaration of 'struct dirent'
struct dirent *dp;
^
01.cpp:34:23: error: 'strlen' was not declared in this scope
if(strlen(dp->d_name)>10){
^
01.cpp:35:38: error: invalid use of incomplete type 'struct dirent'
printf("<target>%s</target>\n",dp->d_name);
^
01.cpp:13:9: error: forward declaration of 'struct dirent'
struct dirent *dp;
^
01.cpp:35:46: error: 'printf' was not declared in this scope
printf("<target>%s</target>\n",dp->d_name);
^
01.cpp:36:16: error: invalid use of incomplete type 'struct dirent'
read_file(dp->d_name);
^
01.cpp:13:9: error: forward declaration of 'struct dirent'
struct dirent *dp;
^
01.cpp:36:24: error: 'read_file' was not declared in this scope
read_file(dp->d_name);
^
01.cpp: At global scope:
01.cpp:42:2: error: expected unqualified-id at end of input
}
^
どうも、CとC++では、関数の名前とかが違う様だが。済みません。間違いの
指摘をお願いします。

質問者からの補足コメント

  • うーん・・・

    回答者の皆さんへ。回答有難う御座います。

    この質問に有るソースは非常に短いので。回答はコードで回答して貰えると
    理解が早いのですが。また、ヒントは時間がかかります。

    私が聞いているのは、要はこれが動く事です。その為の質問ですから。幾ら
    ここで私が文法を理解をしたとしてもそれで結果的に動かなければ。それは
    答えでは有りません。

    これ程簡単な質問と言うのは、無いと思いますが。私が回答者の立場ならここでは
    さっさと実際に動くコードを提示してさっさと閉じる所だと思いますが。

    宜しくお願いします。

      補足日時:2017/03/06 13:54
  • うーん・・・

    01.cpp:16:15: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
    char path[]=".";
    ^
    01.cpp:16:15: error: initializer-string for array of chars is too long [-fpermissive]
    01.cpp: In constructor 'filter::filter(std::string)':
    01.cpp:21:19: error: array used as initializer
    filter(string s){
    ^

      補足日時:2017/03/06 16:54
  • うーん・・・

    有難う御座います。

    仰せのとおり、私の勘違いが有りました。
    ヘッダーファイルのインクルードミスが有りました。

    それ以外にも未だえらーは出ます。

    No.3の回答に寄せられた補足コメントです。 補足日時:2017/03/06 16:59
  • うーん・・・

    有難う御座います。
    下記で動きました。
    //C:/Users/usui/perl/LWP/170220-Iterative_processing/html/01.cpp {{{
    #include <iostream>
    #include <string>
    #include <unistd.h>
    #include <stdlib.h>
    #include <dirent.h>
    #include <string.h>


    using namespace std; // }}}

    class filter{
    private:
    DIR *dir;
    struct dirent *dp;
    //char path[]=".";
    int ret;
    string name;

    続きは、次に

      補足日時:2017/03/06 17:10
  • うーん・・・

    public:
    // コンストラクター
    filter(string s){
    name = s;
    const char* cstr = name.c_str();
    //ret=chdir(cstr);
    //if(ret!=0){
    // cout << "chdirに失敗" << name <<endl;
    // exit(1);
    //}
    if((dir=opendir(cstr))==NULL){
    cout << "opendirに失敗" <<endl;
    exit(-1);
    }

    次に

      補足日時:2017/03/06 17:12
  • うーん・・・

    for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
    if(strlen(dp->d_name)>10){
    cout << "<target>" <<dp->d_name << "</target>" <<endl;
    }
    }
    }
    };

    int main(){
    filter dekita("C:/Users/usui/perl/LWP/170220-Iterative_processing/html");
    }

    // vim:set ft=cpp fdm=marker:

    以上です。

      補足日時:2017/03/06 17:14
  • うーん・・・

    最後に,下記をコメントアウトしないとエラーになるのは何故なのか。
    char path[]=".";

    エラーの内容は次に。

      補足日時:2017/03/06 17:19
  • うーん・・・

    01.cpp:16:15: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
    char path[]=".";
    ^
    01.cpp:16:15: error: initializer-string for array of chars is too long [-fpermissive]
    01.cpp: In constructor 'filter::filter(std::string)':
    01.cpp:21:19: error: array used as initializer
    filter(string s){
    ^

      補足日時:2017/03/06 17:20

A 回答 (7件)

基本的には、コンストラクタの中で、static でない変数を初期化することは禁止されています(最初のメッセージは、警告メッセージで、C++11 の新規格なら許されると言うことです)



あと、C++であてtも、char path[] という配列を初期化することはできません。

ということです。
    • good
    • 1
この回答へのお礼

有難う御座います。

勉強になりました。実は、今日が初めてです。
C++の勉強がです。

少しは、最初のとっつきにくさが取れたのかも。
マダマダ分からない所が有りますが。

ここで、閉じます。有難う御座いました。

お礼日時:2017/03/06 18:23

さて、



filter::filert の中で定義されている
char path[]
が、なぜそこにあるのか? そもそも本当に必要なのか? なぜ、底に書いてしまったのか? 冷静に考えてください。

ただ単に、「適当にコメントアウトしたら、コンパイルに通った」ということを繰り返すと、後々後悔します。
    • good
    • 0
この回答へのお礼

有難う御座います。

仰せの通り不要です。但し、文法的な物が今度は分かりません。
char path[]=".";
上記をコメントアウトしないと下記のエラーが出ます。
取り敢えずはコメントアウトして動きますが。逆に今は、この
エラーが気になっています。

文法的には何が駄目なんでしょうか。
下記のエラーが出ます。
01.cpp:16:15: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
char path[]=".";
^
01.cpp:16:15: error: initializer-string for array of chars is too long [-fpermissive]
01.cpp: In constructor 'filter::filter(std::string)':
01.cpp:21:19: error: array used as initializer
filter(string s){
^

済みません。もう、動く動かないは関係ないのでエラ一になる訳を
教えて下さい。

お礼日時:2017/03/06 17:32

ついでに言えば、中括弧のアンバランスも、書いた人でないと、正しい括弧の位置を決定することはできません。

(想像する事はできます)
    • good
    • 0
この回答へのお礼

有難う御座いました。
修正したソースを下記に載せます。
//C:/Users/usui/perl/LWP/170220-Iterative_processing/html/01.cpp {{{
#include <iostream>
#include <string>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>


using namespace std; // }}}

class filter{
private:
DIR *dir;
struct dirent *dp;
char path[]=".";
int ret;
string name;
public:
// コンストラクター
filter(string s){
name = s;
const char* cstr = name.c_str();
ret=chdir(cstr);
if(ret!=0){
cout << "chdirに失敗" << name <<endl;
exit(1);
}
if((dir=opendir(path))==NULL){
cout << "opendirに失敗" <<endl;
exit(-1);
}

for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
if(strlen(dp->d_name)>10){
cout << "<target>" <<dp->d_name << "</target>" <<endl;
}
}
}
};

int main(){
filter dekita("C:/Users/usui/perl/LWP/170220-Iterative_processing/html");
}

// vim:set ft=cpp fdm=marker:

未だ、エラーが出ます。次に載せます。

お礼日時:2017/03/06 16:51

ですから、



> DIR はどこにある?
に対しては、include で入っていると「思います」ではなくて、
本当に入っているか、そして、どのように定義猿かは調べて欲しいです。

その情報がないと、DIR に関するエラーを消すことは誰にもできません。

> int ret って、何カ所ある?
に対しては、実際に何カ所(そしてどこに)あるか、確認して欲しいです。
本来、複数箇所にあるはずのないものですから。
そして、これがなぜ、複数箇所にあるかは、書いた人以外にはわかりません。

> { } の釣り合いあってる?
に対しては、実際に、釣り合いを確認して欲しいです。
それだけでいくつかのエラーは消えますから。

最後に、「Cでは動いた」と言うことですが、このコード自体はCでコンパイル出来ないはずです。
「Cと同じコードを書いたつもり」でも、どこかに相違があります。
上記の項目は、いずれも、Cの範囲でエラーになることです。
    • good
    • 0
この回答へのお礼

有難う御座います。
Cで動いた時のソースは下記です。

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

void main(argc,argv){
DIR *dir;
struct dirent *dp;
char path[]="C:/Users/usui/perl/LWP/170220-Iterative_processing/html/";

if((dir=opendir(path))==NULL){
perror("opendir");
exit(-1);
}

for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){
printf("%s\n",dp->d_name);
}
closedir(dir);
}

確かに言われた通りにミスが有り修正しました。
次の補足で述べます。容量が足りないので。

お礼日時:2017/03/06 16:30

例えば


01.cpp:28:22: error: 'opendir' was not declared in this scope
というメッセージから何を想像しますか?
この回答への補足あり
    • good
    • 0
この回答へのお礼

有難う御座います。

だから、それは読んで字のごとく。opendirと言うのが
無いと言う事なんだろうけど。それだけでは、答えに
ならない。

要は、ここではインクルード、或いは、別の関数名だったり
はするのだろうか。その知識が私には無い。これは、知っている
人がいれば。そんなに回答が難しい所だとは私は思わないが。

何故にそんなに勿体振るのだろうか。

だから、回答者はそんな所に首を挟むのではなくてそこから
先を言ってもらわないと。先が見えないよ。

もっと、手短に。回答は出来ないのだろうか。
こう言う人ばかりでは、現場もお客も大迷惑をすると
思うが。

何か、人の意見は無視して黙って聞いてろの感じだ。
質問は若しも、私が回答者なら。知っていると仮定を
しての話だが。これほど分かりやすい質問は無いと
思うのだが。

お礼日時:2017/03/06 13:44

多くの部分は、C言語の範囲でわかるエラーです。


DIR はどこにある? とか int ret って、何カ所ある? とか。 { } の釣り合いあってる? とか。
    • good
    • 0
この回答へのお礼

どう思う?

有難う御座います。

DIRはインクルードで入っていると思いますが。
少なくとも、Cでは動きますが。

あの私からも一言、言いたいですが。
どうも、上から目線の人がここは多い様です。

それならば、答えをする人は一発で質問者を唸らせる
回答をして下さい。これが出来ないと言う事は、余り
能力的には大差無いと私は思います。

的確に答えて何ぼですよ。ヒントとか、言っても質問者
が理解をしなければそれは回答としては満点では有りません。

私が回答者の立場で有れば。質問者の不満の気持ちは清く受け止めますが。
と言うのは、先ほどの人は甘リにも回答が酷かったもんですから。

少し、今は興奮をしています。回答者のこんなもんも分からんの
かと言うのは気持ちは分かリますよ。然し、そんなのは正しく
質問者が回答者の答えにしっかりと満足を得てから後に、質問者
に対して言いたい事が有ると言うのは分かりますよ。

先程の様に、何もなくてと言うのは困ります。

失礼しました。これは、あなたに対して言うべき事ではなかったです。
私の愚痴です。

お礼日時:2017/03/06 13:14

コピペ思考では何も学べない。


調べようとする態度が無ければ何も進展しないぞ。

わからない。 だから尋ねる。

そういう態度を繰り返して未来があると思うか。

答えてくれない他人が悪い。
それがお前の結論か。
    • good
    • 7

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!