4

I am trying to call a static method from a.h to b.cpp. from what I have researched, it is as simple as just putting a :: scope resolution but however I tried and it throws me an error "C++ requires a type specifier for all declarations". below is what I have.

a.cpp

float method() {
   //some calculations inside
}

a.h

static float method();

b.cpp

 a::method(); <- error!  "C++ requires a type specifier  for all declarations".
 but if I type without the ::
 a method(); <- doesn't throw any errors.

I quite confused and need guidance.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
user2211678
  • 669
  • 8
  • 13
  • 24
  • 3
    hmm, `method` does not look like an actual method to me, just a plain function. – SirDarius Oct 10 '13 at 15:47
  • Drop the `a::`. Just `method();`, it's cleaner. – Kerrek SB Oct 10 '13 at 15:48
  • Are you showing *all* of the relevant code? Static *methods* are part of a `class`. If `method` is not in a class, you don't want `static` for that declaration. – crashmstr Oct 10 '13 at 15:51
  • in a.h put `static float a::method();` – Hans Z Oct 10 '13 at 15:51
  • @KerrekSB it still throws back the error "C++ requires a type specifier for all declarations" – user2211678 Oct 10 '13 at 15:52
  • @crashmstr in c/c++ static means that identifier exists in a scope that's static in whatever context it was initialized. A static method inside of a class means that the scope is of the class itself, i.e. when you modify static variables, you modify that variable for all instances of that class. A static method defined inside of a .h file means the static scope is that of the .h file, i.e. when you modify that .h file's static variables, you modify it for all calls to that variable in that .h file. – Hans Z Oct 10 '13 at 15:55
  • Is this actually a duplicate of http://stackoverflow.com/questions/11843469/type-specifier-error – doctorlove Oct 10 '13 at 15:58
  • @HansZ The code shown does not have any `class` named `a`, just a header and cpp file. My point was that if `method` is *not* declared in a `class` named `a`, then `static` is probably not appropriate. – crashmstr Oct 10 '13 at 16:02
  • @user2211678 could you show us more code? If you have a class a, and method is declared in it, you need float a::method() in the a.cpp file – crashmstr Oct 10 '13 at 16:14
  • Nevermind I got it working already. thanks for all your help. it turns out to be my shift key is having problem and I end up typing ; and I didn't realize SORRY :/ – user2211678 Oct 10 '13 at 16:40
  • 1
    Your last line, `a method();` does not throw any error because this is interpreted as a function declaration. – François Moisan Oct 10 '13 at 17:32

2 Answers2

3

If you simply have

#include "b.h"
method();

you are just dumping some instructions in the middle of "nowhere" (well somewhere you could declare a function, define a function or do something evil like define a global, all of which require a type specifier to start with)

If you call your function from another method, say main the compiler will think you are calling a method rather than trying to declare something and failing to say what type it is.

#include "b.h"
int main()
{
    method();
}

edit

If you really have a static methid in a class, say class A declared in a header called a.h you call it this way, using the scope resoution operator as you have said, being careful not to dump random function calls at global scope, but to put them inaside methods.

#include "a.h"
int main()
{
    A::method();
}

If the question is also how to declare and define the static method this is the way to do it: in a.h:

#ifndef A_INCLUDED
#define A_INCLUDED

class A{

public :       
      static float method(); 

}; 
#endif

and then define it in a.cpp

#include "a.h"

float A::method()
{
    //... whatever is required
    return 0;
}
doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

It's tough to understand what it is you're trying to do. Try something like:

a.h

// header file, contains definition of class a
// and definition of function SomeFunction()

struct a {
    float method();
    static float othermethod();
    static float yetAnotherStaticMethod() { return 0.f; }

    float value;
}

float SomeFunction();

a.cpp

// implementation file contains actual implementation of class/struct a
// and function SomeFunction()

float a::method() {
   //some calculations inside
   // can work on 'value'
   value = 42.0f;
   return value;
}

float a::othermethod() {
    // cannot work on 'value', because it's a static method
    return 3.0f;
}

float SomeFunction() {
     // do something possibly unrelated to struct/class a
     return 4.0f;
}

b.cpp

#include "a.h"
int main()
{
     a::othermethod();  // calls a static method on the class/struct  a
     a::yetAnotherStaticMethod();  // calls the other static method

     a instanceOfA;

     instanceOfA.method();   // calls method() on instance of class/struct a (an 'object')
}
Arunas
  • 1,282
  • 1
  • 11
  • 17
  • Hey I got it working already. thanks for all your help. it turns out to be my shift key is having problem and I end up typing ; and I didn't realize SORRY :/ – user2211678 Oct 10 '13 at 16:42