Posts

Showing posts with the label static

What is the reason behind “non-static method cannot be referenced from a static context”? [duplicate]

What is the reason behind “non-static method cannot be referenced from a static context”? [duplicate] This question already has an answer here: The very common beginner mistake is when you try to use a class property "statically" without making an instance of that class. It leaves you with the mentioned error message: You can either make the non static method static or make an instance of that class to use its properties. Why? I am not asking for solutions. I would be grateful to know what is the reason behind it. The very core reason! private java.util.List<String> someMethod(){ /* Some Code */ return someList; } public static void main(String strArgs){ // The following statement causes the error. You know why.. java.util.List<String> someList = someMethod(); } This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. ...

I need to reference a variable from a non static class in a non static class constructor. [c#]

I need to reference a variable from a non static class in a non static class constructor. [c#] I'm trying to make a solar system with natural satellites. Currently I'm drawing a planets relative to a static "Sun" class object, but i wold like to make the class use a position of another planet object and draw relative to that planet. To do that i need to extract the x and y position of that planet object. This is the class constructor of a class i use for drawing planets. Nebesko_Telo merkur = new Nebesko_Telo(Sun.x, Sun.y, 4, 12, 4.090909090909091, 1, 255, 255, 255); // A field initializer cannot reference the non-static field, method, // or property 'Form1.merkur'. Nebesko_Telo venera = new Nebesko_Telo(merkur.x, merkur.y, 1, 23, 1.5, 1, 176, 108, 32); This is the class constructor. public Nebesko_Telo(doubl _rel_tel_x, double _rel_tel_y, double _r, double _or, double _Fi_mult, double _tilt_plant_n...

Error: redefinition of “a static variable” in C header files

Error: redefinition of “a static variable” in C header files I have some static variables (say, var1 and var2) declared in two different files. The variables have same name in both files. Some variables (say var1) are not initialized in their declaration and some are (var2), like following. file1.h static bool var1; static bool var2 = false; file2.h static bool var1; static bool var2 = false; According to my understanding, static variables are only restricted to the c files(or h files) they're declared in, so I should be safe having same variable names in multiple header files. But when I compile code, I get error "redefinition of var2", only for the variables that have been initialized. Edit: since some mentioned to use extern keyword, I'd like to clarify that both var1 and var2 are supposed to have different values in different c files, and should only be restricted to their respective files, Header files are copied into .c files. If you...

How to access static members of a class?

How to access static members of a class? I am starting to learn C++ and Qt, but sometimes the simplest code that I paste from a book results in errors. I'm using g++4.4.2 on Ubuntu 10.04 with QtCreator IDE. Is there a difference between the g++ compiler syntax and other compilers? For example when I try to access static members something always goes wrong. g++4.4.2 #include <iostream> using namespace std; class A { public: static int x; static int getX() {return x;} }; int main() { int A::x = 100; // error: invalid use of qualified-name 'A::x' cout<<A::getX(); // error: : undefined reference to 'A::x' return 0; } I think it's exactly the same as declared here and here (isn't it?). So what's wrong with the above code? 8 Answers 8 You've declared the static members fine, but not defined them anywhere. Basically what you've s...