简体   繁体   English

错误:未在范围中声明函数

[英]error: function was not declared in the scope

I have a class named logs_i with a virtual fucntion which named begin_record; 我有一个名为logs_i的类,它带有一个名为begin_record的虚拟功能。 I had to write a new class named counter_logs_t which supposed to has a method which counts the logs. 我必须编写一个名为counter_logs_t的新类,该类应该具有一种计算日志的方法。

Here is the interface and the implemantation of counter_logs_t: 这是counter_logs_t的接口和实现:

class counter_logs_t : public log_i
{
public:
    counter_logs_t(int counter);
    void print_counter(void);
    void add_counter(void);
    virtual void begin_record(void);

private:
    int counter;
};

counter_logs_t::counter_logs_t(int counter) : log_i()
{
    counter = 0;
}

void counter_logs_t::add_counter(void)
{
    counter++;
}

void logs_t::begin_record(void)
{
    log_i::begin_record();
    add_counter();
}


void counter_logs_t::print_counter(void){
    cout<< counter<< endl;
}

int main()
{

        counter_logs_t  container1();
//some code
        container1.print_counter();

    return 0;
}

When I try to build I got the following error: error: 'add_counter' was not declared in the scope 当我尝试构建时,出现以下错误:错误:范围中未声明“ add_counter”

The signature of this function is incorrect, specifically the class name 该函数的签名不正确,特别是类名

void logs_t::begin_record(void)

I think you meant 我想你是说

void counter_logs_t::begin_record(void)

Edit: 编辑:

Your second issue is that you ran into the most vexing parse on this line 第二个问题是您在这条线上遇到了最烦人的解析

counter_logs_t  container1();

This is interpreted as declaring a function named container1 that takes no arguments and returns a coutner_logs_t . 这被解释为声明一个名为container1的函数,该函数不带任何参数并返回coutner_logs_t In fact, there is no default-constructor for counter_logs_t , the only constructor has the following signature 实际上, counter_logs_t没有默认构造函数,唯一的构造函数具有以下签名

counter_logs_t(int counter);

Therefore you have to construct it with a counter argument. 因此,您必须使用counter参数构造它。

counter_logs_t container1{0};
counter_logs_t container1 = counter_logs_t(0);

Or make a default constructor 或设置一个默认的构造函数

counter_logs_t::counter_logs_t() : log_i(), counter(0) {}

then you can just say 那你就可以说

counter_logs_t container1;

You also have an issue where 您也有一个问题

counter_logs_t container1();

Is not declaring a variable. 没有声明变量。 It declares a function named container1 that reutrns a counter_logs_t and takes nothing. 它声明了一个名为container1的函数,该函数将返回counter_logs_t并且不执行任何操作。 You need to change it to 您需要将其更改为

counter_logs_t container1;

To declare a variable. 声明一个变量。

void logs_t::begin_record(void)
{
    log_i::begin_record();
    add_counter();
}

in this function: 1. "logs_t" should be "counter_logs_t". 在此功能中:1.“ logs_t”应为“ counter_logs_t”。 2. i don't recommend you to call class log_i's method begin_record() using "::" operator, unless you know it's a static method. 2.我不建议您使用“ ::”运算符调用log_i类的begin_record()方法,除非您知道这是一个静态方法。 You'd better call it like this this->begin_record for it's a virtual method. 您最好像这样- this->begin_record来调用它,因为它是一个虚拟方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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