简体   繁体   中英

Header file C++

when i'm trying to compile program but it says that i have an error in row when i'm to create a function pow(int base ,int exp),it says that 'pow': illegal qualified name in member declaration,here's my code:

Math.h:

#pragma once
static class Math {
public:
    static int Math::pow(int base,int exp);
};

Math.cpp:

#include "Math.h"
int Math::pow(int base, int exp)
{
    int result = 1;
    for (int i = 0; i < exp; i++)
    {
        result = result * base;
    }
    return result;
}

Line:

static int Math::pow(int base,int exp);

Does not need the Math:: prefix since it is in the Math class declaration.

The class definition is probably intended to look like this:

class Math {
public:
    static int pow(int base,int exp);
};

static before the class has no meaning and is not allowed syntax.

The member function declaration doesn't need (and isn't allowed to) have a qualified name. It belongs to the class already because it is declared in the class scope. There is no need to qualify it any further.


From what you are doing it does seem however that you really want to have a namespace, not a class:

// Header file
namespace Math {
    int pow(int base,int exp);
}
// Source file
namespace Math {
    int pow(int base, int exp)
    {
        int result = 1;
        for (int i = 0; i < exp; i++)
        {
             result = result * base;
        }
        return result;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM