-2

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; }
user3784804
  • 35
  • 1
  • 1
  • 8
  • 10
    `std::string` is inside a namespace, not global. Your code sample could be reduced to just `#include string s;` to demonstrate the same error. Compilers are actually pretty [helpful](http://coliru.stacked-crooked.com/a/7bbe4c3a4162853f) with this one, too. – chris Jun 27 '14 at 22:23
  • You are have `using namespace std` just before `_tmain`. You can move that to the top of the file, or use `std::string`. – R Sahu Jun 27 '14 at 22:25
  • `string ss;` should be `std::string ss;` – shuttle87 Jun 27 '14 at 22:26
  • Thanks. Moving the namespace statement to the top of the file is what I was missing. Spent many hours with google to understand this one. – user3784804 Jun 27 '14 at 22:33
  • 2
    @user3784804, Alternatively, [take it out](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c). – chris Jun 27 '14 at 22:36
  • @user3784804 Avoid this `using namespace std;` statement at all and be explicit (like e.g. `std::string`). This will help to avoid future trouble for you. – πάντα ῥεῖ Jun 28 '14 at 00:05
  • How is this question off topic? This is a c++ forum is it not? Sure the example is a little wordy, but off topic? The problem was a C++ scoping error with string declaration. I thought that string was global once the header file was included. I was wrong but I will bet that I am not the last person to make that mistake. – user3784804 Jul 02 '14 at 23:32

2 Answers2

3

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;
};
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
yizzlez
  • 8,757
  • 4
  • 29
  • 44
-1

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

Good
  • 306
  • 1
  • 17
  • 1
    -1 That's not a really good advice in general. It's better to be explicit about namespaces, or have using statements to disambiguate for your local uses (e.g. `using std::cout LocalStdout;`) – πάντα ῥεῖ Jun 28 '14 at 00:14