gpt4 book ai didi

c - 获取 LNK2001 : unresolved external symbol when I run my program 的错误代码

转载 作者:太空宇宙 更新时间:2023-11-04 07:58:54 25 4
gpt4 key购买 nike

我的代码出现此错误:

1>a1ms4.obj : error LNK2001: unresolved external symbol _name

1>a1ms4.obj : error LNK2001: unresolved external symbol _address

1>a1ms4.obj : error LNK2001: unresolved external symbol _numbers

所以我的程序有 3 个文件:

a1ms4.c , 联系人.c , 联系人.h

当我尝试运行我的程序时遇到了上述错误。

我的程序是这样的

a1ms4.c:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include "contacts.h"
// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
// to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:



int main(void)
{
// Declare variables here:

extern struct Name name;
extern struct Address address;
extern struct Numbers numbers;


// Create a variable of type Contact and call it something self-describing like "contact"
// - HINT: Be sure to initialize the values to 0 and empty C strings
extern struct Contact contact;

// Display the title
printf("Contact Management System\n");
printf("-------------------------\n\n");

// Call the Contact function getName to store the values for the Name member
getName(&name);

// Call the Contact function getAddress to store the values for the Address member
getAddress(&address);

// Call the Contact function getNumbers to store the values for the Numbers member
getNumbers(&numbers);

// Display Contact summary details

//havent dont this yet

// Display Completion Message
printf("Structure test for Contact using functions done!\n");

return 0;
}

联系人.c:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>


// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
// to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:
#include "contacts.h"


// Get and store from standard input the values for Name
// Put your code here that defines the Contact getName function:

void getName(struct Name *contactName) {

char choice;

printf("Please enter the contact's first name: ");
scanf("%c", (*contactName).firstName);
printf("\n");

printf("Do you want to enter a middle intial(s)? (y or n): ");
scanf("%c", &choice);

printf("\n");


if (choice == 'y') {
printf("Please enter the contact's middle intial(s): ");
scanf("%c", (*contactName).middleInitial);
printf("\n");
}

else {

printf("Please enter the contact's last name: ");
scanf("%c", (*contactName).lastName);
printf("\n");

}

}


// Get and store from standard input the values for Address
// Put your code here that defines the Contact getAddress function:
void getAddress(struct Address *contactAddress){

char choice;

printf("Please enter the contact's street number: ");
scanf("%d", (*contactAddress).streetNumber);
printf("\n");

printf("Please enter the contact's street name: ");
scanf("%c", (*contactAddress).street);
printf("\n");

printf("Do you want to enter an apartment number? (y or n): ");
scanf("%c", &choice);

printf("\n");


if (choice == 'y') {
printf("Please enter the contacts apartment number: ");
scanf("%d", (*contactAddress).apartmentNumber);
printf("\n");
}

printf("Please enter the contact's postal code: ");
scanf("%c", (*contactAddress).postalCode);
printf("\n");


printf("Please enter the contact's city: ");
scanf("%c", (*contactAddress).city);
printf("\n");

}




// Get and store from standard input the values for Numbers
// Put your code here that defines the Contact getNumbers function:
void getNumbers(struct Numbers *contactNumber) {
char choice[5];

printf("Do you want to enter a cell phone number? (y or n) ");
scanf("%c", &choice[0]);



if (choice[0] == 'y') {
printf("\n");
printf("Please enter the contact’s cell phone number: ");
scanf("%d", (*contactNumber).cell);
printf("\n");

}

if (choice[0] == 'n') {
printf("\n");
}

printf("Do you want to enter a home phone number? (y or n) ");
scanf("%c", &choice[1]);

if (choice[1] == 'y') {
printf("\n");
printf("Please enter the contact’s home phone number: ");
scanf("%d", (*contactNumber).home);
printf("\n");
}

if (choice[1] == 'n') {
printf("\n");
}

printf("Do you want to enter a home business number? (y or n) ");
scanf("%c", &choice[3]);

if (choice[2] == 'y') {
printf("\n");
printf("Please enter the contact’s business phone number: ");
scanf("%d", (*contactNumber).home);
printf("\n");
}

if (choice[2] == 'n') {
printf("\n");
}


}

最后:

联系人.h:

// Structure type Name declaration (Milestone 1)
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};

// Structure type Address declaration
// Place your code here... (from Milestone 1)

struct Address {
char street[41];
int streetNumber[1];
int apartmentNumber[1];
char postalCode[8];
char city [41];
};



// Structure type Numbers declaration
// Place your code here... (from Milestone 1)
struct Numbers {
int cell[21];
int home[21];
int business[21];
};




// Structure type Contact declaration
// Place your code here... (from Milestone 3)

struct Contact{
char name;
char address;
int number;
};


//------------------------------------------------------
// Function Prototypes
//------------------------------------------------------

// ====== Milestone 4 =======

// Get and store from standard input the values for Name
// Place your code here...
void getName(struct Name *);


// Get and store from standard input the values for Address
// Place your code here...
void getAddress(struct Address *);

// Get and store from standard input the values for Numbers
// Place your code here...

void getNumbers(struct Numbers *);

最佳答案

当您说 extern struct Name name; 时,它的意思是“在其他文件中定义了一个变量 name”。但是真的没有!这就是链接器所提示的 - 它无法在任何文件中找到变量。

如果你只是删除 extern,变量将在 main 中定义,你可以在那里使用它们。

关于c - 获取 LNK2001 : unresolved external symbol when I run my program 的错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48135612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com