简体   繁体   English

即使存在头文件,也未在此范围内声明函数

[英]Function was not declared in this scope, even though header file is present

I'm trying to run some test code to learn c++, but I am getting an error telling me the reverseDigits function was not declared in the main.cpp scope: 我正在尝试运行一些测试代码来学习c ++,但是我收到一条错误消息,告诉我在main.cpp范围中未声明reverseDigits函数:

error: 'reverseDigits' was not declared in this scope . error: 'reverseDigits' was not declared in this scope

But the #include "Solutions.h" header was included in main.cpp , so I thought that it would be in scope. 但是#include "Solutions.h"标头包含在main.cpp ,所以我认为它应该在范围内。

I have checkout other questions, but the answers all relate to problems with circular header file inclusion, which I don't think is the problem here. 我还有其他问题,但答案都与循环头文件包含有关,我认为这不是问题。

Do you know why I am seeing that error? 您知道我为什么看到这个错误吗?

Solution.h Solution.h

#ifndef SOLUTION_H
#define SOLUTION_H

class Solution {
public:
    Solution();
    ~Solution();
    int reverseDigits(int x);
};

#endif // SOLUTION_H

Solution.cpp Solution.cpp

#include "Solution.h"
#include <string>

Solution::Solution()
{
}

Solution::~Solution()
{
}

int Solution::reverseDigits(int x) {
    std::string num_string = std::to_string(x);
    std::string reversed_num_string {};
    for (int i = num_string.length() - 1; i > 0; i--) {
        reversed_num_string.push_back(num_string[i]);
    }
    return stoi(reversed_num_string);
}

main.cpp main.cpp中

#include <iostream>
#include "Solution.h"

int main()
{
    int x {123};
    int result = reverseDigits(x);
    std::cout << result << std::endl;
    return 0;
}

You declared reverseDigits as a member function of the Solution class, then defined it without qualifying it as a member of Solution (Edit: You've since changed it to match declaration and definition, but at point of use, you're trying to use an unqualified function, not a member of a Solution object). 您将reverseDigits声明为Solution类的成员函数,然后在不将其限定为Solution成员的情况下对其进行了定义(编辑:您已经对其进行了更改,以使其与声明和定义匹配,但是在使用时,您尝试使用不合格的函数,而不是Solution对象的成员)。 The declaration in the .h file is visible, but the definition in the .cpp is unrelated, and not visible to main.cpp . .h文件中的声明是可见的,但是.cpp的定义是不相关的,并且对main.cpp不可见。

Declare the function outside the class (since it's clearly unrelated to the class), and it should work, changing to: 在类外声明函数(因为它显然与类无关),并且应该起作用,更改为:

class Solution {
public:
    Solution();
    ~Solution();
};

int reverseDigits(int x); // NOT inside the class definition

I'll note: I have no idea why you have a Solution class at all. 我会注意:我根本不知道为什么要有Solution类。 Defining reverseDigits doesn't require it, so I'm not seeing the point. 定义reverseDigits不需要它,所以我没有明白要点。 If this is part of some automated evaluation framework, you'll have to give more details 如果这是某些自动化评估框架的一部分,则必须提供更多详细信息

Along with ShadowRanger's valid suggestion , I'll highlight upon how you could have used the data as part of your Solution class and applied the function on it. 连同ShadowRanger的有效建议 ,我将重点介绍如何将数据用作Solution类的一部分并在其上应用函数。

Refactoring your class to 将您的班级重构为

class Solution {
 public:
     Solution(int data);
     ~Solution();
     int reverseDigits();
 private:
     int m_data;
};

Solution::Solution(int data)
{
    m_data = data;
}

Solution::~Solution()
{
}

Even though you could have used std::reverse , fixing the error on the i>=0 is needed to have your own reverse function 即使您可以使用std::reverse ,也需要在i>=0上修复错误才能拥有自己的反向功能

int Solution::reverseDigits() {
    std::string num_string = std::to_string(m_data);
    std::string reversed_num_string {};
    for (int i = num_string.length() - 1; i >= 0; i--) {
        reversed_num_string.push_back(num_string[i]);
    }
    return stoi(reversed_num_string);
}

Now call it from your main() as 现在从您的main()调用它

int main() {
    int x = 123;
    Solution sol(x);
    std::cout << sol.reverseDigits() << std::endl;
    return 0;
}

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

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