CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2010
    Posts
    73

    Unhappy Problem in printing characters by using class

    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

    Code:
    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);
    }
    Error that i got in the case

    Code:
    Test::Test(char* firstNamePointer, char lastName[]) {
    
        this->firstNamePointer = firstNamePointer;
        this->lastName = "Mahmood";                        //error
    }
    is

    Test.cpp:13: error: incompatible types in assignment of `const char[8]' to `char[10]'
    and when i use this code

    Code:
    Test::Test(char* firstNamePointer, char lastName[]) {
    
        this->firstNamePointer = firstNamePointer;
        this->lastName = lastName;                             //error
    }
    then the error is

    Test.cpp:14: error: incompatible types in assignment of `char*' to `char[10]'
    Also when i try to use string in my code like this

    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:
    
    };
    why it saying unresolved identifier string even i am including file <string.h>.
    How can i use string in my class.

    Please tell what i am doing wrong

    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Problem in printing characters by using class

    The problem is that you cannot assign to an array. You are heading towards a correct solution, i.e., to use std::string, but you need to #include <string> and qualify string as std::string.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: Problem in printing characters by using class

    I would suggest you go with the string for all "strings".

    use #include <string> //string.h isn't a C++ header.

    then use std::string (string is part of the std namespace)

    std::string middleName ;
    your humble savant

  4. #4
    Join Date
    Jul 2010
    Posts
    73

    Re: Problem in printing characters by using class

    Hmm, thanks. The string problem is solved now. But i want to ask something

    I think in the code

    Code:
    Test::Test(char* firstNamePointer, char lastName[], string middleName) {
    
        this->firstNamePointer = firstNamePointer;
       // this->lastName = "Mahmood";
       // this->lastName = lastName;                  //name = testArray; 
        this->middleName = middleName;
    }
    I am trying to do something like this

    Code:
    int main(int argc, char** argv) {
        
        char name[10];
      
        char testArray[20] = "Masood";
    
        name = testArray;    // this is what i am actually doing in my class( // this->lastName = lastName;)   error
        
        cout << name << endl;
    
         //char lastName[10] = "Masood";
    
        // cout << lastName << endl;
    }
    Is it?

    But when i change line like this

    Code:
    name[0] = testArray[0];
    then i got output like this

    Code:
    M[]"a/    //[] is not actually brackets it's something unknown
    I want to ask the output should be simple "M". what is this ([]"a/)

    second i want to ask why this statement is not working. I simply trying to assign value to public member field

    Code:
      test->item = 5;                     //ok
     
      test->lastName = "Masood";    //error why
    i think this is same as

    Code:
    char lastName[10] = "Masood";
    is test->lastName = "Masood"; can be interpreted something like this when i declared lastName variable in a class?

    Code:
    char * const lastName[10]
    that's why i am getting error like this

    Code:
    main.cpp:24: error: incompatible types in assignment of `const char[7]' to `char[10]'
    Thanks

  5. #5
    Join Date
    Jul 2010
    Posts
    73

    Re: Problem in printing characters by using class

    this is working

    [code]
    test->lastName[0] = 'M';
    test->lastName[1] = 'a';
    test->lastName[2] = 's';
    test->lastName[3] = 'o';
    /[code]

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Problem in printing characters by using class

    I already told you that you cannot assign to an array
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jul 2010
    Posts
    73

    Re: Problem in printing characters by using class

    Hmm now i understood . Actually i was confused about the working of code. I was mixing these code

    Code:
       char lastName[] = {'M', 'a', 's', 'o', 'o', 'd'};  //same as  char firstName[] = "Masood";
    
       char firstName[] = "Masood";
    
       char middleName[10];
    
       middleName = "Masood";   // main.cpp:38: error: incompatible types in assignment of `const char[7]' to `char[10]'
    And after understanding this, i came up with this and it's working very well.

    Code:
    #ifndef _TEST_H
    #define	_TEST_H
    
    #include <string>
    
    using namespace std;
    
    class Test {
    public:
        Test(char* firstNamePointer, char lastName[], string middleName);
        Test(const Test& orig);
        virtual ~Test();
    
        int item;
        char *firstNamePointer;
        char lastName[10] ;
        string middleName;
    
    private:
    
    };
    
    #endif	/* _TEST_H */
    
    #include "Test.h"
    
    Test::Test(char* firstNamePointer, char lastName[], string middleName) {
    
        this->firstNamePointer = firstNamePointer;
        strcpy(this->lastName, lastName);
        this->middleName = middleName;
    }
    
    Test::Test(const Test& orig) {
    }
    
    Test::~Test() {
    }
    
    #include <stdlib.h>
    #include <iostream>
    
    #include "Test.h"
    
    int main(int argc, char** argv) {
    
        Test *test = new Test("Basit", "Ahmed", "Mahmood");
    
        test->item = 5;                     //ok
        //test->middleName = "Kashif";      //ok
       // strcpy(test->lastName, "Masood"); //ok
    
       // test->lastName[0]= 'M';           //ok
       // test->lastName[1]= 'a';           //ok
    
        cout << (*test).item << endl;
        cout << test->item << endl;
        cout << endl;
    
        cout << (*test).firstNamePointer << endl;
        cout << test->firstNamePointer << endl;
        cout << endl;
    
        cout << (*test).middleName << endl;
        cout << test->middleName << endl;
        cout << endl;
    
        cout << (*test).lastName << endl;
        cout << test->lastName << endl;
        cout << endl;
    
        return (EXIT_SUCCESS);
    }
    Thanks

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem in printing characters by using class

    Quote Originally Posted by Basit781 View Post
    Hmm now i understood . Actually i was confused about the working of code. I was mixing these code
    Why are you using char* for string data at all?

    Your program is easily broken if I give it a last name that is more than 10 characters.
    Code:
        int item;
        char *firstNamePointer;
        char lastName[10] ;
        string middleName;
    Get rid of the char* and use std::string for all string data.
    Code:
    Test test("Basit", "ThisisaLongLastName", "Mahmood");
    This will fail with your code.

    Secondly:
    Code:
     Test *test = new Test("Basit", "Ahmed", "Mahmood");
    
        test->item = 5;                     //ok
    Why are you using "new" here? This is an indication you don't know how to properly instantiate objects, and not just an oversight.
    Code:
    Test test("Basit", "Ahmed", "Mahmood");
    Then you don't need "new".
    Code:
    And after understanding this, i came up with this and it's working very well.
    It doesn't work. I showed you how it is easily broken with a last name of more than 10 characters.

    When you write a C++ application, you attempt to make it as bug-free as possible. To do this, you use the safest constructs, one being the use of string classes, not 'C' char* handling as you've done.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Problem in printing characters by using class

    Quote Originally Posted by Basit781 View Post
    Code:
       char lastName[] = {'M', 'a', 's', 'o', 'o', 'd'};  //same as  char firstName[] = "Masood";
    Wrong.

    Code:
       char lastName[] = {'M', 'a', 's', 'o', 'o', 'd', '\0'};  //same as  char firstName[] = "Masood";
    "" is automatically null-terminated. Not the case with arrays. Don't forget to null terminate your character strings.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  10. #10
    Join Date
    Jul 2010
    Posts
    73

    Re: Problem in printing characters by using class

    Hi
    Mr. Paul i read your comments and i am very thankful to you for giving me suggestions. I greatly appreciate your suggestions. But as i already told that i am newbie so it was just an exercise.

    Why are you using char* for string data at all?

    Your program is easily broken if I give it a last name that is more than 10 characters.
    I know it, but i want to see how you can assign value to character array when you use class. I also used string when i was trying this code, you can see in my first post

    Why are you using "new" here? This is an indication you don't know how to properly instantiate objects, and not just an oversight.
    Actually when i first tried this code then i didn't use new, instead i used the syntax, you suggested

    Code:
    Test test("Basit", "Ahmed", "Mahmood");
    
    test.item = 5;
    strcpy(test.lastName, "Masood);
    ....
    But i am reading a book in which i encountered advance Pointers topic. In this topic the author used linked list and Tree structures. Although i didn't read the whole topic but i saw the new syntax.
    So i tried it with new syntax just like that. I want to see how you can access variables when you used simple construction and when you use new (difference between . and ->) . You can see i used two syntax for printing data
    Code:
        cout << (*test).firstNamePointer << endl;
        cout << test->firstNamePointer << endl;
        cout << endl;
    i used it for my understanding.The whole thing was just an exercise. And once you tole me that whenever you have some problem just write simple code and then ask
    But thank you very much for giving me good C++ programming suggestions

    Thanks monarch_dodra for correcting me about character array null terminator

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured