无法识别c+中的字符串数据类型+; 我在Visual C++ 2008,Express版中学习C++程序设计。我的程序没有识别std::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; }

无法识别c+中的字符串数据类型+; 我在Visual C++ 2008,Express版中学习C++程序设计。我的程序没有识别std::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; },c++,string,visual-studio-2008,header,C++,String,Visual Studio 2008,Header,将#include添加到头文件testclass.h将#include添加到头文件testclass.h此错误: 1> c:\users\subith.p\documents\visual studio 2008\projects\test\test\testclass.h(10):错误C2146:语法错误:缺少“;”在标识符“name”之前 表示问题出现在testclass.h头文件中,您尚未向我们显示该文件 最可能的原因是您的TestClass.h没有从中看到include,您应该重新组织i

#include
添加到头文件
testclass.h

#include
添加到头文件
testclass.h

此错误:

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

表示问题出现在
testclass.h
头文件中,您尚未向我们显示该文件

最可能的原因是您的
TestClass.h
没有从
中看到include,您应该重新组织include,将
#include
移到
#include“TestClass.h”

#包括“stdafx.h”
#包括
#包括
#包括“TestClass.h”

或者,作为一种更为推荐和常见的方法,将
#include
添加到您的
testclass.h

此错误:

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

表示问题出现在
testclass.h
头文件中,您尚未向我们显示该文件

最可能的原因是您的
TestClass.h
没有从
中看到include,您应该重新组织include,将
#include
移到
#include“TestClass.h”

#包括“stdafx.h”
#包括
#包括
#包括“TestClass.h”



或者,作为一种更为推荐和常见的方法,将
#include
添加到您的
testclass.h

中,丢失stdafx.h。这会缓存以前的运行,如果您更改了内容但未清除,则在以后重新编译时将看不到更改。需要记住的一点是,编译器从源文件的顶部开始,向下运行,直到到达底部并停止。因此,如果它还没有看到一些东西(比如a#include),当你尝试使用它时,它就不知道了。它不会“浏览”整个文件以了解发生了什么,然后返回并编译代码。它被称为“单过程编译器”,因为它只对代码进行一次检查。这会缓存以前的运行,如果您更改了内容但未清除,则在以后重新编译时将看不到更改。需要记住的一点是,编译器从源文件的顶部开始,向下运行,直到到达底部并停止。因此,如果它还没有看到一些东西(比如a#include),当你尝试使用它时,它就不知道了。它不会“浏览”整个文件以了解发生了什么,然后返回并编译代码。它被称为“单过程编译器”,因为它只对代码进行一次检查。你认为会发生什么变化?哦,你说得对。如果源代码列表中没有行号,那么引用哪个std::string就不那么明显了。我承认我没数数。你认为会有什么变化?哦,你说得对。如果源代码列表中没有行号,那么引用哪个std::string就不那么明显了。我承认我没数数。你的问题是对的,但你的解决方案很脆弱。他应该把两者都包括在内,而不是依赖于一个先包括另一个。“包括你所使用的”我猜测,基于缺乏组织,这样的目标是不存在的。向新用户提供最健壮、最可靠的解决方案通常是最好的方法,imho。@xaxxon好的,你补充道,实际上我曾经在做一个项目,所有的包含都应该放在实现文件中,这真是一个让人头疼的问题。如何创建一个包含字符串的类?所有的东西都必须是指针/ref,所有的类都必须预先声明吗?问题的答案是正确的,但解决方案是脆弱的。他应该把两者都包括在内,而不是依赖于一个先包括另一个。“包括你所使用的”我猜测,基于缺乏组织,这样的目标是不存在的。向新用户提供最健壮、最可靠的解决方案通常是最好的方法,imho。@xaxxon好的,你补充道,实际上我曾经在做一个项目,所有的包含都应该放在实现文件中,这真是一个让人头疼的问题。如何创建一个包含字符串的类?是否所有内容都必须是指针/ref和所有预先声明的类?
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include "TestClass.h"