不识别c ++中的字符串数据类型

时间:2016-04-19 11:02:12

标签: c++ string visual-studio-2008 header

我正在学习Visual C ++ 2008中的C ++编程,快速版。我的程序未识别std::string数据类型。我已经加入了<string>库。

#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;
}

当我在类Rectangle中声明类型为string的私有变量时,它显示以下错误:

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

2 个答案:

答案 0 :(得分:6)

#include <string>添加到您的标题文件testclass.h

答案 1 :(得分:3)

此错误:

1&gt; c:\ users \ subith.p \ documents \ visual studio 2008 \ projects \ test \ test \ testclass.h(10):错误C2146:语法错误:缺少';'在标识符'name'之前

表示问题出在imageLibrary标题文件中,您尚未向我们展示。

最可能的原因是,您的testclass.h未看到TestClass.h中的内容,您应该重新组织您的内容,将<string>移到#include <string>之上:

#include "TestClass.h"

或作为更加推荐和常见的方法,将#include "stdafx.h" #include <iostream> #include <string> #include "TestClass.h" 添加到#include <string>