質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

1896閲覧

c++ で文字列を関数の引数で渡していく方法

trafalbad

総合スコア303

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2020/10/03 04:30

編集2020/10/03 05:36

関数が3つあります。

1.一つ目の関数で画像ファイル名vector<string>に格納して、for文で取り出していきます。
一つ目の関数で画像のファイル名を2つ目の関数に引き渡します。

2.これを3つ目の関数にまで引数で渡して、表示したいです。

しかしエラーが出ます。ファイル名を関数の引数で、渡して表示する方法を教えて下さい。よろしくお願いします。

試したコード

c++

1#include <assert.h> 2#include <dirent.h> 3#include <stdio.h> 4#include <stdlib.h> 5#include <sys/stat.h> 6#include <unistd.h> //DB 7#include <cassert> 8#include <chrono> 9#include <cmath> 10#include <cstdio> 11#include <fstream> 12#include <sstream> 13#include <atomic> //DB 14#include <iostream> 15#include <iomanip> //DB 16#include <queue> 17#include <mutex> //DB 18#include <string> 19#include <vector> 20#include <thread> //DB 21#include <opencv2/opencv.hpp> 22#define IMG_DIR "./img_test/" 23#define IMG_NUM 20 24using namespace std; 25using namespace std::chrono; 26using namespace cv; 27 28vector<string> kinds, images; //DB 29std::vector<string> img_filenames; 30std::string output_name2; 31std::string output_filenames; 32 33 34vector<string> ListImages(const char *path) { 35 vector<string> images; 36 images.clear(); 37 struct dirent *entry; 38 39 /*Check if path is a valid directory path. */ 40 struct stat s; 41 lstat(path, &s); 42 if (!S_ISDIR(s.st_mode)) { 43 fprintf(stderr, "Error: %s is not a valid directory!\n", path); 44 exit(1); 45 } 46 47 DIR *dir = opendir(path); 48 if (dir == nullptr) { 49 fprintf(stderr, "Error: Open %s path failed.\n", path); 50 exit(1); 51 } 52 53 while ((entry = readdir(dir)) != nullptr) { 54 if (entry->d_type == DT_REG || entry->d_type == DT_UNKNOWN) { 55 std::string name = entry->d_name; 56 std::string ext = name.substr(name.find_last_of(".") + 1); 57 if ((ext == "jpg") || (ext == "png")) { 58 images.push_back(name); 59 } 60 } 61 } 62 63 closedir(dir); 64 sort(images.begin(), images.end()); 65 return images; 66} 67 68string path_thread2(const char *path){ 69 cout << "name2: " << path << endl; 70 return path; 71} 72 73 74void path_thread3(const char *path){ 75 cout << "name3: " << path << endl; 76} 77 78//void task(float *result){ 79void path_thread1(void){ 80 cv::Mat input_image[IMG_NUM]; 81 img_filenames = ListImages(IMG_DIR); 82 //cout << "B: " << IMG_DIR+img_filenames[1] << endl; 83 for (int i=0; i<=IMG_NUM; i++){ 84 if (i>=IMG_NUM) break; 85 output_filenames = img_filenames[i]; 86 output_name2 = path_thread2(output_filenames.c_str()); 87 path_thread3(output_name2.c_str()); 88 //input_image[i] = cv::imread(IMG_DIR+img_filenames[i], IMREAD_UNCHANGED); 89 }; 90} 91 92int main(){ 93 // main loop 94 path_thread1(); 95 return 0; 96}

出力

sh

1name2: test_000 2.png 2name3: test_000 2.png 3name2: test_000.png 4name3: test_000.png 5name2: test_001 2.png 6name3: test_001 2.png 7name2: test_001.png 8name3: test_001.png 9name2: test_002 2.png 10name3: test_002 2.png 11name2: test_002.png 12name3: test_002.png 13name2: test_003 2.png 14name3: test_003 2.png 15name2: test_003.png 16name3: test_003.png 17name2: test_004 2.png 18name3: test_004 2.png 19name2: test_004.png 20name3: test_004.png 21name2: test_005 2.png 22name3: test_005 2.png 23name2: test_005.png 24name3: test_005.png 25name2: test_006 2.png 26name3: test_006 2.png 27name2: test_006.png 28name3: test_006.png 29name2: test_007 2.png 30name3: test_007 2.png 31name2: test_007.png 32name3: test_007.png 33name2: test_008 2.png 34name3: test_008 2.png 35name2: test_008.png 36name3: test_008.png 37name2: test_009 2.png 38name3: test_009 2.png 39name2: test_009.png 40name3: test_009.png

#追記
なぜ出力結果が2個になるのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

SHOMI

2020/10/03 06:00

質問内容は消さずに追記しないと回答が意味不明になりますよ
guest

回答1

0

ベストアンサー

C++なら、Stringで渡していけばいいんでは

file.cpp:88:9: error: 'output_name2' was not declared in this scope

'output_name2'が定義されていない、と言うエラーです

file.cpp:88:53: error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const char*' for argument '1' to 'std::__cxx11::string path_thread2(const char*)'

関数の引数の型があっていない、と言うエラーです

投稿2020/10/03 04:42

編集2020/10/03 05:01
y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

trafalbad

2020/10/03 05:36

出力結果でなぜ2つ発生するのでしょうか? ご教授お願いします。
SHOMI

2020/10/03 05:59

質問内容は消さずに追記しないと回答が意味不明になりますよ
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問