-1

I'm beginner in c++, I'm trying to run this simple code using __declspec inside a class for get & put properties but the VScode does not recognize the get&put properties within __declspec as shown in the picture[enter image description here] :https://i.stack.imgur.com/iqxzi.jpg

the full message of the error(above in the picture)

// This is the source code

#include <iostream>
using namespace std;

class clsPerson
{
private:
    string _FirstName;

public:
    void SetFirstName(string FirstName)
    {
        _FirstName = FirstName;
    }
    string GetFirstName()
    {
        return _FirstName;
    }

    __declspec(property(get = GetFirstName, put = SetFirstName)) string FirstName;
};

int main()

{
    system("cls");
    clsPerson Person1;
    Person1.SetFirstName("Zak");
    cout << Person1.GetFirstName() << endl;
    // instead of the above we only write this
    Person1.FirstName = "Zak";
    cout << Person1.FirstName;

    system("pause>0");
    return 0;
};

I tried to include all possible libraries but with no result..

Thanks in advance for all help and support :-)

19
  • Seeing as you are a beginner, why did you decide on using VS Code, one of the more difficult-to-use IDEs? Sep 22 at 22:40
  • 2
    Also note the syntax you're struggling with is not Standard C++ syntax. The compiler you're using underneath VS Code may not have the slightest clue what it means Sep 22 at 22:43
  • 2
    At this point I'm going out on a limb and saying it looks like you have the tools configured correctly, and the problem is you're using the wrong tools for the code you are trying to compile. I'm not sure if that needs a Borland compiler or a Microsoft compiler, or clang, or what, but what ever compiler that use of __declspec is for, you are not using it. Sep 22 at 22:49
  • 1
    Side note: An identifier that starts with two underscores (and less commonly anything that has two underscores in a row anywhere in the name) is a non-standard implementation detail (or someone screwed up with their naming) Any time you see __ in code you need to stop and make certain its for your tools and not some other tools. Sep 22 at 22:57
  • 1
    Side-side note: If you read that link about naming rules you'll notice _FirstName also breaks the the rules. Sep 22 at 23:18

1 Answer 1

0

Finally it work guys with VScode, thanks @starball for the link :-),thanks @HolyBlackCat you have right my friend about the compiler MSVC it's only work with it, and for how .. I use one of buildtools made by Microsoft with VS and before lunch VScode you should executed with Developer Command Prompt for VS 2022, wich run&debug inside of VScode code with cl.exe (compiler) instead of gcc! It's a headache for me but I never give up. enter image description here

4
  • @starball exactly 100% true, but I noticed that if I try to debug&run with (cpp.exe or g++.exe) when I run from (Developer Command Prompt for VS) it gives me : " Build finished with error(s)." ?!!
    – Mr.Zak
    Sep 24 at 21:15
  • have you still not understood what several people have told you? your code uses MSVC-specific extensions to the C++ language. Why would you expect it to work for a compiler other than MSVC?
    – starball
    Sep 24 at 21:52
  • @starball I didn't mean the exact feature (__declspec(...) property) that we have already discuss and agree that it works only with MSVC ! What I meant in the last comment for the other simple codes(not include __declspec(...)).
    – Mr.Zak
    Sep 25 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.