2016-01-22 107 views
0

错误1错误LNK2019:解析外部符号 “空隙__cdecl节目(结构粘性常量&)”(显示@@ YAXABUstringy @@@ Zβ)函数引用_main F:\ C++代码\ exercise8。 42 \ exercise8.42 \ Source.obj exercise8.42错误链接2019

#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; 
} 

我刚开始代码在C++中。

+1

仔细检查声明和定义的签名。 'void show(const stringy&r1)' – Joe

+3

'void show(stringy&r1)'与'void show(const stringy&r1)'不一样。 –

+1

[什么是未定义的引用/未解析的外部符号错误,以及如何解决它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external- symbol-error-and-how-do-i-fix) – drescherjm

回答

1

const函数参数是该函数签名的一部分。因此

void show(const stringy & r1); 

是不一样的

void show(stringy & r1); 

截至main()汇编编译器只知道的void show(const stringy & r1);但随后的链接程序未找到,因为你实现

void show(stringy & r1); 
及其实施

而不是

void show(const stringy & r1); 

解决方案只是在执行show时在stringy & r1之前添加const