-4

I'm writing a function to reverse a string, but I keep on getting the error "Undefined symbols for architecture x86_64" when compiling (clang). Here is my code:

#include <iostream>
#include <string>

using namespace std;

char* reverse(string input);

int main() {
  char* output;
  output = reverse("abcd");
  cout << *output;
}

char* reverse(char* input) {
  char* reversed;
  reversed = new char(sizeof(input) - 1);
  for(int i = 0; i != '\0'; i++) {
    char* j = reversed + sizeof(input - 1);
    input[i] = *j;
    j++;
  }
  return reversed;
}

Specifically, this is what the compiler prints:

Undefined symbols for architecture x86_64:
  "reverse(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in test-c5860d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1

I'm sure that there are also logic errors in my code, but I'd like to have it compile and run at least before I debug those. Thanks!

bparikh
  • 45
  • 7
  • It looks like you're missing the standard library for the mentioned architecture. This is not a code problem, it's about your installation. What system? Which compiler? What command line? – ravnsgaard Jul 01 '18 at 10:44
  • 1
    Please note that `char*` and `std::string` are completely different things. – πάντα ῥεῖ Jul 01 '18 at 10:44
  • 1
    `char* reverse(string input);` and `char* reverse(char* input)` doesn't look like the same function. So the linker is looking for "the other one". – Bo Persson Jul 01 '18 at 10:44
  • @ravnsgaard No, it doesn't look like that. – πάντα ῥεῖ Jul 01 '18 at 10:46
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ Jul 01 '18 at 11:14

1 Answers1

0

The compiler complains about

char* reverse(string input);

is not defined but

char* reverse(char* input);

is.

Unrelated fault.

char* reverse(char* input) {
  char* reversed;
  reversed = new char(sizeof(input) - 1); // <----------- 2 FAULT here
  for(int i = 0; i != '\0'; i++) { // <--- and here
    char* j = reversed + sizeof(input - 1); // <-- and here
    input[i] = *j; // <--- and here
    j++; // <--- and here
  }
  return reversed;
}

Should have been

char* reverse(char* input) {
  char* reversed;
  reversed = new char[strlen(input) + 1];  // new array with room for nul
  char* j = input + strlen(input) - 1; // last char in input
  for(int i = 0; input[i] != '\0'; i++) {
    reversed[i] = *j;
    j--;
  }
  reversed[strlen(input)] = '\0';
  return reversed;
}
Surt
  • 15,501
  • 3
  • 23
  • 39
  • You didn't actually fix the fault. It would be `new char[strlen(input) + 1]` (note the square brackets) to allocate an array for the string and NUL terminator. The remaining code you didn't fix is still full of bugs as well. – Blastfurnace Jul 01 '18 at 11:52
  • @Blastfurnace thanks, edited now, I don't doubt that your right about there being more errors. – Surt Jul 01 '18 at 11:54
  • Hopefully got most of the errors fixed this time. – Surt Jul 01 '18 at 12:01
  • The way `j` is computed still looks very wrong. To be honest, I'd replace the whole loop with `std::reverse_copy(input, input + strlen(input), reversed);` and then add the NUL. – Blastfurnace Jul 01 '18 at 12:14
  • 1
    @Blastfurnace that is the right C++ way of doing it, but the OP is obviously still learning to program so it might be better to show some working code. – Surt Jul 01 '18 at 12:16
  • Thanks for your help! I should've realised to use square brackets instead of parentheses. I also tried your code out but it only printed a blank line. I also tried replacing the definition of `*j` to be `char* j = reversed + strlen(input) - 1;` and the first line of the loop to be `*j = input[i];`. Unfortunately, same result. – bparikh Jul 02 '18 at 21:57
  • @bparikh you will have to either go through it on paper or in a debugger to find out what is wrong, I've added a zero terminator in the latest edit. – Surt Jul 03 '18 at 05:07
  • @Surt thanks for your help! – bparikh Jul 03 '18 at 10:51