获取LNK2001的错误代码:运行程序

时间:2018-01-07 08:38:56

标签: c windows project

我的代码出现此错误:

  

1> a1ms4.obj:错误LNK2001:未解析的外部符号_name

     

1> a1ms4.obj:错误LNK2001:未解析的外部符号_address

     

1> a1ms4.obj:错误LNK2001:未解析的外部符号_numbers

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

a1ms4.c,contacts.c,contacts.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;
}

contacts.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");
        }


    }

最后:

contacts.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 *);

1 个答案:

答案 0 :(得分:2)

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

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