Hi to all,
Hope you all will be fine. I am new to C++. Although i have read the basics but i am a newbie to C++.
Now i want to learn how to use class in C++. For this i made a simple class, assign variables and simply try to assign values to variables. But i got errors
I wrote very simple code
Error that i got in the caseCode:class Test { public: Test(char* firstNamePointer, char lastName[]); Test(const Test& orig); virtual ~Test(); int item; char *firstNamePointer; char lastName[10]; // string middleName; private: }; Test::Test(char* firstNamePointer, char lastName[]) { this->firstNamePointer = firstNamePointer; this->lastName = "Mahmood"; //this->lastName = lastName; } Test::Test(const Test& orig) { } Test::~Test() { } int main(int argc, char** argv) { Test *test = new Test("Basit", "Mahmood"); test->item = 5; //test->middleName = "Ahmed" cout << (*test).item << endl; //ok cout << test->item << endl; //ok cout << endl; cout << (*test).firstNamePointer << endl; //ok cout << test->firstNamePointer << endl; //ok cout << endl; cout << (*test).lastName << endl; cout << test->lastName << endl; return (EXIT_SUCCESS); }
isCode:Test::Test(char* firstNamePointer, char lastName[]) { this->firstNamePointer = firstNamePointer; this->lastName = "Mahmood"; //error }
and when i use this codeTest.cpp:13: error: incompatible types in assignment of `const char[8]' to `char[10]'
then the error isCode:Test::Test(char* firstNamePointer, char lastName[]) { this->firstNamePointer = firstNamePointer; this->lastName = lastName; //error }
Also when i try to use string in my code like thisTest.cpp:14: error: incompatible types in assignment of `char*' to `char[10]'
why it saying unresolved identifier string even i am including file <string.h>.Code:#include <string.h> //also try <string> and <strings.h> class Test { public: Test(char* firstNamePointer, char lastName[]); Test(const Test& orig); virtual ~Test(); int item; char *firstNamePointer; char lastName[10]; // char lastName[10] = "Ahmed"; string middleName; //Unable to resolve identifier string private: };
How can i use string in my class.
Please tell what i am doing wrong
thanks




Reply With Quote