29

First, Parameter.h:

#pragma once
#include <string>

class Parameter {
public:
    Parameter();
    ~Parameter();

private:
    string constValue;
    string varName;
};

And Parameter.cpp:

#include "Parameter.h"

using namespace std;

Parameter::Parameter() {};
Parameter::~Parameter() {};

I've brought these two files down to the barest of bones to get the errors that seem to be popping up. At the two private declarations for strings, I get the two errors:

'constValue': unknown override specifier
missing type specifier - int assumed. Note: C++ does not support default-int

I've seen several questions with these errors, but each refers to circular or missing references. As I've stripped it down to what's absolutely required, I can see no circular references or references that are missing.

Any ideas?

vilequarter
  • 393
  • 1
  • 3
  • 4
  • 4
    The name should be `std::string`. – Pete Becker Feb 02 '16 at 00:13
  • In all the (working) classes I've written, I've never put in the `std::`, instead relying on the `using namespace std;` in the .cpp file. Why would this one be any different? – vilequarter Feb 02 '16 at 00:36
  • 2
    The using declaration has to come before the use of the name. – Pete Becker Feb 02 '16 at 00:48
  • 1
    @David Parady: No, `using namespace std;` in `.cpp` file, located as in your example above, has no effect on header files. Apparently in your other "classes you've written" you did it differently. – AnT stands with Russia Feb 02 '16 at 00:53
  • 1
    TL;DR `override specifier` and the rest is a red herring. A more informative error message would have been: `'constValue' unknown.` – Bob Stein Mar 29 '19 at 20:00

1 Answers1

27

As @Pete Becker points out in the comments, you need to qualify the name string as std::string:

private:
    std::string constValue;
    std::string varName;

The compiler just doesn't know what you're talking about, and it's the equivalent of just writing:

SomeGreatType myMagicalUniversalType

The compiler just doesn't know what type that is unless you've declared, hence the error

missing type specifier - int assumed

You should read up about why you should avoid using namespace std;.

With regards to your question in the comments:

In all the (working) classes I've written, I've never put in the std::, instead relying on the using namespace std; in the .cpp file. Why would this one be any different?

I can only infer that at some point before including "Parameter.h" that you had a using namespace std. E.g.:

// SomeType.h

#using namespace std

...

// Parameter.cpp
#include "SomeType.h"
#include "Parameter.h"

The compiler compiles things top-to-bottom, and including essentially just replaces the #include with the contents of that file

Community
  • 1
  • 1
Tas
  • 7,023
  • 3
  • 36
  • 51
  • I think you're right, I had probably had some classes that included other classes that already had the `using namespace std` in my other working projects. I'll work on getting this fixed. – vilequarter Feb 02 '16 at 01:13
  • 3
    Do yourself a favour and [**avoid `#using namespace std`**](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Tas Feb 02 '16 at 01:14
  • Even if you are avoiding #using namespace std you may want to always use the std string, in which case you do #using std::string; and then you'll only have to type string and it'll know which you want. – Heather Dec 04 '18 at 14:34