A string inside of a struct inside of a class


Keywords:c++ 


Question: 

I can't seem to get the following code to compile. If I replace the all the string references with a char * it will compile and run fine. I am using Visual Studio 2013. What am I missing? I have spent several hours trying to figure this out.

These are some of the compile errors: Error 1 error C2146: syntax error : missing ';' before identifier 'ss' c:\users\visual studio 2013\projects\class struct test\class struct test\class struct test.cpp 16 1 Class Struct Test

Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\visual studio 2013\projects\class struct test\class struct test\class struct test.cpp 16 1 Class Struct Test

Thanks in advance.

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

2 Answers: 

You probably meant to use the standard library string. This is located in the std namespace. Try this:

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


You can use using namespace std at the beginning of the block.