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

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

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

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

Q&A

解決済

2回答

1972閲覧

スコープに関すること

退会済みユーザー

退会済みユーザー

総合スコア0

C++

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

0グッド

0クリップ

投稿2018/10/08 14:50

編集2018/10/08 14:53

自作関数でファイル入力を行いmain関数で実際に入力されたものを出力していこうと思ったのですが、スコープの関係上main関数で出力できず困る羽目になってしまいました。アドバイスしてていただけると幸いです。

.\uho16.cpp:33:17: error: 'uhokun' was not declared in this scope cout << uhokun.bigin() + j << endl;

c++

1#include <iostream> 2#include <fstream> 3#include <sstream> 4#include <vector> 5#include <string> 6 7using namespace std; 8 9int i(0),x; 10 11 12void read_f(const string uho,vector<string> &uhokun){ 13 14 ifstream ifs(uho); 15 16 if(!ifs){ 17 cout << uho << "file no exist" << endl; 18 return ; 19 } 20 string str; 21 while(getline(ifs,str,'\n')){ 22 uhokun.push_back(str); 23 i++; 24 } 25 ifs.close(); 26} 27int main(){ 28 string x; 29 vector<string> y; 30 cin >> x; 31 read_f(x,y); 32 for(int j(0);i != j;j++){ 33 cout << uhokun.bigin() + j << endl; 34 } 35}

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

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

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

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

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

guest

回答2

0

ベストアンサー

いや、cout << &y + j << endl; はだめです、なぜなら読み込んだファイルが3行以上のとき、範囲の直後の要素のさらに次の要素にポインタを向けているため、未定義動作になるからです(有効でない領域へのポインタは配列の最終要素のすぐ次へを除いて存在するだけで未定義動作)。

というかポインタを表示するとかやっていることが謎。

cpp

1#include <iostream> 2#include <fstream> 3#include <vector> 4#include <string> 5 6std::vector<std::string> read_f(const std::string& uho){ 7 std::ifstream ifs(uho); 8 std::vector<std::string> re; 9 if(!ifs){ 10 throw std::runtime_error("file no exist"); 11 } 12 for(std::string str; std::getline(ifs,str,'\n');){ 13 re.emplace_back(std::move(str)); 14 } 15 return re; 16} 17int main(){ 18 std::string x; 19 std::cin >> x; 20 auto y = read_f(x); 21 for(auto&& y_e : y){ 22 std::cout << y_e << std::endl; 23 } 24}

https://wandbox.org/permlink/WK203Y0JiPo5cUtK

やるならこうでは?

投稿2018/10/08 16:45

編集2018/10/08 16:50
yumetodo

総合スコア5850

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

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

yumetodo

2018/10/08 16:59

もっというとstd::getlineの第三引数の区切り文字はデフォルトで'\n'なので省略していいな
退会済みユーザー

退会済みユーザー

2018/10/13 06:59

そうなんですね、了解です
guest

0

こんな感じにしたらコンパイル通りました!

c++

1#include <iostream> 2#include <fstream> 3#include <sstream> 4#include <vector> 5#include <string> 6 7using namespace std; 8 9int i(0),x; 10 11 12void read_f(const string uho,vector<string> &uhokun){ 13 14 ifstream ifs(uho); 15 16 if(!ifs){ 17 cout << uho << "file no exist" << endl; 18 return ; 19 } 20 string str; 21 while(getline(ifs,str,'\n')){ 22 uhokun.push_back(str); 23 i++; 24 } 25 ifs.close(); 26} 27int main(){ 28 string x; 29 vector<string> y; 30 cin >> x; 31 read_f(x,y); 32 for(int j(0);i != j;j++){ 33 cout << &y + j << endl; 34 } 35}

投稿2018/10/08 15:04

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

yumetodo

2018/10/08 16:51

未定義動作です
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問