0

Error 1 error LNK2019: unresolved external symbol "void __cdecl show(struct stringy const &)" (?show@@YAXABUstringy@@@Z) referenced in function _main F:\c++代码\exercise8.42\exercise8.42\Source.obj exercise8.42

#include<iostream>
#include<cstring>

struct stringy{
    char * str;
    int ct;
};

void set(stringy & r1, char * a);
void show(const stringy & r1);
;

int main()
{
    stringy beany;
    char testing[] = "Reality isn't, and show() go there";
    set(beany, testing);
    show(beany);

    return 0;
}

void set(stringy & r1, char *a)
{
    int i = 0;
    while (a[i] != '\0')
        i++;
    r1.str = new char;
    r1.ct = i;
    for (int b = 0; b < i; b++)
        r1.str[b] = a[b];
    r1.str[i + 1] = '\0';
}

void show(stringy & r1)
{
    std::cout << *r1.str;
}

I am just beginning code in c++.

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
  • 1
    Double check the signature of the declaration and the definition. `void show(const stringy & r1)` – Joe Jan 22 '16 at 19:49
  • 3
    `void show(stringy & r1)` is not the same as `void show(const stringy & r1)`. – Captain Obvlious Jan 22 '16 at 19:50
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – drescherjm Jan 22 '16 at 19:53
  • @drescherjm It is a simple typo. The function definition does not match the declaration. – NathanOliver Jan 22 '16 at 19:55

1 Answers1

1

const of the function argument is part of that function's signature. Thus

void show(const stringy & r1);

is not the same as

void show(stringy & r1);

At the compilation of main () the compiler is aware only of void show(const stringy & r1); but then the linker doesn't find its implementation since you implemented

void show(stringy & r1);

and not

void show(const stringy & r1);

The solution is just to add const before stringy & r1 at the implementation of show.

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45