Calling static methods from A.cpp into main.cpp

Hi all, i just started learning C++ and i was tasked with an assignment recently.

i am currently stuck at my requirement which is creating a static method in a class and then i need to call it into my main.cpp

eg. A.h

 
 static float computeSomething(string, int, int, float, float);


A.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
float A::a_Percent(string Type1)
{
    float a_Percentage= 0.0;
    if(Type1== "Type O")
    {
        a_Percentage= 30.0;
    }
    else if (Type1== "Type B")
    {
        a_Percentage== 45.0;
    }
    else if (Type1 == "Type A")
    {
        a_Percentage== 60.0;
    }
    else if (Type1 == "Type F")
    {
        a_Percentage== 75.0;
    }
    else if (Type1 == "Type G")
    {
        a_Percentage== 90.0;
    }
    else if (Type1 == "Type K")
    {
        a_Percentage== 80.0;
    }
    else if (Type1 == "Type M")
    {
        a_Percentage== 70.0;
    }
    return (a_Percentage);

}





float A::computeSomething(string Type1, int type2, int type3, float type4, float type5)
{

    float calcualte1 = A::a_Percent(Type1) / 100;
    float cal2= (type3+ type4)/200;
    int cal3 = type2+ type3;
    float cal4 = (calcualte1 - type3) * type2;

    return cal4;
}


before i even run the code in my main.cpp

i am getting an error
"error: cannot call member function 'float A::a_Percent(std::__cxx11::string)' without object"

which i am only suppose to initialize an object in my main.cpp

before anybody say thats i didnt include any "include files on top"
i already did..

Thanks in advance.
Static functions need to be members of a class. See also
http://www.learncpp.com/cpp-tutorial/812-static-member-functions/
creating a static method in a class

a) create a header and a source file for your class, e.g. MyClass.h and MyClass.cpp

b) declare your class in the header:
class MyClass {};

c) add a static method:
static void mymethod();

d) if you want to test it, the easier way is to make it public and return something:
1
2
public:
    static int mymethod();


e) include your class header inside your class source file:
#include "MyClass.h"

e) define your method in the source file:
1
2
3
4
5
int MyClass::mymethod()
{
    // add code here
    return // something which is an int or can be implicitly converted into int
}


call it into my main.cpp

a) include your class header
#include "MyClass.h"

b) add a function main():
1
2
3
4
int main()
{
    return 0;   
}


c) invoke your method from inside main():
std::cout << // add your method here

d) compile:
g++ -std=gnu++17 -Wall -Wextra -pedantic-errors -pipe -O3 MyClass.cpp main.cpp -o main.exe
> "error: cannot call member function 'float A::a_Percent(std::__cxx11::string)' without object"
a member function should use or modify the internal state of the caller object. If you don't give a damn about the state of the object, then it shouldn't be a member function, you may make it static
a_Percent() should not be a member function, but static of free function.
hi guys, thanks for the replied. I already solved this.. :)
Topic archived. No new replies allowed.