Reputation: 2111
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:
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.
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
#ifndef SOLUTION_H
#define SOLUTION_H
class Solution {
public:
Solution();
~Solution();
int reverseDigits(int x);
};
#endif // SOLUTION_H
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
#include <iostream>
#include "Solution.h"
int main()
{
int x {123};
int result = reverseDigits(x);
std::cout << result << std::endl;
return 0;
}
Upvotes: 4
Views: 7934
Reputation: 85875
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.
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
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
int main() {
int x = 123;
Solution sol(x);
std::cout << sol.reverseDigits() << std::endl;
return 0;
}
Upvotes: 2
Reputation: 155674
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). The declaration in the .h
file is visible, but the definition in the .cpp
is unrelated, and not visible to 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. Defining reverseDigits
doesn't require it, so I'm not seeing the point. If this is part of some automated evaluation framework, you'll have to give more details
Upvotes: 3