致命错误LNK1120:2个未解决的外部因素

时间:2016-11-06 20:28:53

标签: c++

我最近在学校(不到2个月前)开始进行C / C ++编程,所以我不知道自己在做什么。

从我的代码中我收到了以下错误消息:

错误LNK2019:函数_main中引用的未解析的外部符号“double __cdecl MyRand(double,double)”(?MyRand @@ YANNN @ Z) 错误LNK2019:函数_main中引用的未解析的外部符号“int __cdecl love_code(void)”(?love_code @@ YAHXZ) 致命错误LNK1120:2个未解析的外部

我在这个网站上看到了类似的问题,但没有提供的解决方案对我有用。我该如何解决这个错误?

原始代码提供的图片以及输出应该是什么样的,如果有人想知道的话。 enter image description here enter image description here

我的代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

double length (double x1, double y1, double z1, double x2, double y2, double z2);
double squares (double x);
double MyRand ( double a, double b);
int love_code();

int main()
{

char name[20], parent[15];
strcpy_s(name, "Susan McLeod");
strcpy_s(parent, "Bernice McLeod"); 
int i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13;
i2 = name[0], i3 = name[1], i4 = name[2], i5 = name[3], i12 = name[4], i13 = name[5];
i6 =parent[0], i7=parent[1], i8=parent[2], i9=parent[3], i10=parent[4], i11=parent[5];
cout << "The ASCII code of [" << name << "] are " <<'['<< i2 <<']'<<'['<< i3 <<']'<<'['<< i4 <<']'<<'['<< i5 <<']'<<'['<< i12 <<']'<<'['<< i13 <<']'<< endl;
cout << "The ASCII code of [" << parent << "] are " <<'['<< i6 <<']'<<'['<< i7 <<']'<<'['<< i8 <<']'<<'[' << i9 <<']'<<'['<< i10 <<']'<<'['<< i11 <<']'<< endl;


int j, love, txtcode, shift, len;
FILE *f1, *f2, *f3;
char txt [5000];


love = love_code();
cout << "The love code is [" << love << ']' << endl;
srand (love);


fopen_s(&f1, "letter.txt", "r");
fopen_s(&f2, "hw_04_code.txt", "w");
fopen_s(&f3, "hw_03_code.txt", "w");

if (f1 == NULL) printf ("Cannot open file - letter.txt\n");
if (f2 == NULL) printf ("Cannot open file - hw_04_code.txt\n");
if (f3 == NULL) printf ("Cannot open file - hw_03_code.txt\n");


j = 0;
while (fscanf_s (f1, "%c", &txt [j]) !=EOF)

{
shift = (int) (MyRand (0, 6));
txtcode = txt [j] << shift;
fprintf (f3, "%10d\n", txt [j]);
//fprintf (f2, "%10d, %10d\n", txtcode, shift)
fprintf (f2, "%10d\n", txtcode);
j++;
}

txt [j] = '\0';
len = strlen (txt);
printf ("The total number of characters are %d\n", len);


fclose (f1);
fclose (f2);
fclose (f3);
return (0);
}

//return random number between a and b

double MyRand ( double a, double b);
{
return ((b - a + 1) * rand() / (double) RAND_MAX + a);
}

1 个答案:

答案 0 :(得分:0)

此语句不会创建函数,而是创建另一个函数声明

double MyRand ( double a, double b); // ; <- terminates the statement creates a declaration

{
 return ((b - a + 1) * rand() / (double) RAND_MAX + a);
}

由于编译器找不到声明的函数,因此会给出错误。 删除 - &gt;在;的末尾MyRand ( double a, double b),编译器不会抱怨未解析的MyRand(double,double)外部。