2016-04-19 24 views
1

私はVisual C++ 2008 Express EditionでC++プログラミングを学んでいます。私のプログラムはstd::stringデータ型を識別していません。私は<string>ライブラリを含んでいます。私はクラスの長方形で文字列型のプライベート変数を宣言するときC++の文字列データ型が認識されない

#include "stdafx.h" 
#include <iostream> 
#include "TestClass.h" 
#include <string> 

using namespace std; 

class Rectangle { 
private: 
    int width; 
    int height; 
    double area; 
    string name; 

public: 
    Rectangle() 
    { 
     width = 0; 
     height = 0; 
    } 

    int getWidth() 
    { 
     return width; 
    } 

    int getHeight() 
    { 
     return height; 
    } 

    void setWidth(int width) 
    { 
     this->width = width; 
    } 

    void setHeight(int height) 
    { 
     this->height= height; 
    } 

    void setArea() 
    { 
     area = width * height; 
    } 

    double getArea() 
    { 
     return area; 
    } 

    Rectangle (const Rectangle & x) 
    { 
     this->area = x.area; 
    } 

    void friendtestfunction(Rectangle2 s) 
    { 
     cout << "" << s.name; 
    } 
}; 

int Rectangle2::stat_var = 5; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Rectangle rect; 
    rect.setWidth(10); 
    rect.setHeight(20); 
    rect.setArea(); 

    cout<< "height is equal to :" << rect.getHeight() << endl; 
    cout<< "width is equal to :" << rect.getWidth() << endl; 
    cout << "area is equal to :" << rect.getArea() << endl; 

    getchar(); 
    return 0; 
} 

それは次のエラーを示した:

1>c:\users\subith.p\documents\visual studio 2008\projects\test\test\testclass.h(10) : error C2146: syntax error : missing ';' before identifier 'name' 

1>c:\users\subith.p\documents\visual studio 2008\projects\test\test\testclass.h(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
+0

はstdafx.hを失います。以前の実行がキャッシュされ、変更してもクリアされない場合は、後で再コンパイルするときに変更が表示されません。 – xaxxon

+0

コンパイラは、ソースファイルの先頭から開始し、コンパイラがソースファイルの一番下で停止して停止するまで下がります。だからまだ何か(#include のような)を見たことがないなら、それを使用しようとするとそれについてはわかりません。ファイル全体を "見て"何が起きているのか把握していないし、コードをコンパイルしてコンパイルします。これはコードを1回だけ処理するため、「シングルパスコンパイラ」と呼ばれています。 – xaxxon

答えて

6

は、あなたのヘッダファイルtestclass.h#include <string>を追加します。

+0

あなたはそれがどう変わると思いますか? – xaxxon

+0

これは、エラーメッセージに表示される内容です。 – ZDF

+0

おっと、そうです。ソースリストに行番号がなくても、どのstd :: stringが参照されているのかがはっきりとわかりませんでした。私は数えないと認めます。 – xaxxon

3

このエラー:

1> C:\ Users \ユーザーsubith.pドキュメントは、Visual Studio 2008 \プロジェクト\テスト\テスト\のtestclass.hを\ \(10):エラーC2146:構文エラー:見つかりません「; '識別子 'name'の前に

は、問題がtestclass.hヘッダーファイルにあることを示しています。

最も可能性の高い理由は、あなたのTestClass.hは、<string>から含まれ、あなたが含まを再編成する必要があります参照#include "TestClass.h"#include <string>を移動しないことです:#include <string>を追加

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include "TestClass.h" 

以上お奨めすると、共通のaproachとしてtestclass.hに送信してください。

+1

問題は正しいですが、あなたの解決策は脆弱です。彼は両方にを含めるべきであり、一方を他方の前に含めることに頼らないでください。 「使用するものを含める」http://include-what-you-use.org/ – xaxxon

+0

まあ、OPは実装ファイルのみを含むようにしておきたい... – marcinj

+1

私は、オリジナル化の欠如に基づいて推測していますが、そのような目標が存在しないこと。新しいユーザーに最も堅牢で信頼性の高いソリューションを提示することが、通常、最良のアプローチです。 – xaxxon

関連する問題