dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: [RESOLVED] error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    [RESOLVED] error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    Hi guys!

    The error:
    Code:
    error C2662: 'Item::GetName' : cannot convert 'this' pointer from 'const Item' to 'Item &'
    My class definition:
    Code:
    class Inventory
    {
    public:
    	Inventory(vector<Item> ITEMLIST): itemList(ITEMLIST) {}
    	inline void ListItems();
    private:
    	vector<Item> itemList;
    };
    The class member function that the error is occurring in (error line in red):
    Code:
    inline void Inventory::ListItems()
    {
    	if(itemList.empty())
    		cout << "\n\nYou have no items.";
    	else
    	{
    		vector<Item>::const_iterator iter = itemList.begin();
    		for(iter; iter != itemList.end(); ++iter)
    		{
    			cout << "\n\n";
    			cout << iter->GetName();
    		}
    	}
    }
    And the class (Item) member function that the iter is trying to access:
    Code:
    string GetName() { return name; }
    I can't get around this error.. it's beyond my knowledge I think. I've tried and I'm seeking helppppppp. If I'm missing anything out let me know!
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    May 2002
    Posts
    1,435

    Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    Without seeing more of your code I can't be certain, but maybe this will help:

    string GetName() const { return name; }

    It is always good practice to make any class member function that does not modify the contents of the class (such as GetName()) a constant.

  3. #3
    Join Date
    Feb 2007
    Location
    Voronezh, Russia
    Posts
    19

    Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    Need add 'const'

    Correct code:
    Code:
    string GetName() const { return name; }
    Regards,
    Alexander Kiselev
    http://www.linkedin.com/in/akiselev
    ICQ: 32212717
    Skype: kas_ru

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    You can only call functions marked const for the const objects!
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    It is always good practice to make any class member function that does not modify the contents of the class (such as GetName()) a constant.
    Sorry, I forgot when I was making the class. I have made other const member functions I swear. :x

    I don't understand... the objects iter were accessing weren't const... can someone please explain to me WHY it's giving me this error... I am a beginner so go easy with the exclamation marks... *hints at cilu*. Something that seems trivial and petty to you guys is a whole new thing to me.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    Quote Originally Posted by Mybowlcut
    Sorry, I forgot when I was making the class. I have made other const member functions I swear. :x

    I don't understand... the objects iter were accessing weren't const... can someone please explain to me WHY it's giving me this error... I am a beginner so go easy with the exclamation marks... *hints at cilu*. Something that seems trivial and petty to you guys is a whole new thing to me.
    Yes it was:
    Code:
    vector<Item>::const_iterator iter = itemList.begin();
    Should I suggest a good C++ book?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    HAHA! Now THAT'S embarrassing! Hahahahaha... ohhh dear. Oh, Cilu, you break my heart with your book suggestions. I'm trying my hardest to learn, honest! But... I guess I do frequent this page more than a beginner should... since you're one of the guys who is always correcting me, suggest one that stands out of that list (I've seen that list a few times) and I'll buy it. I guess the game programming books don't work as well. :P
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    Young padawan, I have two lessons for you:
    1. learn to pay attention, and don't jump to rushed conclusions
    2. learn the first rule first

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  9. #9
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    Star Wars, right? I haven't watched that many of those haha... I do try my hardest to pay attention. But whenever I'm here I get the feeling I'm making myself sound REALLY stupid... oh I guess that's jumping to a rushed conclusion? Or is it? Damnit!
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

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

    Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    Quote Originally Posted by Mybowlcut
    Sorry, I forgot when I was making the class. I have made other const member functions I swear. :x
    You should also pass your objects by reference or const reference, not by value.

    Your constructor is being passed a vector by value. That means that the compiler will make a temporary copy, which could make your program much slower.
    Code:
    Inventory(const vector<Item>& ITEMLIST): itemList(ITEMLIST) {}
    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &'

    Oops. Cheers for that, Paul.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)