My code not running on test cases. What am I doing wrong?

Please clarify what you mean by «code not running on test cases». What exactly do you see?

Also, please always poste code verbatim rather than through a screenshot.

Okay, thanks.

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

using namespace std;

namespace reverse_string {
    string reverse(string to_reverse){
        int len = to_reverse.length();
        for(int i=0; i < len / 2; i++){
            char temp = to_reverse[i];
            to_reverse[i] = to_reverse[len - i - 1];
            to_reverse[len - i - 1] = temp;
        }
        return to_reverse;
    }
}  // namespace reverse_string

I wrote this code to reverse any given string. Whenever, I click run tests button on online exercism editor, I get the following errors:

We received the following error when we ran your code:

In file included from /tmp/reverse-string/reverse_string_test.cpp:3:
/tmp/reverse-string/reverse_string_test.cpp: In function 'void ____C_A_T_C_H____T_E_S_T____0()':
/tmp/reverse-string/reverse_string_test.cpp:10:35: error: 'reverse_string' is not a member of 'reverse_string'; did you mean 'reverse_string'?
   10 |     REQUIRE("" == reverse_string::reverse_string(""));
      |                                   ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:1:
/tmp/reverse-string/reverse_string.h:4:11: note: 'reverse_string' declared here
    4 | namespace reverse_string {
      |           ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:3:
/tmp/reverse-string/reverse_string_test.cpp:10:35: error: 'reverse_string' is not a member of 'reverse_string'; did you mean 'reverse_string'?
   10 |     REQUIRE("" == reverse_string::reverse_string(""));
      |                                   ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:1:
/tmp/reverse-string/reverse_string.h:4:11: note: 'reverse_string' declared here
    4 | namespace reverse_string {
      |           ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:3:
/tmp/reverse-string/reverse_string_test.cpp:10:35: error: 'reverse_string' is not a member of 'reverse_string'; did you mean 'reverse_string'?
   10 |     REQUIRE("" == reverse_string::reverse_string(""));
      |                                   ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:1:
/tmp/reverse-string/reverse_string.h:4:11: note: 'reverse_string' declared here
    4 | namespace reverse_string {
      |           ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:3:
/tmp/reverse-string/reverse_string_test.cpp: In function 'void ____C_A_T_C_H____T_E_S_T____2()':
/tmp/reverse-string/reverse_string_test.cpp:16:40: error: 'reverse_string' is not a member of 'reverse_string'; did you mean 'reverse_string'?
   16 |     REQUIRE("tobor" == reverse_string::reverse_string("robot"));
      |                                        ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:1:
/tmp/reverse-string/reverse_string.h:4:11: note: 'reverse_string' declared here
    4 | namespace reverse_string {
      |           ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:3:
/tmp/reverse-string/reverse_string_test.cpp:16:40: error: 'reverse_string' is not a member of 'reverse_string'; did you mean 'reverse_string'?
   16 |     REQUIRE("tobor" == reverse_string::reverse_string("robot"));
      |                                        ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:1:
/tmp/reverse-string/reverse_string.h:4:11: note: 'reverse_string' declared here
    4 | namespace reverse_string {
      |           ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:3:
/tmp/reverse-string/reverse_string_test.cpp:16:40: error: 'reverse_string' is not a member of 'reverse_string'; did you mean 'reverse_string'?
   16 |     REQUIRE("tobor" == reverse_string::reverse_string("robot"));
      |                                        ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:1:
/tmp/reverse-string/reverse_string.h:4:11: note: 'reverse_string' declared here
    4 | namespace reverse_string {
      |           ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:3:
/tmp/reverse-string/reverse_string_test.cpp: In function 'void ____C_A_T_C_H____T_E_S_T____4()':
/tmp/reverse-string/reverse_string_test.cpp:21:40: error: 'reverse_string' is not a member of 'reverse_string'; did you mean 'reverse_string'?
   21 |     REQUIRE("nemaR" == reverse_string::reverse_string("Ramen"));
      |                                        ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:1:
/tmp/reverse-string/reverse_string.h:4:11: note: 'reverse_string' declared here
    4 | namespace reverse_string {
      |           ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:3:
/tmp/reverse-string/reverse_string_test.cpp:21:40: error: 'reverse_string' is not a member of 'reverse_string'; did you mean 'reverse_string'?
   21 |     REQUIRE("nemaR" == reverse_string::reverse_string("Ramen"));
      |                                        ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:1:
/tmp/reverse-string/reverse_string.h:4:11: note: 'reverse_string' declared here
    4 | namespace reverse_string {
      |           ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:3:
/tmp/reverse-string/reverse_string_test.cpp:21:40: error: 'reverse_string' is not a member of 'reverse_string'; did you mean 'reverse_string'?
   21 |     REQUIRE("nemaR" == reverse_string::reverse_string("Ramen"));
      |                                        ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:1:
/tmp/reverse-string/reverse_string.h:4:11: note: 'reverse_string' declared here
    4 | namespace reverse_string {
      |           ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:3:
/tmp/reverse-string/reverse_string_test.cpp: In function 'void ____C_A_T_C_H____T_E_S_T____6()':
/tmp/reverse-string/reverse_string_test.cpp:26:46: error: 'reverse_string' is not a member of 'reverse_string'; did you mean 'reverse_string'?
   26 |     REQUIRE("!yrgnuh m'I" == reverse_string::reverse_string("I'm hungry!"));
      |                                              ^~~~~~~~~~~~~~
In file included from /tmp/reverse-string/reverse_string_test.cpp:1:
/tmp/reverse-string/reverse_string.h:4:11: note: 'reverse_string' declared here
    4 | namespace reverse_string {
      |           ^~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/reverse-string.dir/build.make:63: CMakeFiles/reverse-string.dir/reverse_string_test.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:77: CMakeFiles/reverse-string.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

It looks to me like the function should be named reverse_string rather than reverse.

The code highlighted in the errors are excerpts of the tests. They try to call your function, but the compiler finds there is no function by that name.

Hi there.

I’m having the same issue as the first user.

I’m trying to solve a problem with C++ and I don’t really know where I’m supposed to put my code because when I hit on run tests, it doesn’t seem to find my code.

I made sure the function name is the same as the tester in expecting and I wrote the function inside the right namespace but it doesn’t work.

Also, why are there two files, the .cpp and the .h files?
I mean, I found that the .h is for the headers but I don’t understand how we should use it (if we should use it at all in here).

It’d be nice if the exercises explained what the tester is expecting from our code, like names or structure, because right now it seems confusing for beginners.

Apparently some insight is offered in the next article:

I managed to make it run.

The function declaration should be made inside the .h file while the function definition should be made inside the .cpp file.

For example, if we need a function with the name convert that returns a string and takes an integer as an argument we would declare it inside the .h file:

string convert (int num);

And we would define it inside the .cpp file:

string convert(int num) {
 // function body in here ...
}

Making sure we use the right namespaces and the necessary headers:


#include <string>

using namespace std;

For more information about how to declare and define functions:

So going back to the issue the original poster had, you should make sure that the name you give to your function is the name the program is expecting and you can figure this out by looking at the error messages.
In your case it’s expecting the name reverse_string
So declare this function in the .h file:

string reverse_string(string to_reverse);

And keep the .cpp file as it is (change the name of the function as I told you) and it should work.

And also, since you’re importing the .h file in your .cpp file, the following headers should go in the .h file:

#include <string>

using namespace std;