
下記で、大量のエラーが出る。
//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++では、関数の名前とかが違う様だが。済みません。間違いの
指摘をお願いします。

No.7ベストアンサー
- 回答日時:
基本的には、コンストラクタの中で、static でない変数を初期化することは禁止されています(最初のメッセージは、警告メッセージで、C++11 の新規格なら許されると言うことです)
あと、C++であてtも、char path[] という配列を初期化することはできません。
ということです。
有難う御座います。
勉強になりました。実は、今日が初めてです。
C++の勉強がです。
少しは、最初のとっつきにくさが取れたのかも。
マダマダ分からない所が有りますが。
ここで、閉じます。有難う御座いました。

No.6
- 回答日時:
さて、
filter::filert の中で定義されている
char path[]
が、なぜそこにあるのか? そもそも本当に必要なのか? なぜ、底に書いてしまったのか? 冷静に考えてください。
ただ単に、「適当にコメントアウトしたら、コンパイルに通った」ということを繰り返すと、後々後悔します。
有難う御座います。
仰せの通り不要です。但し、文法的な物が今度は分かりません。
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){
^
済みません。もう、動く動かないは関係ないのでエラ一になる訳を
教えて下さい。

No.5
- 回答日時:
ついでに言えば、中括弧のアンバランスも、書いた人でないと、正しい括弧の位置を決定することはできません。
(想像する事はできます)有難う御座いました。
修正したソースを下記に載せます。
//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:
未だ、エラーが出ます。次に載せます。

No.4
- 回答日時:
ですから、
> DIR はどこにある?
に対しては、include で入っていると「思います」ではなくて、
本当に入っているか、そして、どのように定義猿かは調べて欲しいです。
その情報がないと、DIR に関するエラーを消すことは誰にもできません。
> int ret って、何カ所ある?
に対しては、実際に何カ所(そしてどこに)あるか、確認して欲しいです。
本来、複数箇所にあるはずのないものですから。
そして、これがなぜ、複数箇所にあるかは、書いた人以外にはわかりません。
> { } の釣り合いあってる?
に対しては、実際に、釣り合いを確認して欲しいです。
それだけでいくつかのエラーは消えますから。
最後に、「Cでは動いた」と言うことですが、このコード自体はCでコンパイル出来ないはずです。
「Cと同じコードを書いたつもり」でも、どこかに相違があります。
上記の項目は、いずれも、Cの範囲でエラーになることです。
有難う御座います。
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);
}
確かに言われた通りにミスが有り修正しました。
次の補足で述べます。容量が足りないので。
有難う御座います。
だから、それは読んで字のごとく。opendirと言うのが
無いと言う事なんだろうけど。それだけでは、答えに
ならない。
要は、ここではインクルード、或いは、別の関数名だったり
はするのだろうか。その知識が私には無い。これは、知っている
人がいれば。そんなに回答が難しい所だとは私は思わないが。
何故にそんなに勿体振るのだろうか。
だから、回答者はそんな所に首を挟むのではなくてそこから
先を言ってもらわないと。先が見えないよ。
もっと、手短に。回答は出来ないのだろうか。
こう言う人ばかりでは、現場もお客も大迷惑をすると
思うが。
何か、人の意見は無視して黙って聞いてろの感じだ。
質問は若しも、私が回答者なら。知っていると仮定を
しての話だが。これほど分かりやすい質問は無いと
思うのだが。

No.2
- 回答日時:
多くの部分は、C言語の範囲でわかるエラーです。
DIR はどこにある? とか int ret って、何カ所ある? とか。 { } の釣り合いあってる? とか。
有難う御座います。
DIRはインクルードで入っていると思いますが。
少なくとも、Cでは動きますが。
あの私からも一言、言いたいですが。
どうも、上から目線の人がここは多い様です。
それならば、答えをする人は一発で質問者を唸らせる
回答をして下さい。これが出来ないと言う事は、余り
能力的には大差無いと私は思います。
的確に答えて何ぼですよ。ヒントとか、言っても質問者
が理解をしなければそれは回答としては満点では有りません。
私が回答者の立場で有れば。質問者の不満の気持ちは清く受け止めますが。
と言うのは、先ほどの人は甘リにも回答が酷かったもんですから。
少し、今は興奮をしています。回答者のこんなもんも分からんの
かと言うのは気持ちは分かリますよ。然し、そんなのは正しく
質問者が回答者の答えにしっかりと満足を得てから後に、質問者
に対して言いたい事が有ると言うのは分かりますよ。
先程の様に、何もなくてと言うのは困ります。
失礼しました。これは、あなたに対して言うべき事ではなかったです。
私の愚痴です。

