Good day. The essence of my problem set out a simple example.

class MyClass{ static int MyMethod(){ int a; a=1; return(a); }}; void main(){ int a; a = MyClass::MyMethod(); } 

The last line will have the error "undefined reference to MyClass :: MyMethod ()"

What am I missing?

  • This code really generates a lot of errors , but nothing like the "undefined reference" (this is a linker error when something is missing) can not be here, this code will not reach the linking stage. Give the real code and the full text of the error. - VTT

1 answer 1

I think you missed the word public . Your function is closed and unavailable.

If you make it public , everything compiles fine: https://ideone.com/7Ms3Cf

 struct MyClass{ static int MyMethod(){ int a; a=1; return(a); }}; int main(){ int a; a = MyClass::MyMethod(); } 

Yes, you also forgot that main returns an int .

  • in the function, immediately return one, or better yet, just keep the static const int a = 1; and only (comment for the author of the question ...) - AR Hovsepyan
  • Implementations are allowed to have additional main options, including the ability to return void . It is clear that such code will not be portable. - αλεχολυτ