C++ 类内部结构内部的字符串

C++ 类内部结构内部的字符串,c++,string,class,struct,C++,String,Class,Struct,我似乎无法编译以下代码。如果我用char*替换所有字符串引用,它将编译并运行良好。我正在使用Visual Studio 2013。我错过了什么?我花了几个小时试图弄明白这一点 以下是一些编译错误: 错误1错误C2146:语法错误:缺少“;”在标识符“ss”之前c:\users\visualstudio 2013\projects\class struct test\class struct test\class struct test.cpp 16 1 class struct test 错误2

我似乎无法编译以下代码。如果我用char*替换所有字符串引用,它将编译并运行良好。我正在使用Visual Studio 2013。我错过了什么?我花了几个小时试图弄明白这一点

以下是一些编译错误: 错误1错误C2146:语法错误:缺少“;”在标识符“ss”之前c:\users\visualstudio 2013\projects\class struct test\class struct test\class struct test.cpp 16 1 class struct test

错误2错误C4430:缺少类型说明符-假定为int。注意:C++不支持默认INT:\ValueStudio\\VisualStudio 2013 \项目\类StultTest.Byt结构体测试\类Strut Test.CPP 16 1级结构测试< /P> 提前谢谢

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

class test
{
public:
    struct structType
    {
        int int1;
        int int2;
        string ss;
    };

public:
    int getint1();
    int getint2();
    string getString();
    test()
    {
        privateVar.int1 = 5;
        privateVar.int2 = 6;
        privateVar.ss = "This is test string 1";
    };
    ~test(){};

private:
    structType privateVar;
};

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    test t;

    cout << "Int 1:  " << t.getint1() << endl;
    cout << "Int 2:  " << t.getint2() << endl;
    cout << "String:  " << t.getString() << endl;
};

int test::getint1()     { return privateVar.int1;}
int test::getint2()     { return privateVar.int2;}
string test::getString(){ return privateVar.ss; }
#包括“stdafx.h”
#包括
#包括
课堂测试
{
公众:
结构类型
{
int int1;
int int2;
字符串ss;
};
公众:
int getint1();
int getint2();
字符串getString();
测试()
{
privateVar.int1=5;
privateVar.int2=6;
privateVar.ss=“这是测试字符串1”;
};
~test(){};
私人:
结构类型privateVar;
};
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
试验t;

cout您可能打算使用标准库字符串。该字符串位于
std
命名空间中。请尝试以下操作:

struct structType {
    int int1;
    int int2;
    std::string ss;
};

您可以在块的开头使用
using namespace std

std::string
在名称空间中,而不是全局的。您的代码示例可以简化为
#include string s;
,以演示相同的错误。编译器实际上也很好地处理了这个问题。您可以使用
namespace std
JUt before
\u tmain
。您可以将其移到文件的顶部,或者使用
std::string
string ss;
应该是
std::string ss;
谢谢。将namespace语句移到文件的顶部是我所缺少的。我花了很多时间与谷歌一起来理解这一点。@user3784804,或者,。您是正确的t、 这是一个名称空间问题。Chris解决了。-1一般来说,这不是一个很好的建议。最好明确名称空间,或者使用using语句消除本地使用的歧义(例如,
使用std::cout LocalStdout;
),谢谢你的建议!