markyrocks Posted January 27, 2020 Share Posted January 27, 2020 (edited) 11 minutes ago, TheDcoder said: Best of luck with your learning @markyrocks, it is a fun journey assuming that you enjoy what you are doing. I do enjoy it. Much rather do this than watch tv lol. Maybe you can clear up something I was having an issue with. If I create a class in c++. Then declare a static variable like so Class class { Public: Static std::string str; }; //seems like anyway I initialize it gives me a linker error? class c; //is fine c.str="string"; //error c::str="string"; //error I've seen this done in tutorials and it seemed to work either way but I'm using vs 2019 and the tutorial is using some form of vs. Doesn't seem to error in the videos.... any thoughts? The only reason I'm interested in this is bc some member variables don't need to have a separate instance for every object. Like is str was the date. It doesn't matter what instance of the class I'm using the date should be the same. Edited January 27, 2020 by markyrocks Spoiler "I Believe array math to be potentially fatal, I may be dying from array math poisoning" Link to comment Share on other sites More sharing options...
TheDcoder Posted January 27, 2020 Share Posted January 27, 2020 5 minutes ago, markyrocks said: Maybe you can clear up something I was having an issue with. Sorry, I do not program in C++ so I don't know the specifics of how objects/classes work there. Someone more capable will answer your question EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
Danyfirex Posted January 28, 2020 Share Posted January 28, 2020 @markyrocks You're issue is Quote Static members of a class are not associated with the objects of the class: they are independent variables so you must define them like: class Myclass { public: static std::string str; }; std::string Myclass::str="Hello World"; int main() { std::cout << Myclass::str << "\n"; Myclass::str="Hola Mundo"; Myclass obj; std::cout << obj.str << "\n"; return 0; } Saludos markyrocks 1 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
markyrocks Posted January 28, 2020 Share Posted January 28, 2020 57 minutes ago, Danyfirex said: @markyrocks You're issue is so you must define them like: class Myclass { public: static std::string str; }; std::string Myclass::str="Hello World"; int main() { std::cout << Myclass::str << "\n"; Myclass::str="Hola Mundo"; Myclass obj; std::cout << obj.str << "\n"; return 0; } Saludos Ty yes I was playing around with this a bit last night and came to the conclusion that I was declaring it out of scope? Declaring the static variable inside of the main must make it invisible to the compiler. I'm assuming declaring it outside of the main makes the variable global. At least that's my take on it. Tutorials often don't make fine details like this explicitly clear, well at least the ones I'm watching anyways. Spoiler "I Believe array math to be potentially fatal, I may be dying from array math poisoning" Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now