未在此作用域中声明函数错误

时间:2019-03-06 20:38:24

标签: c++ function scope declare

我试图在一个类中定义一个函数,然后在.cpp程序中使用它。我已经在main上面声明了该函数,但无法弄清楚为什么我得到范围声明中未声明的错误。

来自g ++的错误:“此范围内未声明错误:'convert'”

谢谢。

A.h:

class A
{
    public:

        int convert(char bcd);

};

program.cpp:

#include "A.h"
#include <iostream>
#include<stdio.h>

using namespace std;


int A::convert(char b)
{
    return b*5;
}

int main(){

    char myword = '27'; 
    char cc = convert(myword); //scope error here
    cout << "Number is: " << cc << endl;

   return 0;
}

2 个答案:

答案 0 :(得分:2)

错误本身是因为您没有任何由名称convert()定义的“自由功能”,该名称可以在您尝试调用它的范围内直接访问的任何范围内。您有一个是class A的成员方法(“函数”)的方法,但没有在该类之外定义的方法。因此,是的,您定义了一个名为convert()的“函数”,但是在您试图调用它的范围内它是不可访问的。您必须添加一些内容来告诉编译器在哪里可以找到您要调用的“ convert()”函数。有几种方法可以做到:

  1. 对于非静态类成员函数(当它属于类时通常称为“方法”),首先需要该类类型的对象才能调用该方法。
  2. 对于类静态成员函数(通常称为“静态方法”),可以使用该类型的对象来调用它以解析范围,或者,由于它是静态方法,因此可以使用类的名称(以及::范围解析运算符)来解析范围。
  3. 如果您有一个“自由函数”,它是在一个单独的命名空间中定义的,该命名空间不包含您试图调用该函数的范围,则需要使用::范围解析运算符(例如“ namespaceName::functionName()”),或者使用“ using”关键字将该函数的名称带入当前范围。

由于您要询问类成员方法,因此在以下讨论中,我将忽略上面的#3。...

您将有一个class A的实例吗?如果是这样,说您称它为“弗雷德”,那么您可以叫fred.convert(myword);。但是A::convert()不使用class A的任何成员数据,因此也许您希望它是一个静态成员方法(即,即使没有类类型的对象也可以调用它),在其中需要将该方法声明为static,并且在调用它时,请使用“ A::convert(myword);”。

下面是创建class A实例并在其上调用方法A::convert()的示例:

===== file 'A.h' =====
class A
{
    public:
        int convert(char bcd);
};

===== file 'program.cpp' =====
#include "A.h"
#include <iostream>
#include<stdio.h>

using namespace std;

int A::convert(char b)
{
    return b*5;
}

int main()
{
    char myword = '27';
    A fred; // create an instance of class A named "fred"
    char cc = fred.convert(myword);
    cout << "Number is: " << cc << endl;
}

下面是一个示例,其中A::convert()是静态成员方法,并且不需要类的实例来调用该方法:

===== file 'A.h' =====
class A
{
    public:
        static int convert(char bcd); // <--- note 'static' keyword
};

===== file 'program.cpp' =====
#include "A.h"
#include <iostream>
#include<stdio.h>

using namespace std;

int A::convert(char b)
{
    return b*5;
}

int main()
{
    char myword = '27';
    A fred; // create an instance of class A named "fred"
    char cc = fred.convert(myword); // you can call convert() on an A object
    char dd = A::convert(myword); // or you can call it without an A object
    cout << "Number cc is: " << cc << endl;
    cout << "Number dd is: " << dd << endl;
}

答案 1 :(得分:1)

您混淆了三件事:

  1. 功能
  2. 类成员方法
  3. 静态类方法

一个函数是一组要连续执行的命令。您可以这样声明它们:

type function(type parameter1, type parameter2 /*etc */);

您可以这样定义它们:

type function(type parameter1, type parameter2 /*etc */) {
  // implementation...
}

并这样称呼他们:

type myvar = function(parameter1,parameter2 /*etc */);

Class方法是一个函数,它也是一个类的成员。它通常使用实例数据执行某些操作。您可以这样声明它们:

class A
{
    public:

        type method(type parameter1, type parameter2);

};

这样定义它们:

type A::method(type parameter1, type parameter2)
{
    return b*5;
}

并这样称呼他们:

A myVar;
myVar.method(parameter1,parameter2);

最后,有@phonetagger建议,这是静态类方法。这些就像常规方法一样,但是通常不涉及实例数据。但是,它们通常以某种方式与班级相关。您可以这样声明它们:

class A
{
    public:

        static type method(type parameter1, type parameter2);

};

这样定义它们:

type A::method(type parameter1, type parameter2)
{
    return b*5;
}

并这样称呼他们:

A::method(parameter1,parameter2);

您需要确定真正需要哪一个,并正确实施。

相关问题