No.1
- 回答日時:
コピペ思考では何も学べない。
調べようとする態度が無ければ何も進展しないぞ。
わからない。 だから尋ねる。
そういう態度を繰り返して未来があると思うか。
答えてくれない他人が悪い。
それがお前の結論か。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- その他(プログラミング・Web制作) python fbprophetについて 1 2022/09/29 19:44
- C言語・C++・C# c言語の問題の説明、各所ごとに 5 2023/07/26 11:03
- MySQL テーブル作成です。どこかのスペルが間違っているか記号など スペースかな? 1 2022/10/01 05:08
- C言語・C++・C# プログラミングの授業の課題です 1 2023/01/17 22:15
- PHP php エラー 2 2022/10/23 16:43
- C言語・C++・C# バイナリファイルをコピーするのにかかる時間を測りたいのですが実行するとFatel error:gli 2 2022/11/03 01:10
- Windows 10 VirtualBox 7のゲストOSでの物理HDDパーティションのマウント方法 2 2023/05/04 13:01
- Visual Basic(VBA) ExcelVBAに関する質問 3 2023/02/17 10:47
- PHP php テーブルが作成できない 1 2022/11/17 23:41
- 英語 この英文は平易な反面格調高いですか? 1 2023/01/15 12:04
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
_tcscat がうまくいきません(V...
-
gccでコンパイル時のエラー
-
構文エラーが出ているのですが...
-
CStdioFile での数値データの読...
-
switch文のエラーについて
-
includeファイルが開けない
-
C++ 平均値、最大値と最小値の...
-
RS232cと通信が可能な、Cまたは...
-
構造体に決められた文字列を入...
-
C言語のポインターで詰まっている
-
#include "fstream.h"
-
Enterキーを押されたら次の処理...
-
「Aに対するBの割合」と「Aに対...
-
複数桁10進数の*桁目だけを抽出...
-
float と double
-
float型とdouble型の変数の違い...
-
[C++]メンバ関数の仮引数について
-
Aの値からBの値を除するとは??
-
関数の呼び出し
-
プログラム、どのように書いて...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
なぜ、C++の標準ヘッダをインク...
-
switch文のエラーについて
-
VC++で文字列から任意の文字を...
-
構文エラーが出ているのですが...
-
gccでコンパイル時のエラー
-
空ENTERの判別
-
vectorのイテレータを大小比較...
-
JPEGやPNGが読めるLoadImage関数
-
C++で、テキストファイルを一行...
-
std::wstringのメモリリークに...
-
enumの値から定義名を文字列化...
-
CStringとString
-
#define中の#のエスケープ
-
std::map の const 修飾について
-
#include "fstream.h"
-
_tcscat がうまくいきません(V...
-
C言語のポインターで詰まっている
-
構造体配列のvectorへの変換と...
-
C++での <iostream.h>と<iostre...
-
ヘッダーファイルがインクルー...
おすすめ情報
回答者の皆さんへ。回答有難う御座います。
この質問に有るソースは非常に短いので。回答はコードで回答して貰えると
理解が早いのですが。また、ヒントは時間がかかります。
私が聞いているのは、要はこれが動く事です。その為の質問ですから。幾ら
ここで私が文法を理解をしたとしてもそれで結果的に動かなければ。それは
答えでは有りません。
これ程簡単な質問と言うのは、無いと思いますが。私が回答者の立場ならここでは
さっさと実際に動くコードを提示してさっさと閉じる所だと思いますが。
宜しくお願いします。
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){
^
有難う御座います。
仰せのとおり、私の勘違いが有りました。
ヘッダーファイルのインクルードミスが有りました。
それ以外にも未だえらーは出ます。
有難う御座います。
下記で動きました。
//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(cstr))==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:
以上です。
最後に,下記をコメントアウトしないとエラーになるのは何故なのか。
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){
^