• ベストアンサー

C++の名前空間について

C++学習者です。 Visual Studio 2015 を使ってアブストラクトクラスを作ろうとしています。 教本どうりにコードを入力したのですが、間違いを表す赤い波線が出てきました。 添付してある写真のように、名前空間std にはstring が定義されていないようです。 ヘッダーファイル<string.h> をstd の一部として認識させるにはどうすればよいのでしょうか? 詳しい方、どうか教えてください。お願いします。

質問者が選んだベストアンサー

  • ベストアンサー
  • wormhole
  • ベストアンサー率28% (1619/5653)
回答No.1

namespaceを使用する場合、includeするのはstring.hではなくstringだったかと。 #include <string>

papashiroSooke
質問者

お礼

早速の回答、有難うございます。 自分の入力ミスでした。

関連するQ&A

  • C++で>>演算子のオーバーロード

    C++学習者です。 Visual Studio Community 上で、ある教本を使って勉強しています。 現在Stringというクラスを作って、文字列に対して連結や部分文字列の取り出しなどができるようにするための色々な演算子のオーバーロードをする関数を定義していますが、疑問点がありますので、お聞きしたいと思います。 Stringクラスのプライベート変数は、文字列の長さを表すlength と、new 演算子で動的に確保するメモリー領域の始まりのアドレスを表す *sPtr の二つです。 クラス内ではパブリックなメンバー関数としていろいろな演算子がオーバーロードされていて、これらについてはよく理解できるのですが、friend 関数として定義されている入力演算子(>>)について納得がいかない部分があります。 その関数は次のようになっています。 istream &operator>>(istream &input, String &s) { char temp[100]; input >> setw(100) >> temp; s = temp; return input; } わからないのは s = temp; の部分です。 sはStringクラスのオブジェクトで、temp は単なる文字列なのに、なぜ代入できるのでしょうか? 代入演算子=のオーバーロード関数も下に挙げますが、この中でも単なる文字列をStringクラスのオブジェクトに代入できるようにはなってないように見えます。 const String &String::operator=(const String &right) { if (&right != this){ // avoid assignment of itself delete [ ] sPtr; length = right.length; sPtr = new char[ length + 1]; strcpy(sPtr, right.sPtr); } else cout<< "attempted to assign a String to itself \n\n"; return *this; } どなたか答えて頂けると有難いです。

  • C++について。

    現在”猫でもわかるプログラミング”のC++編をSDKと共に勉強している身です。 現在第22章、第23章を勉強中です。 22章 http://www.kumei.ne.jp/c_lang/cpp/cpp_22.htm 23章 http://www.kumei.ne.jp/c_lang/cpp/cpp_23.htm 作業環境はVisual Studio 2005.net C++です 22章でのプログラムを作成し、実行した結果エラーが出てしまいました。 ソースです #include <iostream> class xy_position { int x; int y; public: xy_position(int x = 0, int y = 0){ xy_position::x = x; xy_position::y = y; } int X() const {return x;} int Y() const {return y;} }; ostream& operator << (ostream& o, const xy_position& p) { return o << "(" << p.X() << "," << p.Y() << ")"; } int main(void) { xy_position a(50, 60), b; std::cout << a << b << std::endl; return 0; } (17):error C2143: 構文エラー : ';' が '&' の前にありません。 (17):error C4430: 型指定子がありません - int と仮定しました。メモ: C++ は int を既定値としてサポートしていません (17):error C2065: 'o' : 定義されていない識別子です。 (17):error C2059: 構文エラー : 'const' (18):error C2143: 構文エラー : ';' が '{' の前にありません。 (18):error C2447: '{' : 対応する関数ヘッダーがありません (旧形式の仮引数リスト?) (26):error C2679: 二項演算子 '<<' : 型 'xy_position' の右オペランドを扱う演算子が見つかりません (または変換できません)。 このようなエラーが出てしまいました。 もちろんソースは全て同様に書いています。 この”猫でも”で使用しているコンパイラと異なるために出たエラーでしょうか? それに単に cout << のように記述するとエラーが出てしまい、 std::cout << のように記述しなければ通りません。 また、エラーとは別の質問になってしまいますが、プログラム中に int X() const {return x;} という記述がありますが、このconstの意味が分かりません。 単純に return x が変更不可能という意味でしょうか? 次に23章についての質問です。 ここでもソースは同じなのに以下のようなエラーが出てしまいます。 ソースです。 #include <iostream> int main(void) { int x=10, y=15, z=20; std::cout << "16進表示" << std::endl; std::cout.setf(ios::hex); std::cout << x << std::endl; std::cout << y << std::endl; std::cout << z << std::endl; std::cout.unsetf(iostream::hex); std::cout << "8進数" << std::endl; std::cout.setf(ios::oct); std::cout << x << std::endl; std::cout << y << std::endl; std::cout << z << std::endl; return 0; } (8):error C2653: 'ios' : 識別子がクラス名でも名前空間名でもありません。 (8):error C2065: 'hex' : 定義されていない識別子です。 (13):error C2653: 'iostream' : 識別子がクラス名でも名前空間名でもありません。 (15):error C2653: 'ios' : 識別子がクラス名でも名前空間名でもありません。 (15):error C2065: 'oct' : 定義されていない識別子です。 これも何か設定をしなければいけないのでしょうか? なにぶんC++は・・・というかオブジェクト指向の言語は初心者なもので疑問も多いですorz

  • C++のvectorについて教えてください。

    C++のvectorについて教えてください。 現在悩んでいる問題について簡単に説明するために、テストコードを書きました。 #include <vector> class IntType { private: int num; public: IntType( int n ):num( n ){}; }; std::vector< IntType > IntVector; void main() { } このコードをDebug版でコンパイルすると 1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(285) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : テンプレート 引数を 'const std::reverse_iterator<_RanIt> &' に対して 'const size_t' から減少できませんでした 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2236) : 'std::operator <' の宣言を確認してください。 のようなエラーが出ます。 しかし、Release版でコンパイルするとコンパイルは通ります。 Debug版でコンパイルを通すにはどのようなコードを追加すればよいでしょうか? 知恵を貸してください。 よろしくおねがいします。 /** VisualStudio2008 AcademiEdition */

  • C++ String クラスについて

    こんにちわ。 C++のStringクラスについて質問です。 #include <iostream.h> #include <string.h> struct DATA { string word; int *count; }; ↑の様な形でストリング型のwordを定義したいのですが、うまくいきません・・・ 私は Microsoft Visual C++ 6.0 を使っています。 コンパイラによって、上の様な定義ができなかったりするのでしょうか? char word[10]; の様にしたほうがいいのでしょうか? string word; と char word[10]; では、後々どのような違いが生じてくるでしょうか? つまらない質問ですみません。よろしくおねがいします。

  • C言語のヘッダファイル

    前回のC言語のコンパイラの質問で、たくさんのご回答ありがとうございました。 おかげさまでコンパイラはなんとかなりそうですが、ひょっとしたらまた同じ質問を載せるかもしれません(^^; 今回はC言語のヘッダファイルを自分で作ってみたいんですが、よくわかりません。 microsoft visual studioのvisual c++付属のINCLUDEフォルダにある、stdio.hを開いてみたのですが、基本のprintf()やscanf()なども、どう定義されているか わからず終いでした。本などでもいまいち詳しく書いていなく(そう思うのは僕だけ?)、困っています。c言語のヘッダファイルとc++のヘッダファイルは やっぱり別ものなんでしょうか?ご回答おまちしております。

  • C/C++関数間でのStringクラスの扱い

    以下のようなコードを実行してみましたが思い通りに動いてくれません. "sample"という文字列がstrへとコピーされると思ったのですが. stringクラスのc_str()メソッドはconst char*だと言っているので無理矢理キャストしたのが原因でしょうか.stringクラスは記憶領域を自動で変更してくれるのではないのですか.それともこの挙動は仕様ですか. -------- 以下コード -------- #include <iostream> #include <string> using namespace std; int func(char *); int main(void) {     string str("");     func((char *)str.c_str());     cout << "String: " << str << endl;     return EXIT_SUCCESS; } int func(char *buf) {     buf = "sample";     return 0; } -------- 以上コード --------

  • リリースモードの時にリンカエラーが発生します

    リリースモードの時にリンカエラーが発生します Visual Studioで、自作ライブラリを使ったプログラムの開発をしていたのですが、リリースモード時にリンカエラーが発生してしまいました。 デバッグモード時は特に問題なくリンクできるのですが、リリースモードでコンパイルをすると以下のようなエラーが発生します。 LibGame.lib(Game.obj) : error LNK2001: 外部シンボル ""bool __cdecl FileExists(class std::basic_string,class std::allocator >)" (?FileExists@@YA_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)" は未解決です。 LibGame.lib(Mesh.obj) : error LNK2001: 外部シンボル ""public: struct ARCHIVE_ENTRY * __thiscall CArchive::Find(class std::basic_string,class std::allocator >)" (?Find@CArchive@@QAEPAUARCHIVE_ENTRY@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)" は未解決です。 LibGame.lib(Mesh.obj) : error LNK2001: 外部シンボル ""class std::basic_string,class std::allocator > __cdecl ExtractFilePath(class std::basic_string,class std::allocator >)" (?ExtractFilePath@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@@Z)" は未解決です。 Release/ShtGame.exe : fatal error LNK1120: 外部参照 3 が未解決です。 プロジェクトのプロパティ等でリンク先のライブラリを有無やヘッダをチェックしたのですが、原因がつかめませんでした。 どのようなエラー原因が考えられるでしょうか? よろしくお願いします OS:Windows XP SP3 言語:VC++ 開発環境:Visual Studio 2005 Academic Edition

  • ヘッダーファイルがインクルードされない(?)

    ダイアログ上のボタンをクリックすると指定されたテキストファイルをオープンし、 ファイルの内容をスペースで区切って格納するプログラムを http://oshiete1.goo.ne.jp/kotaeru.php3?q=474452 こちらを少し変えて作ったのですが、 コンパイル・デバッグを行なうと 'std' : 識別子がクラス名でも名前空間名でもありません。 'vector' : 定義されていない識別子です。 'ofstream' : 定義されていない識別子です。 と言ったようなエラーになってしまいます。 必要なヘッダーファイルはインクルードされているはずなのですが・・・。 この原因は何故でしょうか? /**********プログラムソース**********/ #include <iostream> #include <fstream> #include <string> #include <vector> #include <utility> #include "stdafx.h" #include "SCHEDULE.h" #include "SCHEDULEDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // ~中略~ void CSCHEDULEDlg::OnReq() {    typedef std::pair<std::string,std::string> item;    std::vector<item> participation;    std::ofstream file(dataFile.txt);    std::string line;    while ( std::getline(file,line) ) {      std::string::size_type pos = line.find(' ');      participation.push_back(item(line.substr(0,pos), line.substr(pos+1)));    }    for ( int i = 0; i < participation.size(); ++ i) {      std::cout << "result = " << participation[j] << "]\n";    } }

  • Visial C++におけるプログラミング

    Visial C++ 2008を使用したプログラミングについてです。 学校の課題をやるためやむなくVisial C++2008を使用していて 学校では実行が出来たのに家では出来ませんでした。 ソースファイルは以下のとおりです。 #include <stdio.h> #include <stdlib.h> #include <time.h> /*構造体宣言*/ struct Students{ int N; int A; int B; int C; }; int main(void){ struct Students std[50]; /*構造体型配列*/ int i,N,A,B,C,num,scannum; srand((unsigned)time(NULL)); /*乱数の初期化*/ FILE *file; /*ファイルのポインタを用意*/ file=fopen("Data.txt","r"); /*Dataファイルの読み込み*/ /*ファイルのオープンチェック*/ if(file==NULL){ fprintf(stderr,"cannnot open file 'Data.txt'\n"); exit(1); } /*Studentsにデータを格納*/ for(i=0;i<=49;i++){ fscanf(file,"%d%d%d",&N,&A,&B); std[i].N=N; std[i].A=A; std[i].B=B; std[i].C=70+(rand()/(RAND_MAX+1.0)*31); /*表示*/ printf("学籍番号:%d.",std[i].N); printf("科目A:%d.\n",std[i].A); printf("科目B:%d.\n",std[i].B); printf("科目C:%d.\n",std[i].C); } fclose(file); return 0; } エラーとしては 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(21) : error C2275: 'FILE' : この型は演算子として使用できません 1> c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(69) : 'FILE' の宣言を確認してください。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(21) : error C2065: 'file' : 定義されていない識別子です。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(23) : error C2065: 'file' : 定義されていない識別子です。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(23) : warning C4047: '=' : 間接参照のレベルが 'int' と 'FILE *' で異なっています。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(26) : error C2065: 'file' : 定義されていない識別子です。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(26) : warning C4047: '==' : 間接参照のレベルが 'int' と 'void *' で異なっています。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(32) : error C2065: 'file' : 定義されていない識別子です。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(32) : warning C4047: '関数' : 間接参照のレベルが 'FILE *' と 'int' で異なっています。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(32) : warning C4024: 'fscanf' : の型が 1 の仮引数および実引数と異なります。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(36) : warning C4244: '=' : 'double' から 'int' への変換です。データが失われる可能性があります。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(45) : error C2065: 'file' : 定義されていない識別子です。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(45) : warning C4047: '関数' : 間接参照のレベルが 'FILE *' と 'int' で異なっています。 1>c:\documents and settings\devil\my documents\visual studio 2008\projects\テスト\テスト\tt.c(45) : warning C4024: 'fclose' : の型が 1 の仮引数および実引数と異なります。 1>ビルドログは "file://c:\Documents and Settings\devil\My Documents\Visual Studio 2008\Projects\テスト\テスト\Debug\BuildLog.htm" に保存されました。 と表示されます。FILEの宣言をしているのになんで確認してください と出るのでしょうか。。。。解決にご協力お願いします。m(__)m

  • C++, strcpy の warning.

    VC2005 Express Edition を使ってます。 #include <iostream> #include <string> using namespace std; int main() {   char str[10] = "hello";   strcpy(str, "HELLO"); } ↑ こちらをコンパイルすると hello.cpp .\hello.cpp(8) : warning C4996: 'strcpy' was declared deprecated C:\Program Files\Microsoft Visual Studio 8\VC\include\string.h(73) : see declaration of 'strcpy' という警告が出るのはどうしてでしょうか? #include <string.h> も試したのですが同じ警告でした。

専門家に質問してみよう