Click here to Skip to main content
15,561,246 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a method:

CString             GetName( void ) const {return m_Name;}


if I write:
CString tt;
tt = GetName();


I have an error in debug..I don't know why

What I have tried:

I tried code that I've written and it doesn't work
Posted
Updated 14-Oct-22 6:22am
Comments
Greg Utas 14-Oct-22 9:45am    
The fragment looks OK, so you're going to have to show more code than this. The error is elsewhere.
OriginalGriff 14-Oct-22 9:57am    
"It doesn't work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Use the "Improve question" widget to edit your question and provide better information.
jeron1 14-Oct-22 9:59am    
In addition to what Greg stated, also post the exact error you are seeing.

1 solution

C++
CString tt;
tt = GetName();

The declaration of GetName suggests that it is part of some class that you have created, which holds a string called m_Name. But you are trying to call it as a stand-alone function, not as a method of a class. You need an object of your class (fully constructed) before you can use its methods. So your code should most likely be something like:
C++
class MyClass
{
    CString m_name;
public:
    MyClass(CString name)
    {
        m_name = name;
    }
    CString GetName( void ) const {return m_Name;}
};

// then in your main or other function :

MyClass thing("Fred");
CString tt;
tt = thing.GetName();
 
Share this answer
 
Comments
merano99 15-Oct-22 3:54am    
+5
Richard MacCutchan 15-Oct-22 4:09am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900