0

I'm not sure what I did wrong, I'm pretty new to c++ and I included the "string" library and using std. but I still get an error, I didn't include all my code, since its unnecessary

ERROR

./year.h:25:5: error: unknown type name 'string'; did you mean 'std::string'?
    string monthStr();
    ^~~~~~
    std::string

CODE SOURCE

#include <iostream>
#include <iomanip>
#include <string>
#include "Year.h"

using namespace std;
string Year::monthStr()
{
    if (m >= 1 && month <= 12)  return monthStrings[month - 1];
    else                        return "Unknown";
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 1
    try std::string intread of string – ponayz Sep 29 '18 at 19:26
  • 3
    You need to [completely forget that "using namespace std;" exists in the C++ language](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). It's just a bad dream. – Sam Varshavchik Sep 29 '18 at 19:28
  • 1
    Most certainly the declaration in your `year.h` looks like this `string monthStr()` (the error `./year.h:25:5` tells you that the problem is in the `year.h` file). So you should change it to `std::string monthStr()` there (never ever add `using namespace std;` to a `.h` file). – t.niese Sep 29 '18 at 19:29
  • But can I just not have "string monthStr()"? – decentProgrammer Sep 29 '18 at 19:30
  • You can, but you really should write `std::string` at least in your header. And if you really want to use `using namespace` in a `.cpp` file then never before a `#include` – t.niese Sep 29 '18 at 19:35

2 Answers2

3

Your error is not in that part of the code. It's in the Year.h, check it out.

0

It could be a problem with your year.h header file where you might forgot to use using namespace std, Ideally it is good to avoid adding using namespace stuff to avoid namespace pollution

Just Use: std::string

asim
  • 452
  • 4
  • 12
  • I did include "using namespace std;" at my header file still doesn't work. Is there another way instead of me using "std::string" ? – decentProgrammer Sep 29 '18 at 19:30