-2

So I have the following code in a header file named Classes.h:

#ifndef CLASSESS_H
#define CLASSESS_H

class PalindromeCheck
{
private:
    string strToCheck;
    string copy;

public:
    PalindromeCheck(string testSubject) : strToCheck(testSubject) {} //Constructor

    void Check()
    {
        copy = strToCheck; //Copy strToCheck into copy so that once strToCheck has been reversed, it has something to be checked against.
        reverse(strToCheck.begin(), strToCheck.end());  //Reverse the string so that it can be checked to see if it is a palindrome.

        if (strToCheck == copy) 
        {
            cout << "The string is a palindrome" << endl;
            return;
        }
        else 
        {
            cout << "The string is not a palindrome" << endl;
            return;
        }
    }
};

#endif

And now I have the following code in a source file:

#include <iostream>
#include <string>
#include <algorithm>
#include "Classes.h"
using namespace std;

int main()
{
    PalindromeCheck firstCheck("ATOYOTA");

    firstCheck.Check();

    return 0;
}

When I compiled this code using the Visual C++ Compiler, I got a ton of errors messages that all stemmed from these first four:

'strToCheck': unknown override specifier missing type specifier - int assumed. 'copy': unknown override specifier missing type specifier - int assumed.

I tried adding in #include <string> into the header file and recompiled it but it did absolutely nothing. This confuses me because I thought I could use a string as a datatype, but apparently not in a class? It would be great if someone could help me out because I don't know why my code isn't working.

Hayden
  • 41
  • 7
  • 2
    Get rid of `using namespace std` and replace `string` with `std::string` and your problems (including the ones you didn't realize you had) will go away. – David Schwartz Jul 28 '17 at 15:40
  • The `using namespace std;` comes after `#include "Classes.h"`. Thus, when defining `class PalindromCheck` the compiler tries to find `string` in the global namespace but it cannot be found. (Not yet.) Solution is provided in answer... – Scheff's Cat Jul 28 '17 at 15:40

1 Answers1

7

You need to #include <string> in the class header itself.

You also need to either use the std:: namespace (preferable) or also add using namespace std to that header as well (which I strongly discourage).

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • So post your latest code in your question, not some old version. Otherwise, of course people will point out the obvious problems, which you've apparently since fixed. And that's a waste of everyone's time. – underscore_d Jul 28 '17 at 15:39
  • Awesome! Thank you! I didn't see the last part of your answer until after I posted that comment, so I deleted it. My code works now! Thanks! – Hayden Jul 28 '17 at 15:46