Why won't C++ let me use a string as a data member in a class? -
so have following code in 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 copy once strtocheck has been reversed, has checked against. reverse(strtocheck.begin(), strtocheck.end()); //reverse string can checked see if palindrome. if (strtocheck == copy) { cout << "the string palindrome" << endl; return; } else { cout << "the string not palindrome" << endl; return; } } }; #endif
and have following code in source file:
#include <iostream> #include <string> #include <algorithm> #include "classes.h" using namespace std; int main() { palindromecheck firstcheck("atoyota"); firstcheck.check(); return 0; }
when compiled code using visual c++ compiler, got ton of errors messages stemmed 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>
header file , recompiled did absolutely nothing. confuses me because thought use string datatype, apparently not in class? great if me out because don't know why code isn't working.
you need #include <string>
in class header itself.
you need either use std::
namespace (preferable) or add using namespace std
header (which discourage).
Comments
Post a Comment