Error Can Only Use This Within A Nonstatic Member Function
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies
Non Static Member Function In C++
of this site About Us Learn more about Stack Overflow the company difference between static and nonstatic member function in c++ Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges
C++ Non Static Member Reference Must Be Relative To A Specific Object
Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a reference to non-static member function must be called c++ minute: Sign up What is a nonstatic member function? up vote 5 down vote favorite 2 I am being told that I can't use the 'this' keyword in a class function. I'm coming from c# and i'm used to this working, but the compiler tells me that it can only be used within nonstatic member functions. D3DXVECTOR3 position; void Position(D3DXVECTOR3 position) { invalid use of nonstatic member function c++ this.position = position; } c++ methods member non-static share|improve this question edited Sep 29 '11 at 16:47 iammilind 37.4k1287186 asked Sep 29 '11 at 16:21 SirYakalot 2,72463768 are you sure it worked in c#? in c#, a 'class function' is called a 'static method', and you cannot use 'this' in those either? –muratgu Sep 29 '11 at 16:25 yeah I did it all the time to refer to a classes variable in the constructor, or is the constructor different? –SirYakalot Sep 29 '11 at 16:27 Perhaps you should show the class this method belongs to - that might help clarify what you're doing –Useless Sep 29 '11 at 16:31 add a comment| 3 Answers 3 active oldest votes up vote 10 down vote accepted this is a pointer containing the address of the object. D3DXVECTOR3 position; void YourClassNameHere::Position(D3DXVECTOR3 position) { this->position = position; } Should work. D3DXVECTOR3 position; void YourClassNameHere::Position(D3DXVECTOR3 position) { (*this).position = position; } Should also work. share|improve this answer edited Mar 28 '15 at 8:50 Mehrdad 103k64310612 answered Sep 29 '11 at 16:24 Pubby 34.3
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies
Virtual Can Only Appear On Non Static Member Functions
of this site About Us Learn more about Stack Overflow the company invalid use of 'this' outside of a non-static member function Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
Invalid Use Of This In Non Member Function
Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a http://stackoverflow.com/questions/7600346/what-is-a-nonstatic-member-function minute: Sign up Invalid use of this in a non static member function up vote 2 down vote favorite I got the following class: class Foo { private: static float scale; public: static float setScale(float scale); }; When I am trying to implement setScale like this: float Foo::setScale(float scale) { this->scale = scale; return scale; } It throws an error: Invalid use http://stackoverflow.com/questions/27583384/invalid-use-of-this-in-a-non-static-member-function of 'this' outside of a non-static member function. I really don't get this since my function is marked static. I saw some related questions but it didn't answer my question. So how can I fix this? I know I can change the names and don't use this but there probably is a solution for this? EDIT: Also when I implement the function inside the class it still throws the same error. c++ share|improve this question edited Dec 20 '14 at 19:07 asked Dec 20 '14 at 19:02 Bas 1,76051438 Should setScale() be a static member function? Static member functions are member functions that aren't bound to a specific instance. A function that sets a data member on the current instance looks to be coupled with the actual object, so making it static is a contradiction to its intended behavior. Same goes for scale. Ask yourself if scale should be different or the same for every instance. If it should be the same, then make it static, otherwise don't. –0x499602D2 Dec 20 '14 at 19:05 add a comment| 3 Answers 3 activ
library Strings library Containers library Algorithms library Iterators library Numerics library Input/output library Localizations library Regular expressions http://en.cppreference.com/w/cpp/language/member_functions library (C++11) Atomic operations library (C++11) Thread support library (C++11) Filesystem library (C++17) Technical Specifications [edit] C++ language Classes General overview class/struct types http://www.learncpp.com/cpp-tutorial/812-static-member-functions/ union types Members data members static members the this pointer nested classes member templates bit fields using-declarations member functions member access specifiers constructors and member function member initializer lists default member initializer(C++11) friend specifier explicit specifier converting constructor Special member functions default constructor copy constructor move constructor(C++11) copy assignment operator move assignment operator(C++11) destructor Inheritance base and derived classes virtual member functions override(C++11) final(C++11) pure virtual functions and abstract classes [edit] A non-static member static member function function is a function that is declared in a member specification of a class without a static or friend specifier. class S { int mf1(); // non-static member function declaration void mf2() volatile, mf3() &&; // can be cv-qualified and reference-qualified int mf4() const { return data; } // can be defined inline virtual void mf5() final; // can be virtual, can use final/override S() : data(12) {} // constructors are member functions too int data; }; int S::mf1() { return 7; } // if not defined inline, has to be defined at namespace Any function declarations are allowed, with additional syntax elements that are only available for non-static member functions: final and override specifiers, pure-specifiers, cv-qualifiers, ref-qualifiers, and member initialization lists. A non-static member function of class X may be called 1) For an object of type X using
there. It looks like you're using an ad blocker. That's okay. But we would like you to know that we are able to keep this content free and updated because we're ad supported. If you're finding our content valuable, please consider supporting us by disabling your ad blocker for just this site. Thanks, AlexLearnCpp.com 8.12 -- Static member functions By Alex on September 18th, 2007 | last modified by Alex on September 19th, 2016 Static member functions In the previous lesson on static member variables, you learned that static member variables are member variables that belong to the class rather than objects of the class. If the static member variables are public, we can access them directly using the class name and the scope resolution operator. But what if the static member variables are private? Consider the following example: 12345678910111213 class Something{private:static int s_value;};int Something::s_value = 1; // initializer, this is okay even though s_value is private since it's a definitionint main(){// how do we access Something::s_value since it is private?} In this case, we can't access Something::s_value directly from main(), because it is private. Normally we access private members through public member functions. While we could create a normal public member function to access s_value, we'd then need to instantiate an object of the class type to use the function! We can do better. It turns out that we can also make functions static. Like static member variables, static member functions are not attached to any particular object. Here is the above example with a static member function accessor: 1234567891011121314 class Something{private:static int s_value;public:static int getValue() { return s_value; } // static member function};int Something::s_value = 1; // initializerint main(){std::cout << Something::getValue() << '\n';} Because static member functions are not attached to a particular object, they can be called directly by using the class name and the scope resolution operator. Like static member variables, they can also be called through objects of the class type, though this is not recommended. Static member functions have no *this pointer Static member functions have two interesting quirks worth noting. First, because static member functions are not attached to an object, they have no this pointer! This makes sense when you think about it -- the this pointer always points to the object that the member function is working on. Static member functions do not wor